お久しぶりです。
最近またAWSのサービスを一通り触ってみていて、プロビジョニングまでAWSマネージドでやってくれるOpsWorksを本番環境で使ってみたいと思い、Chefに取り組んでいます。
Chefにはレシピをローカルのvagrantで簡単に実行・テストできる test-kitchen というgemのツールがあるのですが、
これを初めて使ってみたところ非常に使い勝手がよく、当然ながらEC2でも試したいという気になりました。
今回は test-kitchen からEC2を扱える、kitchen-ec2を使ってみます。
環境
- 実行元は手元の MBP macOS High Sierra 10.13.5
- ruby 2.3.3 (rbenv) gem 2.7.2
- Chef DK 3.0.36 をインストール
- Test Kitchen version 1.21.2
- aws-cli/1.11.63 Python/3.5.2
test-kitchen は単体でgemからインストールできますが、Chef DKに付いてくるので真っ新な環境ではこっちを入れた方がよいと思います。
Chef DK
https://downloads.chef.io/chefdk
AWS CLI で認証情報を設定
kitchen-ec2では、AWSの認証情報(APIキーとトークン)は環境変数か、aws-cliの認証情報から読み込むことになります。
今回は既にインストール済みのaws-cliを使っていますが、必要に応じて先に aws configure を行ってAPIキーを登録してください。
参考
AWS CLI の設定
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-chap-getting-started.html
インストールと準備
ChefDKで入れたtest-kitchenにはkitchen-ec2は付いてこないので、個別にgemで入れます。
1 |
$ gem install kitchen-ec2 |
適当なディレクトリでmetadata.rbを作り、それを元にinitし、
サンプルとして、適当なディレクトリを作成するレシピを用意します。
1 2 3 4 5 6 7 8 9 10 11 |
$ mkdir createdir $ cd createdir $ vim metadata.rb (以下で作成) name "createdir" version "0.1.0" $ kitchen init $ mkdir recipes $ vim recipes/default.rb |
1 2 3 4 5 6 7 |
directory "/tmp/path/to/example/recipe-executed" do mode 0755 owner 'root' group 'root' recursive true action :create end |
これではローカルvagrantでしか動作しないため、
initで作られた kitchen.yml を編集します。
kitchen-ec2のREADMEにあるExampleを元に編集します。
https://github.com/test-kitchen/kitchen-ec2#example
上記READMEにVPC内に起動するためのパラメータも用意されていますが、今回は手っ取り早くVPC外に起動することにします。
現行世代のm4やm5、t2インスタンスはVPC外で起動できないため、旧世代である m3.medium で起動しています。
参考
VPC でのみ利用可能なインスタンスタイプ
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/using-vpc.html#vpc-only-instance-types
よりコストを気にする場合は、VPC内にt2.nanoやt2.microなどで起動するのが良いかもしれません。
以下のようになりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
driver: name: ec2 aws_ssh_key_id: keypair-name security_group_ids: ["sg-xxxxxx"] region: ap-northeast-1 availability_zone: ap-northeast-1a require_chef_omnibus: true instance_type: m3.medium transport: ssh_key: /path/to/your/keypair-name.pem connection_timeout: 10 connection_retries: 5 username: ec2-user provisioner: name: chef_solo platforms: - name: AmazonLinux-201803 driver: image_id: ami-92df37ed suites: - name: default run_list: - recipe[createdir::default] attributes: |
実際に使う場合は、以下の編集が最低限必要になります。
- aws_ssh_key_id: AWSのキーペア名
- ssh_key:上で指定したキーペアに対応する秘密鍵のファイルパス
- security_group_ids: sshアクセス可能なセキュリティグループのID
converge の実行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$ kitchen converge -----> Starting Kitchen (v1.21.2) -----> Creating <default-AmazonLinux-201803>... Detected platform: amazon version 2018.03. on x86_64. Instance Type: m3.medium. Default username: ec2-user (default). If you are not using an account that qualifies under the AWS free-tier, you may be charged to run these suites. The charge should be minimal, but neither Test Kitchen nor its maintainers are responsible for your incurred costs. Instance <i-xxxxxxxxxxxx> requested. Polling AWS for existence, attempt 0... Attempting to tag the instance, 0 retries EC2 instance <i-xxxxxxxxxxxx> created (中略) resolving cookbooks for run list: ["createdir::default"] Synchronizing Cookbooks: - createdir (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: createdir::default * directory[/tmp/path/to/example/recipe-executed] action create - create new directory /tmp/path/to/example/recipe-executed - change mode from '' to '0755' - change owner from '' to 'root' - change group from '' to 'root' Running handlers: Running handlers complete Chef Client finished, 2/2 resources updated in 06 seconds Downloading files from <default-AmazonLinux-201803> Finished converging <default-AmazonLinux-201803> (0m26.39s). -----> Kitchen is finished. (1m19.83s) |
うまく実行されたようでしたので、実際にログインして確認してみます。
1 2 3 4 5 6 7 8 9 10 11 12 |
$ kitchen login __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/ 10 package(s) needed for security, out of 12 available Run "sudo yum update" to apply all updates. [ec2-user@ip-10-186-43-xxx ~]$ ls -al /tmp/path/to/example drwxr-xr-x 2 root root 4096 6月 8 16:01 recipe-executed |
ディレクトリ作成のレシピに沿って、正しく作られていることが確認できました。
インスタンスの破棄
AWSですので無駄な課金が走らないように、テスト後は忘れずに削除しましょう。
ローカルのvagrantと同様に kitchen destroy を実行するだけで、EBSも含めてシャットダウンとリソースの削除が行われました。
1 2 3 4 5 6 |
$ kitchen destroy -----> Starting Kitchen (v1.21.2) -----> Destroying <default-AmazonLinux-201803>... EC2 instance <i-xxxxxxxxx> destroyed. Finished destroying <default-AmazonLinux-201803> (0m0.37s). -----> Kitchen is finished. (0m3.61s) |
所感
Chefの学習コストが高いと聞いていましたが思ったほどではなさそうでした。
OpsWorksはカスタムAMIを使うことで、一切Chefを使わない運用も可能ではあるのですが、カスタムAMIとChefによるプロビジョンを併用することで、より起動時間と柔軟性を自由にデザインした構成が実現できそうです。