VagrantのプロバイダとしてEC2を利用する。

本番で利用するようなEC2インスタンスを、そのままVagrantコマンドで透過的に操作できると便利と思い、

まあ、実際のところはVirtualBoxなどからVagrantboxを作成するのが面倒になったのだけど。。。

vagrant-aws-plugin を用いて、ローカルのvagrantから指定AWS VPC内のインスタンスをupしたり、

haltしたり操作できるようにしてみる。

前提

  • Vagrant1.7.8
  • MacOS siella 10.12
  • Java1.8.0_25
  • aws cliツールはインストール済み

まずはvagrant-aws-pluginをインストールする

$ vagrant plugin install vagrant-aws

Vagrantファイルは以下のようになる

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "dummy"
  config.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
  config.ssh.pty = true
  config.vm.provider :aws do |aws, override|
    aws.access_key_id = ENV["AWS_ACCESS_KEY"]
    aws.secret_access_key = ENV["AWS_SECRET_KEY"]
    aws.ami = "{AMI-id}"
    aws.instance_type="t2.micro"
    aws.security_groups=["{セキュリティグループ}", "{セキュリティグループ}"]
    aws.keypair_name = "{キーペア名}"
    aws.region = "ap-northeast-1"
    aws.associate_public_ip = true
    aws.subnet_id = "{subnet id}"
    override.ssh.username= ENV['AWS_USER_NAME']
    override.ssh.private_key_path = '~/.ssh/id_rsa'
  end
  config.vm.synced_folder "../web", "/var/www/web", create: true, owner: "apache", group:"apache", mount_options: ["dmode=775", "fmode=664"]
  • config.vm.providerには:awsを指定

  • AWSキーなどべた書きにしたくないので以下のようにして、環境変数から取得する

macなら ~/.bashrcなんかに

export AWS_ACCESS_KEY=xxxxxxxxx
export AWS_SECRET_KEY=xxxxxxxxx
export AWS_USER_NAME=xxxxx

を記述する。

そして上記がととのったら

$ vagrant up

Bringing machine 'default' up with 'aws' provider...
==> default: Warning! The AWS provider doesn't support any of the Vagrant
==> default: high-level network configurations (`config.vm.network`). They
==> default: will be silently ignored.
==> default: Starting the instance...
==> default: Waiting for instance to become "ready"...
==> default: Waiting for SSH to become available...
==> default: Machine is booted and ready for use!
==> default: Rsyncing folder: xxxx
==> default: Rsyncing folder: xxxx
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

これでVirtualBoxなどローカルでイメージを作る場合と同じ要領で、カジュアルにEC2インスタンスVagrantとしてつかえるので便利です。

試しに、vagrant status

$ vagrant status
Current machine states:

default                   running (aws)

The EC2 instance is running. To stop this machine, you can run
`vagrant halt`. To destroy the machine, you can run `vagrant destroy`.

vagrant ssh

$ vagrant ssh
Last login: Fri Nov 25 07:34:49 2016 from x.x.x.x
[xxxxxx@ip-192-168-11-186 ~]$

Vagrantの共有フォルダ(ローカル<-->Vagrant)の扱い

EC2をVagrantのプロバイダとして利用する場合の、同期フォルダの扱いはrsyncとなる。

  • ローカル → rsync → EC2

気をつけなくてはいけないのは、EC2の変更はローカルに反映されない。

同期が発生するタイミングは、vagrant upを実行したときと手動で vagrant rsync を実行したタイミングとなる。

$ vagrant rsync

==> default: Rsyncing folder: ローカルフォルダ/ => リモートフォルダ/

ローカルに変更がある場合、自動でリモートに反映する

自動で反映するには、$ vagrant rsync-autoを実行する。

$ vagrant rsync-auto
==> default: Doing an initial rsync...
==> default: Rsyncing folder: ローカルフォルダ => リモートフォルダ
==> default: Watching: ローカルフォルダ
==> default: Watching: ローカルフォルダ

...

VagrantでEC2をプロバイダとすると、VirtualBoxなどをプロバイダとしてローカルに環境を構築する場合と同じ要領でインスタンスを操作できて、 非常に便利です。 例えば、本番で利用するamiを元にVagrant→EC2で開発をやれば、本番にしたとき環境の差異で動かない問題など解決するのではないでしょうか。

(・∀・)イイネ!!