この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、せーのです。巷ではやれdockerだ、Ansibleだとコンテナや新たな構成管理ツールに旅立たれてしまう方が多い中、まるでももクロにもAKBにも見向きもせずにMAXを応援し続けるが如くChefを見守っております。さて、今日はChefの操作ツール「knife」を使ってcloudformationを操作する、というプラグインのご紹介です。
名前は「knife-cloudformation」。できることはCLIとほぼ同等
こちらのプラグインの名前は「knife-cloudformation」と言いまして、現在リリースバージョンはv0.2.10。Developerブランチではつい10日前にもアップデートが行われている絶賛進化中のプラグインです。
- knife-cloudformation - https://github.com/hw-labs/knife-cloudformation/tree/develop
このプラグインではAWS CLIでできることはほぼ出来ます。コマンド一覧をご紹介します。
- knife cloudformation create NAME
- knife cloudformation describe NAME
- knife cloudformation destroy NAME [NAME]
- knife cloudformation events NAME
- knife cloudformation export NAME
- knife cloudformation import NEW_STACK_NAME [JSON_EXPORT_FILE]
- knife cloudformation inspect NAME
- knife cloudformation list NAME
- knife cloudformation promote NAME NAME
- knife cloudformation update NAME
- knife cloudformation NAME
オプションも非常に豊富です。豊富すぎて書ききれませんので--helpオプションで確認して下さい。例えばcreateコマンドなら
knife cloudformation create --help
ですね。 オプションにはknifeっぽく--local-modeというオプション(chefのlocal modeを使う)や--chef-zero-portというオプションがあったり、--print-onlyというようなテンプレートを確認するオプションなど多岐に渡りますので是非チェックしてみてください。ちなみにこのプラグイン、OpenStackやRackspaceにも使える等応用も効く非常に賢い子です。
インストール
ではインストールしてみましょう。githubからダウンロードし、インストールにはgemを使います。
Tsuyoshis-Air-3:~ Tsuyoshi$ cd ~/dev/
Tsuyoshis-Air-3:dev Tsuyoshi$ git clone https://github.com/hw-labs/knife-cloudformation.git knife-cloudformation
Cloning into 'knife-cloudformation'...
remote: Counting objects: 2038, done.
remote: Total 2038 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (2038/2038), 287.64 KiB | 166.00 KiB/s, done.
Resolving deltas: 100% (1223/1223), done.
Checking connectivity... done.
Tsuyoshis-Air-3:dev Tsuyoshi$ cd knife-cloudformation/
Tsuyoshis-Air-3:knife-cloudformation Tsuyoshi$ ls
CHANGELOG.md README.md
Gemfile knife-cloudformation.gemspec
LICENSE lib
ダウンロードが確認出来たらgemでインストールします。
Tsuyoshis-Air-3:knife-cloudformation Tsuyoshi$ sudo gem install knife-cloudformation
Password:
Fetching: http_parser.rb-0.6.0.gem (100%)
Building native extensions. This could take a while...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/ext/builder.rb:54: warning: Insecure world writable dir /Users/Tsuyoshi/bin/FDK/Tools/osx in PATH, mode 040777
Successfully installed http_parser.rb-0.6.0
Fetching: form_data-0.1.0.gem (100%)
Successfully installed form_data-0.1.0
Fetching: http-0.7.1.gem (100%)
Successfully installed http-0.7.1
Fetching: multi_xml-0.5.5.gem (100%)
Successfully installed multi_xml-0.5.5
Fetching: xml-simple-1.1.4.gem (100%)
Successfully installed xml-simple-1.1.4
Fetching: miasma-0.2.10.gem (100%)
Successfully installed miasma-0.2.10
Fetching: attribute_struct-0.2.8.gem (100%)
Successfully installed attribute_struct-0.2.8
Fetching: sparkle_formation-0.2.6.gem (100%)
Successfully installed sparkle_formation-0.2.6
Fetching: knife-cloudformation-0.2.10.gem (100%)
Successfully installed knife-cloudformation-0.2.10
Parsing documentation for http_parser.rb-0.6.0
unable to convert "\xCA" from ASCII-8BIT to UTF-8 for lib/ruby_http_parser.bundle, skipping
Installing ri documentation for http_parser.rb-0.6.0
Parsing documentation for form_data-0.1.0
Installing ri documentation for form_data-0.1.0
Parsing documentation for http-0.7.1
Installing ri documentation for http-0.7.1
Parsing documentation for multi_xml-0.5.5
Installing ri documentation for multi_xml-0.5.5
Parsing documentation for xml-simple-1.1.4
Installing ri documentation for xml-simple-1.1.4
Parsing documentation for miasma-0.2.10
Installing ri documentation for miasma-0.2.10
Parsing documentation for attribute_struct-0.2.8
Installing ri documentation for attribute_struct-0.2.8
Parsing documentation for sparkle_formation-0.2.6
Installing ri documentation for sparkle_formation-0.2.6
Parsing documentation for knife-cloudformation-0.2.10
Installing ri documentation for knife-cloudformation-0.2.10
9 gems installed
確認します。サブコマンドに[cloudformation]の一覧が見えていればインストール成功です。
Tsuyoshi$ knife --help
Usage: knife sub-command (options)
-s, --server-url URL Chef Server URL
--chef-zero-port PORT Port to start chef-zero on
-k, --key KEY API Client Key
--[no-]color Use colored output, defaults to false on Windows, true otherwise
-c, --config CONFIG The configuration file to use
--defaults Accept default values for all questions
-d, --disable-editing Do not open EDITOR, just accept the data as is
-e, --editor EDITOR Set the editor to use for interactive commands
-E, --environment ENVIRONMENT Set the Chef environment
-F, --format FORMAT Which format to use for output
-z, --local-mode Point knife commands at local repository instead of server
-u, --user USER API Client Username
--print-after Show the data after a destructive operation
-V, --verbose More verbose output. Use twice for max verbosity
-v, --version Show chef version
-y, --yes Say yes to all prompts for confirmation
-h, --help Show this message
Available subcommands: (for details, knife SUB-COMMAND --help)
......中略.....
** CLOUDFORMATION COMMANDS **
knife cloudformation create NAME
knife cloudformation describe NAME
knife cloudformation destroy NAME [NAME]
knife cloudformation events NAME
knife cloudformation export STACK_NAME
knife cloudformation import NEW_STACK_NAME [JSON_EXPORT_FILE]
knife cloudformation inspect NAME
knife cloudformation list NAME
knife cloudformation promote NEW_STACK_NAME DESTINATION
knife cloudformation update NAME
knife cloudformation validate
設定
インストールが終わったら次は設定です。設定はとても簡単で環境変数にAWS API KEY等のキー値を書き込み、knife.rbにその環境変数をセットすればOKです。
~/.chef/knife.rb
knife[:cloudformation][:credentials] = {
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:aws_region => ENV['AWS_REGION']
}
Tsuyoshi$ export AWS_ACCESS_KEY_ID=[AWS API KEY]
Tsuyoshi$ export AWS_SECRET_ACCESS_KEY=[AWS API SECRET KEY]
Tsuyoshi$ export AWS_REGION=[リージョン]
では試しにテンプレートを流してみましょう。手元にお正月に作ったテンプレートがあったのでこれを使います。
Tsuyoshis-Air-3:knife-cloudformation Tsuyoshi$ knife cloudformation create knifecfntest --file ~/dev/test.template
/Library/Ruby/Gems/2.0.0/gems/chef-11.8.0/lib/chef/mixin/path_sanity.rb:26: warning: Insecure world writable dir /Users/Tsuyoshi/bin/FDK/Tools/osx in PATH, mode 040777
Cloud Formation: create
-> Name: knifecfntest
DBUser: test
DBPassword: testtest
Cloud Formation Events for Stack: knifecfntest
Events for stack: knifecfntest
Time Resource Logical Id Resource Status Resource Status Reason
2015-01-06 08:25:10 UTC knifecfntest CREATE_IN_PROGRESS User Initiated
2015-01-06 08:25:22 UTC testDB CREATE_IN_PROGRESS
2015-01-06 08:25:22 UTC testEC2 CREATE_IN_PROGRESS
2015-01-06 08:25:23 UTC testEC2 CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:25:26 UTC testDB CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:26:11 UTC testEC2 CREATE_COMPLETE
2015-01-06 08:26:13 UTC testELB CREATE_IN_PROGRESS
2015-01-06 08:26:15 UTC testELB CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:26:16 UTC testELB CREATE_COMPLETE
2015-01-06 08:36:35 UTC testDB CREATE_COMPLETE
2015-01-06 08:36:37 UTC knifecfntest CREATE_COMPLETE
Stack create complete: SUCCESS
Outputs for stack: knifecfntest
No outputs found
途中にあるDBUserやDBPasswordというのはパラメータ値です。ここは対話式になるようです。では結果を確認します。
しっかり出来上がっていますね。ついでなのでコマンドで削除してみましょう。
Tsuyoshis-Air-3:knife-cloudformation Tsuyoshi$ knife cloudformation destroy knifecfntest
/Library/Ruby/Gems/2.0.0/gems/chef-11.8.0/lib/chef/mixin/path_sanity.rb:26: warning: Insecure world writable dir /Users/Tsuyoshi/bin/FDK/Tools/osx in PATH, mode 040777
WARNING: Destroying Cloud Formation: knifecfntest
Destroy formation? (Y/N) y
Cloud Formation Events for Stack: knifecfntest
Events for stack: knifecfntest
Time Resource Logical Id Resource Status Resource Status Reason
2015-01-06 08:25:10 UTC knifecfntest CREATE_IN_PROGRESS User Initiated
2015-01-06 08:25:22 UTC testDB CREATE_IN_PROGRESS
2015-01-06 08:25:22 UTC testEC2 CREATE_IN_PROGRESS
2015-01-06 08:25:23 UTC testEC2 CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:25:26 UTC testDB CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:26:11 UTC testEC2 CREATE_COMPLETE
2015-01-06 08:26:13 UTC testELB CREATE_IN_PROGRESS
2015-01-06 08:26:15 UTC testELB CREATE_IN_PROGRESS Resource creation Initiated
2015-01-06 08:26:16 UTC testELB CREATE_COMPLETE
2015-01-06 08:36:35 UTC testDB CREATE_COMPLETE
2015-01-06 08:36:37 UTC knifecfntest CREATE_COMPLETE
2015-01-06 15:38:08 UTC knifecfntest DELETE_IN_PROGRESS User Initiated
2015-01-06 15:38:16 UTC testELB DELETE_IN_PROGRESS
2015-01-06 15:38:16 UTC testDB DELETE_IN_PROGRESS
2015-01-06 15:38:16 UTC testELB DELETE_COMPLETE
2015-01-06 15:38:18 UTC testEC2 DELETE_IN_PROGRESS
2015-01-06 15:38:35 UTC testEC2 DELETE_COMPLETE
2015-01-06 15:43:34 UTC testDB DELETE_COMPLETE
2015-01-06 15:43:35 UTC knifecfntest DELETE_COMPLETE
-> Destroyed Cloud Formation: knifecfntest
DELETEコマンドが効いているのを確認できました。この後コマンド終了と共に削除されました。
まとめ
いかがでしたでしょうか。使ってみるとわかるのですが、CLIよりも直感的に、使いやすく感じるのは私がChefを愛しているからなのでしょうか。 これからもMAX共々Chefも愛してあげてください。よろしくお願い致します。