CloudFormation + Chef-soloで環境構築

世間はCloudTailだのWorkSpacesだのAppStreamだので盛り上がっている中、地道にCloudFormationと戦っているのが僕です。

今回はCloudFormationからChef-soloをキックして環境構築してみました。

参考文献はこちら。



Chef Recipe

RecipeはS3に配置する想定です。必要なのはRecipe自体のtar.gzファイルとJSONファイル。

tar.gzファイルは以下構成である必要があります(myreceipeがレシピ名)

JSONファイルはこんな感じです。
{
  "run_list": [ "recipe[myrecipe]" ]
}

Template

Templateの中身はこんな感じ。長いです。gistに張ってます
Cloud-Initからcfn-initを叩いて、こんな感じで動きます。
  1. yumで各パッケージをインストール(config1)した後、
  2. alternativesでRubyを1.9に切り替えてgemでchefをインストール(config2)、
  3. その後Chef-soloで使うsolo.rbを作成(config3)し、
  4. chef-soloコマンドでrecipeを読み込んで実行。

Parameters

このTemplateをCloudFormationのStackに食わせたときのParametersがこちら。


  • EC2InstanceType ... EC2インスタンスのタイプを設定します。選択肢は"t1.micro", "m1.small", "m1.large"の3つで、Defaultではt1.microが入力されています。
  • RecipeURL ... 実行するChef recipe(tar.gzファイル)のURLを指定します。
  • JsonURL ... Chef-soloに渡すJSONファイルのURLを指定します。
  • KeyName ... 認証鍵名を指定します。

Result

Opscode Communityで公開されているbuild-essentialというRecipeを実行してみました。
/var/log/cfn-init.logにcfn-initの実行ログがあります。
2013-11-14 08:18:22,892 [INFO] Running configSets: default
2013-11-14 08:18:22,893 [INFO] Running configSet default
2013-11-14 08:18:22,939 [INFO] Running config config1
2013-11-14 08:18:49,925 [INFO] Yum installed [u'gcc-c++', u'ruby19', u'rubygems', u'ruby19-devel']
2013-11-14 08:18:49,946 [INFO] Running config config2
2013-11-14 08:18:49,960 [INFO] Command alternatives succeeded
2013-11-14 08:19:44,386 [INFO] Command install-chef succeeded
2013-11-14 08:19:44,413 [INFO] Command mkdir-chef succeeded
2013-11-14 08:19:44,422 [INFO] Running config config3
2013-11-14 08:19:44,427 [INFO] ConfigSets completed
2013-11-14 08:20:08,241 [INFO] Starting new HTTP connection (1): 169.254.XXX.XXX
2013-11-14 08:20:08,248 [INFO] Starting new HTTPS connection (1): cloudformation-waitcondition-us-west-2.s3-us-west-2.amazonaws.com
chef-soloの実行ログは/var/chef-solo/result.logにあります。
[2013-11-14T08:19:45+00:00] INFO: Forking chef instance to converge...
[2013-11-14T08:19:45+00:00] INFO: *** Chef 11.8.0 ***
[2013-11-14T08:19:45+00:00] INFO: Chef-client pid: 4493
[2013-11-14T08:19:47+00:00] INFO: Setting the run_list to ["recipe[build-essential]"] from JSON
[2013-11-14T08:19:47+00:00] INFO: Run List is [recipe[build-essential]]
[2013-11-14T08:19:47+00:00] INFO: Run List expands to [build-essential]
[2013-11-14T08:19:47+00:00] INFO: Starting Chef Run for ip-XXX-XXX-XXX-XXX.us-west-2.compute.internal
[2013-11-14T08:19:47+00:00] INFO: Running start handlers
[2013-11-14T08:19:47+00:00] INFO: Start handlers complete.
[2013-11-14T08:19:47+00:00] INFO: Processing package[autoconf] action install (build-essential::rhel line 38)
[2013-11-14T08:19:48+00:00] INFO: package[autoconf] installing autoconf-2.63-5.1.7.amzn1 from amzn-main repository
[2013-11-14T08:19:51+00:00] INFO: Processing package[bison] action install (build-essential::rhel line 38)
[2013-11-14T08:19:52+00:00] INFO: package[bison] installing bison-2.4.1-5.7.amzn1 from amzn-main repository
[2013-11-14T08:19:54+00:00] INFO: Processing package[flex] action install (build-essential::rhel line 38)
[2013-11-14T08:19:56+00:00] INFO: package[flex] installing flex-2.5.36-1.8.amzn1 from amzn-main repository
[2013-11-14T08:19:58+00:00] INFO: Processing package[gcc] action install (build-essential::rhel line 38)
[2013-11-14T08:19:59+00:00] INFO: Processing package[gcc-c++] action install (build-essential::rhel line 38)
[2013-11-14T08:19:59+00:00] INFO: Processing package[kernel-devel] action install (build-essential::rhel line 38)
[2013-11-14T08:19:59+00:00] INFO: package[kernel-devel] installing kernel-devel-3.4.68-59.97.amzn1 from amzn-updates repository
[2013-11-14T08:20:05+00:00] INFO: Processing package[make] action install (build-essential::rhel line 38)
[2013-11-14T08:20:07+00:00] INFO: Processing package[m4] action install (build-essential::rhel line 38)
[2013-11-14T08:20:07+00:00] INFO: Chef Run complete in 19.951180673 seconds
[2013-11-14T08:20:07+00:00] INFO: Running report handlers
[2013-11-14T08:20:07+00:00] INFO: Report handlers complete
ちゃんと実行されてますね!

まとめ

コレ作るのに2日かかりました。StackがRollbackするたびに超イライラした。でもcfn-initとChefの動きについて大分理解が進んで良かったです。



入門Chef Solo - Infrastructure as Code入門Chef Solo - Infrastructure as Code


伊藤直也
売り上げランキング : 549

Amazonで詳しく見る by AZlink

このブログの人気の投稿

転職のお知らせ

40代の減量戦略 〜体重-14kg、体脂肪率-12%を実現した具体的な方法〜

45歳になりました