LoginSignup
3
4

More than 5 years have passed since last update.

Vagrant使用手順メモ(AWS EC2使用)

Last updated at Posted at 2015-01-18

Vagrantセットアップ

  1. Vagrant本体インストール
  2. Vagrant公式サイトを参照)

  3. プラグインインストール
  4. $ vagrant plugin install vagrant-aws
  5. EC2用Box追加
  6. 
    $ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
    
  7. Vagrantfile作成
  8. 
    $ mkdir vagrant-sample
    $ cd vagrant-sample
    $ vagrant init
    
  9. Vagrantfile編集
  10. 以下、Vagrantfileの設定例
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "dummy"
    
      config.vm.provider :aws do |aws, override|
        # AWS Access
        aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
        aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
        aws.keypair_name = "KEYPAIR NAME"
    
        # Instance config
        aws.instance_type = "t2.micro"
        aws.region = "ap-northeast-1"
        aws.availability_zone =  "ap-northeast-1a"
        aws.ami = "ami-xxxxxxxx"
        aws.security_groups = ['SECURITY GROUP']
        aws.tags = {
          'Name' => 'Instance Name',
          'Description' => 'Instance Description'
        }
    
        # VPC config
        aws.subnet_id = "subnet-xxxxxxxx"
    
        # for Amazon Linux
        aws.user_data = "#!/bin/sh\nsed -i 's/^.*requiretty/#Defaults requiretty/' /etc/sudoers\n"
    
        # SSH
        override.ssh.username = "ec2-user"
        override.ssh.private_key_path = "private-key.pem"
        override.ssh.pty = true
      end
    end
    
    (その他設定内容参考:mitchellh/vagrant-aws

  11. AWSアクセスキー設定
  12. 事前に取得したアクセスキーを環境変数に設定
    
    $ export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
    $ export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    

EC2インスタンス起動・終了

  1. 仮想マシン起動(EC2インスタンス生成)
  2. 
    $ vagrant up --provider=aws --no-provision
    
  3. Provisioning実行
  4. 
    $ vagrant provision
    
  5. 終了(EC2インスタンス破棄)
  6. 
    $ vagrant destroy
    

仮想マシンその他操作一覧

  • SSH接続
    
    $ vagrant ssh
    
  • 再起動
    
    $ vagrant reload
    
  • 停止
    
    $ vagrant halt
    
  • 状態確認
    
    $ vagrant status
    
  • SSH接続情報確認
    
    $ vagrant ssh-config
    

Vagrantその他プラグイン

  • AWS SDK
    $ vagrant plugin install aws-sdk
  • 仮想マシンのProxy設定
    $ vagrant plugin install vagrant-proxyconf
  • 仮想マシンにChef-Soloインストール
    $ vagrant plugin install vagrant-omnibus
  • サーバ構成テスト
    $ vagrant plugin install vagrant-serverspec
  • 作成したインスタンスをAMI化
    $ vagrant plugin install vagrant-ami
  • 仮想マシンの状態管理
    $ vagrant plugin install sahara
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4