funasaki memo

このブログ上の投稿は個人のものであり、所属する企業を代表する投稿ではありません。所属:AWSのSolutions Architect

Elastic Beanstalk Configuration Files を使ってみる

Elastic Beanstalk Configuration Files という機能が先月リリースされましたね。これは、Beanstalk によって起動されたインスタンスの内部をConfiguration file を使って、カスタマイズすることが出来る機能です。これによって、Beanstalkで動作しているインスタンスにソフトウェアやライブラリをインストールしたり、ファイルを作成・管理できたりします。
これで、ソフトウェアのバージョンアップやOSのバージョンアップのたびにAMIを作り直す面倒さから解放されますね!

で、早速使ってみました。

まずは、事前準備として

※今回はWindows の環境で実行しています。

msysGitは C:\Program Files (x86)\Gitにインストール。
Elastic Beanstalk CLIのzipをダウンロードしてC:\ec2フォルダ以下に展開。
gitコマンド、ebコマンドのあるフォルダをPATHに追加。

Beanstalkを展開した先のフォルダへ移動して、git init . を実行。

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git init .
Reinitialized existing Git repository in C:/ec2/AWS-ElasticBeanstalk-CLI-2.1/AWS
-ElasticBeanstalk-CLI-2.1/.git/

次にElasitc Beanstalk の init コマンドを実行

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>eb init
To get your AWS Access Key ID and Secret Access Key,
  visit "https://aws-portal.amazon.com/gp/aws/securityCredentials".
Enter your AWS Access Key ID (current value is "xxxxx*****xxxxxx"):
Enter your AWS Secret Access Key (current value is "xxxxx*****XXXXX"):
Select an AWS Elastic Beanstalk service region (current value is "Asia Pacific (
Tokyo)").
Available service regions are:
1) US East (Virginia)
2) US West (Oregon)
3) US West (North California)
4) EU West (Ireland)
5) Asia Pacific (Singapore)
6) Asia Pacific (Tokyo)
Select:  (1 to 6): 6
Enter an AWS Elastic Beanstalk application name (current value is "funaapp1"): fu
naapp1
Enter an AWS Elastic Beanstalk environment name (current value is "funaapp1-env")
: funaapp1-env
Select a solution stack (current value is "32bit Amazon Linux running Tomcat 7")
.
Available solution stacks are:
1) 32bit Amazon Linux running PHP 5.3
2) 64bit Amazon Linux running PHP 5.3
3) 64bit Windows Server 2008 R2 running IIS 7.5
4) 32bit Amazon Linux running Tomcat 7
5) 64bit Amazon Linux running Tomcat 7
6) 32bit Amazon Linux running Tomcat 6
7) 64bit Amazon Linux running Tomcat 6
8) 32bit Amazon Linux running Python
9) 64bit Amazon Linux running Python
10) 32bit Amazon Linux running Ruby 1.8.7
11) 64bit Amazon Linux running Ruby 1.8.7
12) 32bit Amazon Linux running Ruby 1.9.3
13) 64bit Amazon Linux running Ruby 1.9.3
Select:  (1 to 13):
Create an RDS DB Instance? [y/n] (current value is "No"): n

以上でApplication, Environmentの作成が完了。statusコマンドを実行してみると、

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>eb status
Environment "funaapp1-env" is not running.

当然not running となっている。

Beanstalk をスタートさせてみる。

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>eb start
Starting application "funaapp1".
Waiting for environment "funaapp1-env" to launch.
2012-11-03 18:34:02     INFO    createEnvironment is starting.
2012-11-03 18:34:07     INFO    Using elasticbeanstalk-ap-northeast-1-3588427332
04 as Amazon S3 storage bucket for environment data.
2012-11-03 18:34:40     INFO    Created load balancer named: awseb-e-m-AWSEBLoa-
ET3025GQJG18
2012-11-03 18:34:47     INFO    Created security group named: awseb-e-mpk3qfwqjd
-stack-AWSEBSecurityGroup-1ODUYI0BD4K35
2012-11-03 18:34:51     INFO    Created Auto Scaling launch configuration named:

途中省略

2012-11-03 18:39:29     INFO    Application available at funaapp1-env-rssd8rj2rf
.elasticbeanstalk.com.
Application is available at "funaapp1-env-rssd8rj2rf.elasticbeanstalk.com".

以上で起動は完了。
ここで、Beanstalkで起動されたインスタンスにログインできるように変更する。ログインするためにはkey-pairの設定が必要。
eb init コマンドでkey-pairの指定をすることがなかったので、Management Consoleでkey-pairの指定をする。
起動した環境がReadyの状態にならないと、設定を変更できない。
f:id:kenjifunasaki:20121103200812p:plain
f:id:kenjifunasaki:20121103200859p:plain
数分待って、以下のようにSuccessfully running version...と出れば、edit configurationが出来るようになる。
f:id:kenjifunasaki:20121103203033p:plain
注意事項として、ここのconfigurationは、今回のElastic Beanstalk Configuration Filesとは違う。ここでいうconfigurationは、Elastic BeanstalkのApplication, Environmentの設定のこと。ELBとか、使用するインスタンスのタイプを何にするか?などなどがコレに該当。
edit configrationすると、以下の画面が出てくる。
f:id:kenjifunasaki:20121103201235p:plain
ここでkey-pairを設定する。すると裏側では今動いているインスタンスが止まって、新たにインスタンスを作成、その起動時にkey-pairを設定される模様。

key-pairの設定後、Beanstalkで自動起動されたインスタンスに該当する秘密鍵ファイルを使ってログインする。今回は /home/ec2-user/ 以下にtest1.txtをConfiguration Fileを使って、自動作成させたいので、やってみる。
まずは、.ebextensions フォルダを Beanstalk CLI コマンドの展開先に作成。今回はC:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1直下に作成した。.ebextensions フォルダ内に .config ファイル(テキストファイル)を作成し、以下の内容を書き込む。

commands:
  001_touch:
    command: "touch /home/ec2-user/test1.txt"

次にgit add . コマンドを実行して、上記変更結果を stage する。

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git add .

その後でcommit。

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git commit -m "
commit"
[master ac86ec1] commit
 Committer: unknown <xxxx@xxx.xx.xx>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)

最後にaws.pushを実行。

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git aws.push
Counting objects: 363, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (355/355), done.
Writing objects: 100% (363/363), 4.89 MiB | 26 KiB/s, done.
Total 363 (delta 166), reused 0 (delta 0)
remote:
To https://XXXXXXXXXXXXXXXXXXX:20121103XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@git.elasticbeanstalk.ap-northeast-1.amazonaws.c
om/repos/66756e6161707032/funaapp2-env
 * [new branch]      HEAD -> master

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>

これで自動作成が実行される。数分後インスタンスにログインして見てみると、

[ec2-user@ip-10-146-33-219 ~]$ pwd
/home/ec2-user
[ec2-user@ip-10-146-33-219 ~]$ ls -lh
total 0
-rw-rw-rw- 1 root root 0 Nov  3 10:43 test1.txt

test1.txtが無事できてました!

次にConfiguration Fileで、packageインストールしてみます。

Beanstalkで起動されたインスタンスで、試しにhttpdのインストール状況を確認してみると

[ec2-user@ip-10-146-33-219 elasticbeanstalk]$ sudo yum list httpd
Loaded plugins: fastestmirror, priorities, security, update-motd
Loading mirror speeds from cached hostfile
 * amzn-main: packages.ap-northeast-1.amazonaws.com
 * amzn-updates: packages.ap-northeast-1.amazonaws.com
Installed Packages
httpd.i686                    2.2.22-1.23.amzn1                     @amzn-main
Available Packages
httpd.i686                    2.2.23-1.25.amzn1                     amzn-updates
<||
httpdの2.2.22がインストールされていて、2.2.23がインストールされていない。
そこで、今回は2.2.23をインストールさせてみる。.ebextensions/.configファイルを以下のように編集する。
packages:
  yum:
    httpd: [2.2.23]

commands:
  001_touch:
    command: "touch /home/ec2-user/test1.txt"
編集完了後、前回同様に下記コマンドを実行。
>|sh|
C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git add .

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git commit -m "
httpd"

C:\ec2\AWS-ElasticBeanstalk-CLI-2.1\AWS-ElasticBeanstalk-CLI-2.1>git aws.push

コマンド実行数分後、インスタンスでインストール状況を確認すると、

[ec2-user@ip-10-146-33-219 elasticbeanstalk]$ sudo yum list httpd
Loaded plugins: fastestmirror, priorities, security, update-motd
Loading mirror speeds from cached hostfile
 * amzn-main: packages.ap-northeast-1.amazonaws.com
 * amzn-updates: packages.ap-northeast-1.amazonaws.com
Installed Packages
httpd.i686                    2.2.23-1.25.amzn1                    @amzn-update

httpd の 2.2.23がインストールされてました。以上!めでたしめでたし。