.ebextensionsによる初期化処理
Elastic Beanstalkでは、アプリケーションディレクトリの直下の.ebextensions
ディレクトリに.config
で終わるファイルを用意しておくと、アプリケーションをpushした際に良きに計らっていろんなことをしてくれる。
configファイルはYAMLで記述する。詳細はこちらを参照すると良い。
なお、処理の順番は、以下の通りで、packages〜servicesまでは、Webサーバの準備が終わっていて、まだアプリが抽出されていない時点で実施され、container_commandsは、Webサーバの準備が終わり、かつアプリの抽出が終わっていてデプロイされる前に実行される
- packages
- files
- commands
- services
- container_commands
sudoできるようにする
ということでアプリからコマンドをキックしたりしたい場合にsudoの設定を変えたい場合はよくある。Elastic BeanstalkではカスタムAMIが利用できるので、予め自分でAMIを作っておいてもよいが、初期化処理の中でやってしまうのが保守性を含めて簡単である。
以下がその例。まずsudoするときにttyを必要としてしまうとアプリ内からキックできないのでこいつを無効にする。その上で、Elastic Beanstalkのアプリ用ユーザーのwebappに対してパスワードなしでsudoできるように設定している
commands:
01_sudo:
command: "sed -i 's/^.*requiretty/#Defaults requiretty/' /etc/sudoers"
02_sudo_webapp:
command: "grep -q 'webapp ALL=(ALL) NOPASSWD: ALL' /etc/sudoers || echo -e 'webapp ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
これで色々自由になるはず。
もっと自由にしたかったら?
ドキュメントに書いていないが、hookを使うと、適用のタイミングを含めてもっと自由に使える。