GWはVagrantでAWSを操作してリア充になりたかったので、やったことをメモ。
AWS自体ほぼ初めて触るので、使い方の確認も兼ねてます。
VPCやらEBSについてはいずれやりたいと思います。
ec2-cliもお勉強が必要かな。
参考文献:
書籍「実践Vagrant」
vagrantではじめるクラウド開発環境(AWS編)
環境
ホストOS:Windows 7 Standard 64bit
Vagrant:1.5.0
コンソール:Git Bash 1.8.3
準備
AWSアカウントの作成
かつあい
vagrantユーザの作成
-
Service > Deployment & Management > IAMに移動 - 左のメニューの
Usersに移動し、Create New Usersをクリック -
vagrantと入力してCreateをクリック - 完了画面が表示されるので、
Access Key IdとSecret Access Keyを控えた上でClose Windowをクリック - 左のメニューの
Groupsに移動し、Create New Groupをクリック -
vagrantと入力してContinueをクリック -
Power User AccessのところのSelectをクリック -
Continueをクリック -
Create groupをクリック -
Usersに移動し、vagrantユーザを選択して右画面下のGroupsタブを選択し、Add user to groupsをクリック -
vagrantをチェックしてAdd to Groupsをクリック
Security Groupの作成
1.Service > Compute & Networking > EC2に移動
2.NETWORK & SECURITY > Security Groupsに移動
3.Create Security Groupをクリック
4.グループ情報について下記のように入力
| 項目名 | 値 |
|---|---|
| Security group name | vagrant |
| Description | vagrant security group |
| VPC | デフォルト(*がついている項目) |
5.Inboundタブを選択してAdd Ruleをクリックし、下記のルールを追加
| Type | Protocol | Port Range | Source |
|---|---|---|---|
| SSH | TCP | 22 | Anywhere |
| HTTP | TCP | 80 | Anywhere |
| HTTPS | TCP | 443 | Anywhere |
6.Createをクリック
SSHの鍵を作成
1.Service > Compute & Networking > EC2に移動
2.NETWORK & SECURITY > Key Pairsに移動
3.Create Key Pairをクリック
4.vagrantと入力してCreateをクリック → vagrant.pemをダウンロード
5.~/.sshにvagrant.pemを格納
Vagrant、Chefのインストール
かつあい
rsyncのインストール
rsyncをインストールしないと、Chef-solo実行時にレシピを転送できなくてエラーになる。
VirtualBoxが対象だとrsyncがなくても動作するけど、AWSは駄目らしい。
1.Cygwinをインストール(インストーラのURL)
2.C:\cygwin64\binディレクトリから下記のファイルをC:\Program Files (x86)\Git\binディレクトリにコピー
- rsync.exe
- cygiconv-2.dll
- cygintl-8.dll
- cygpopt-0.dll
- cygwin1.dll
3.Cygwinが不要の場合はアンインストール
Vagrantの設定
プラグインvagrant-awsとvagrant-omnibusとdotenvをインストール
vagrant-awsはAWSプロバイダを利用するためのプラグイン。
vagrant-omnibusはゲストOSにChefをインストールするためのプラグイン。
dotenvはカレントディレクトリの.envファイルに値の宣言をすることで、環境変数として読み込むことができるGem。
アクセスキーなどをVagrantfileに直接書かずに外部ファイル化できる。社内プロキシの設定などもこれで定義したほうがよさそう。
$ vagrant plugin install vagrant-aws
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Installed the plugin 'vagrant-aws (0.4.1)'!
$ vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Installed the plugin 'vagrant-omnibus (1.4.1)'!
$ vagrant plugin install dotenv
Installing the 'dotenv' plugin. This can take a few minutes...
Installed the plugin 'dotenv (0.11.1)'!
~/vagrant-awsディレクトリを作成
以降はここで作業。
クックブックを作成
Apacheをインストールするクックブックを作成する。
$ knife solo init .
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...
$ knife cookbook create -o site-cookbooks apache2
** Creating cookbook apache2
** Creating README for cookbook: apache2
** Creating CHANGELOG for cookbook: apache2
** Creating metadata for cookbook: apache2
** Creating specs for cookbook: apache2
#
# Cookbook Name:: apache2
# Recipe:: default
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package "httpd"
service "httpd" do
action [:enable, :start]
end
~/vagrant-aws/.envにAWSの認証情報を記載
# providerのデフォルトをAWSに変更
VAGRANT_DEFAULT_PROVIDER="aws"
# AWSの認証情報
AWS_SSH_USERNAME="ec2-user"
AWS_SSH_KEY="~/.ssh/vagrant.pem"
AWS_ACCESS_KEY_ID="<ユーザ作成時のAccess Key Id>"
AWS_SECRET_ACCESS_KEY="<ユーザ作成時のSecret Access Key>"
AWS_KEYPAIR_NAME="vagrant"
AWS_SECURITY_GROUP="vagrant"
~/vagrant-aws/Vagrantfile.chefにSSLの設定を記載
本設定をしないと、chef_soloでのプロビジョニング時に警告が発生する。
詳細はハマったことのメモを参照。
Chef::Config.ssl_verify_mode = :verify_peer
Vagrantfileの作成
1.Vagrantfileの初期化
$ vagrant init dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
2.Vagrantfileの編集
vagrant initで出力されたコメントは割愛。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# .envの情報を読み込み
Dotenv.load
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
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.vm.provider :aws do |provider, override|
provider.access_key_id = ENV['AWS_ACCESS_KEY_ID']
provider.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
provider.keypair_name = ENV['AWS_KEYPAIR_NAME']
# EC2インスタンスの設定
provider.region = "ap-northeast-1"
provider.availability_zone = "ap-northeast-1a"
# Amazon LinuxのAMIを指定
# 今回はEC2のIMAGES > AMIsで検索したAMIを指定
provider.ami = "ami-c9562fc8"
provider.instance_type = "t1.micro"
provider.instance_ready_timeout = 120
provider.terminate_on_shutdown = false
provider.security_groups = [ ENV['AWS_SECURITY_GROUP'] ]
provider.tags = {
"Name" => "vagrant-aws",
"Description" => "Boot from vagrant-aws",
}
# sudoをttyでログインしているユーザからも実行できるようにする
# 自作AMIを用意する際にはこの設定をあらかじめしておく
provider.user_data = <<-USER_DATA
#!/bin/sh
echo "Defaults !requiretty" > /etc/sudoers.d/vagrant-init
chmod 440 /etc/sudoers.d/vagrant-init
USER_DATA
# AWSへの接続ユーザおよび秘密鍵の指定
override.ssh.username = ENV['AWS_SSH_USERNAME']
override.ssh.private_key_path = ENV['AWS_SSH_KEY']
override.vm.synced_folder ".", "/home/ec2-user/vagrant", disabled: true
end
# chefのインストール/アップデート
config.omnibus.chef_version = :latest
# chef soloの設定
config.vm.provision :chef_solo do |chef|
chef.custom_config_path = "Vagrantfile.chef"
chef.cookbooks_path = ["cookbooks/", "site-cookbooks/"]
chef.add_recipe "apache2"
end
end
EC2インスタンスの作成+起動+プロビジョニング
$ vagrant up
Bringing machine 'default' up with 'aws' provider...
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properl
y encoded.
==> default: HandleBoxUrl middleware is deprecated. Use HandleBox instead.
==> default: This is a bug with the provider. Please contact the creator
==> default: of the provider you use to fix this.
==> 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: Launching an instance with the following settings...
==> default: -- Type: t1.micro
==> default: -- AMI: ami-c9562fc8
==> default: -- Region: ap-northeast-1
==> default: -- Availability Zone: ap-northeast-1a
==> default: -- Keypair: vagrant
==> default: -- User Data: yes
==> default: -- Security Groups: ["vagrant"]
==> default: -- User Data: #!/bin/sh
==> default: echo "Defaults !requiretty" > /etc/sudoers.d/vagrant-init
==> default: chmod 440 /etc/sudoers.d/vagrant-init
==> default: -- Block Device Mapping: []
==> default: -- Terminate On Shutdown: false
==> default: -- Monitoring: false
==> default: -- EBS optimized: false
==> default: Waiting for instance to become "ready"...
==> default: Waiting for SSH to become available...
(中略)
無事Apacheをインストールしたインスタンスが作成されました。
SSHで接続
$ vagrant ssh
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-ami/2014.03-release-notes/
1 package(s) needed for security, out of 18 available
Run "sudo yum update" to apply all updates.
[ec2-user@*** ~] $ rpm -q httpd
httpd-2.2.27-1.2.amzn1.x86_64
複数のインスタンスを構築
# -*- mode: ruby -*-
# vi: set ft=ruby :
Dotenv.load
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
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.omnibus.chef_version = :latest
# 1つ目のインスタンス
config.vm.define :node01 do |node|
# インスタンス毎に個別に設定したい内容を記載
configure_node(node) do |aws, override|
aws.ami = "ami-c9562fc8"
aws.instance_type = "t1.micro"
aws.tags = {
"Name" => "aws-node01",
"Description" => "Boot from vagrant-aws",
}
end
node.vm.provision :chef_solo do |chef|
chef.custom_config_path = "Vagrantfile.chef"
chef.cookbooks_path = ["cookbooks/", "site-cookbooks/"]
chef.add_recipe "apache2"
end
end
# 2つ目のインスタンス
config.vm.define :node02 do |node|
configure_node(node) do |aws, override|
aws.ami = "ami-c9562fc8"
aws.instance_type = "t1.micro"
aws.tags = {
"Name" => "aws-node02",
"Description" => "Boot from vagrant-aws",
}
end
node.vm.provision :chef_solo do |chef|
chef.custom_config_path = "Vagrantfile.chef"
chef.cookbooks_path = ["cookbooks/", "site-cookbooks/"]
chef.add_recipe "apache2"
end
end
# AWS認証設定など、全インスタンス共通の設定を集約
def configure_node(node, &block)
node.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 = ENV['AWS_KEYPAIR_NAME']
aws.region = "ap-northeast-1"
aws.availability_zone = "ap-northeast-1a"
aws.instance_ready_timeout = 120
aws.terminate_on_shutdown = false
aws.security_groups = [ ENV['AWS_SECURITY_GROUP'] ]
aws.user_data = <<-USER_DATA
#!/bin/sh
echo "Defaults !requiretty" > /etc/sudoers.d/vagrant-init
chmod 440 /etc/sudoers.d/vagrant-init
USER_DATA
override.ssh.username = ENV['AWS_SSH_USERNAME']
override.ssh.private_key_path = ENV['AWS_SSH_KEY']
override.vm.synced_folder ".", "/home/ec2-user/vagrant", disabled: true
block.call(aws, override)
end
end
end
その他メモ
Instance IDをローカルで取得
# config.vm.defineで指定した名称を指定
$ cat .vagrant/machines/node01/aws/id
ハマったところのメモ
インスタンスは作成できるが、Chefのクックブックを転送できない
rsyncをインストールする。VirtualBoxだとrsyncがなくてもchef_soloでプロビジョニングできるが、AWSだとエラーになる。
SSL validation of HTTPS requests is disabled.というWARNメッセージが出力される
chef_solo実行時に下記のような警告メッセージが出力される。
[2014-05-05T03:12:59+00:00] WARN:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SSL validation of HTTPS requests is disabled. HTTPS connections are still encrypted, but chef is not able to detect forged replies or man in the middle attacks.
To fix this issue add an entry like this to your configuration file:
# Verify all HTTPS connections (recommended)
ssl_verify_mode :verify_peer
# OR, Verify only connections to chef-server
verify_api_cert true
To check your SSL configuration, or troubleshoot errors, you can use the `knife ssl check` command like so:
knife ssl check -c /tmp/vagrant-chef-1/solo.rb
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
この記事を参考にして対応しました。
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.というWARNメッセージが出力される
この記事によるとvagrant plugin install unfでunfをインストールすれば解決すると記載されていますが、
やってみたところ下記のエラーが発生しました。
$ vagrant plugin install unf
Installing the 'unf' plugin. This can take a few minutes...
Installed the plugin 'unf (0.1.4)'!
$ vagrant provision
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.
C:/Users/ユーザ名/.vagrant.d/gems/gems/unf_ext-0.0.6-x86-mingw32/lib/unf_ext.rb:6:in `require': cannot load such file -- unf_ext.so (LoadError)
unf_ext.rbを確認したところ、lib/Rubyのバージョン/ext_unf.soを読み込もうとしているところで例外が発生している。
~/.vagrant.d/gems/rubyを見るとRubyのバージョンは2.0.0なのに対し、
~/.vagrant.d/gems/gems/unf_ext-0.0.6-x86-mingw32/libディレクトリの下には1.8と1.9しかないのが原因っぽい。
Rubyのバージョンを下げればいいのかな?
とりあえずchef_soloは動くので、vagrant plugin uninstall unfでunfをアンインストールしました。