EC2: User Dataを使ってインスタンス起動時の処理を自動化する
インスタンス起動時に目にするUser Dataって何?って感じなので取り敢えず。
#!/bin/bash -ex yum -y install httpd php php-pear php-xml yum -y install git /etc/init.d/httpd start pear channel-discover pear.amazonwebservices.com pear install aws/sdk
といったように、「#!」(Hash-bang)から始まる文字列を User-data記述すると、Amazon LinuxのCloud-initによって、起動時に実行される。スクリプトを記述したファイルを指定することも可能。
実行された様子は、起動したインスタンスの /var/log/cloud-init.log に記述される。
User-dataは、16KB上限であれば、どんな文字列(JSON, プロパティ形式)でも大丈夫。
各インスタンスからは、 http://169.254.169.254/latest/user-data/ で取得することが可能。
インスタンスメタデータ http://169.254.169.254/latest/meta-data/ と組み合わせると、より高度なスクリプティングが可能になる。
CloudInit - Community Ubuntu Documentation
Using Amazon's CloudFormation, cloud-init, chef and fog to automate infrastructure | Allan Feid
Automating Application Installation Using AWS CloudFormation and Cloud-Init - AWS CloudFormation
このあたりも