リア充爆発日記

You don't even know what ria-ju really is.

vagrant + berkshelf + chef で鼻血を出したままec2を起動させてみる

vagrant + berkshelf + chefで環境をつくってみたら鼻血が出たついでに、AWSと連携してec2のインスタンスを起動させてみようと思う。

参考

公式
https://github.com/mitchellh/vagrant-aws

敬愛するアイディーナオヤさんのエントリを見ればもうこれは成功したも同然。
http://d.hatena.ne.jp/naoya/20130315/1363340698

ダミーのboxを追加

起動させるのはEC2だけど、Vagrantの構成上、boxが必要らしい。

$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

プラグインのインストール

$ vagrant plugin install vagrant-aws

アイディーナオヤさんは必要ないかもって言ってたけど必要だった。
インスコしてないと

The provider 'aws' could not be found, but was requested to
back the machine 'default'. Please use a provider that exists.

って言われる。

cookbook作成

$ berks cookbook ec2

Vagrantfile修正

Vagrant.configure("2") do |config|
#  config.vm.hostname = "ec2-berkshelf"
  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id     = ENV['AWS_ACCESS_KEY_ID']
    aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
    aws.keypair_name = "ria10"
    override.ssh.username = "ec2-user"
    override.ssh.private_key_path = "~/.ssh/ria10.pem"
    aws.instance_type = "m1.small"
    aws.region = "ap-northeast-1"
    aws.ami = "ami-xxxxxxxx"
    aws.security_groups = ['AWS-OpsWorks-Web-Server']
  end
~snip~

config.vm.boxなど、必要そう以外なものは全部コメントアウトしておいたほうがいいかも。networkとか。
usernameとprivate_key_pathは今は第2引数(上記のoverride)を使うようだった。

vagrant up

$ vagrant up --provider=aws 

こんな感じでprovider指定をする。

で、たぶんエラーが起きる。mkdir -p /vagrantのとこで。
requirettyのせいだ。起動はしているけどこれだとプロビジョンはされない。困った。

で、cloud-init使ってrequirettyの設定変えればよくね?という意見があったのでやってみたけどうまくいかない。
https://github.com/mitchellh/vagrant/issues/1482

たぶん、cloud-initがうごくタイミングよりmkdir -pのほうが早いんだ。同じ意見の人がいたのでもう僕もあきらめることにした。
http://chiastolite.github.io/blog/2013/05/10/vagrant-aws/

しょうがないのでrequirettyの設定を変えた状態のAMIを用意することに。もともとrbenvとRubyのインストールをした状態のAMIを用意することにしてたので(Rubyにインストールはすごく時間かかるから)手間でもないし。

このAMIの作る手順は別エントリに→chefで管理する前提のRails用のEC2インスタンスを作る


このAMIでリトライ。

[ria10@localhost ec2 (master)]$ vagrant up --provider=aws
Bringing machine 'default' up with 'aws' provider...
[Berkshelf] This version of the Berkshelf plugin has not been fully tested on this version of Vagrant.
[Berkshelf] You should check for a newer version of vagrant-berkshelf.
[Berkshelf] If you encounter any errors with this version, please report them at https://github.com/RiotGames/vagrant-berkshelf/issues
[Berkshelf] You can also join the discussion in #berkshelf on Freenode.
[Berkshelf] Updating Vagrant's berkshelf: '/Users/ria10/.berkshelf/vagrant/berkshelf-20130521-79131-tykzh'
[Berkshelf] Using ec2 (0.1.0) at path: '/Users/ria10/vagrant/chef_kitchen/ec2'
[default] Warning! The AWS provider doesn't support any of the Vagrant
high-level network configurations (`config.vm.network`). They
will be silently ignored.
[default] Launching an instance with the following settings...
[default]  -- Type: m1.small
[default]  -- AMI: ami-xxxxxxxx
[default]  -- Region: ap-northeast-1
[default]  -- Keypair: ria10
[default]  -- Security Groups: ["AWS-OpsWorks-Web-Server"]
[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: /Users/ria10/vagrant/chef_kitchen/ec2/ => /vagrant
[default] Rsyncing folder: /Users/ria10/.berkshelf/vagrant/berkshelf-20130521-79131-tykzh/ => /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: chef_solo...
Generating chef JSON and uploading...
Running chef-solo...
[2013-05-23T12:40:07+00:00] INFO: *** Chef 11.4.4 ***
[2013-05-23T12:40:09+00:00] INFO: Setting the run_list to ["recipe[ec2::default]"] from JSON
[2013-05-23T12:40:09+00:00] INFO: Run List is [recipe[ec2::default]]
[2013-05-23T12:40:09+00:00] INFO: Run List expands to [ec2::default]
[2013-05-23T12:40:09+00:00] INFO: Starting Chef Run for ip-10-162-26-246.ap-northeast-1.compute.internal
[2013-05-23T12:40:09+00:00] INFO: Running start handlers
[2013-05-23T12:40:09+00:00] INFO: Start handlers complete.
[2013-05-23T12:40:09+00:00] INFO: Chef Run complete in 0.003765445 seconds
[2013-05-23T12:40:09+00:00] INFO: Running report handlers
[2013-05-23T12:40:09+00:00] INFO: Report handlers complete

できた!