LoginSignup
6
4

More than 5 years have passed since last update.

Elastic Beanstalkでサンプルアプリケーションを動かしてみる

Posted at

AWSのElastic Beanstalkを試す機会があったため、ほぼマニュアル通りなものの、サンプルアプリケーションが動くところまでやってみたので、その時のメモ。

EB CLIのインストール

AWSをCLIで使うときはよくawscliを使うが、Beanstalkを使うときはawsebcliを使うらしい。
ということでインストールする。

$ sudo pip install awsebcli

EB CLIプロジェクトの初期化

プロジェクトディレクトリを作成する。
尚、AWSのプロファイルを個別に作成している場合は、AWS CLIを使うときは最初にプロファイルを指定するか、コマンド実行時にプロファイルを指定する必要がある。

$ export AWS_EB_PROFILE=default
$ mkdir eb-sample-app
$ cd eb-sample-app

作成したら、eb initを実行。
リージョンは東京、プラットフォームは今回はPythonを選択した。
また、内部で作成されるEC2インスタンスにSSHログイン出来るようにするかどうか尋ねられるが、今回はログイン可能なようにした。

$ eb init

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-southeast-1 : Asia Pacific (Singapore)
7) ap-southeast-2 : Asia Pacific (Sydney)
8) ap-northeast-1 : Asia Pacific (Tokyo)
9) sa-east-1 : South America (Sao Paulo)
10) cn-north-1 : China (Beijing)
(default is 3): 8

Enter Application Name
(default is "eb-sample-app"):
Application eb-sample-app has been created.

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
11) Java
(default is 1): 3

Select a platform version.
1) Python 3.4
2) Python
3) Python 2.7
4) Python 3.4 (Preconfigured - Docker)
(default is 1): 3
Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
1) mykey
2) [ Create new KeyPair ]
(default is 2): 1

プロジェクトディレクトリの特定のファイルを無視する

.ebignoreファイルに、アプリケーションに含めたくないファイルを書いておく。
.ebignoreがなく.gitignoreがある場合は、.gitignoreを読みに行くようなので、そちらに書いておいても良い。
eb initした直後の状態では、以下のようになっていた。

$ cat .gitignore

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

環境の作成

続いてBeanstalkの環境を作成する。
プロジェクトディレクトリ以下にアプリケーションのソースコードがあればそれがデプロイされるが、それ以外の場合はサンプルアプリケーションが使用される。
環境の作成には時間がかかるので、作成中にコマンドラインに戻りたい場合はCtrl+Cを入力する。

$ eb create
Enter Environment Name
(default is eb-sample-app-dev):
Enter DNS CNAME prefix
(default is eb-sample-app-dev):
WARNING: The current directory does not contain any source code. Elastic Beanstalk is launching the sample application instead.
Environment details for: eb-sample-app-dev
  Application name: eb-sample-app
  Region: ap-northeast-1
  Deployed Version: Sample Application
  Environment ID: e-xxxxxxxxxx
  Platform: 64bit Amazon Linux 2015.09 v2.0.4 running Python 2.7
  Tier: WebServer-Standard
  CNAME: eb-sample-app-dev.elasticbeanstalk.com
  Updated: 2015-11-xx 15:07:37.735000+00:00
Printing Status:
INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-ap-northeast-1-xxxxxxxxxxx as Amazon S3 storage bucket for environment data.
INFO: Environment health has transitioned to Pending. There are no instances.
INFO: Created security group named: sg-xxxxxxxx
INFO: Created load balancer named: awseb-e-7-AWSEBLoa-xxxxxxxxxxx
INFO: Created security group named: awseb-e-xxxxxxxxxxx-stack-AWSEBSecurityGroup-xxxxxxxxxxx
INFO: Created Auto Scaling launch configuration named: awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingLaunchConfiguration-xxxxxxxxxxx
INFO: Created Auto Scaling group named: awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingGroup-xxxxxxxxxxx
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1:xxxxxxxxxxx:scalingPolicy: xxxxxxxxxxxxxxxxxxxxxx:autoScalingGroupName/awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingGroup-xxxxxxxxxxx:policyName/awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingScaleDownPolicy-xxxxxxxxxxx
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1: xxxxxxxxxxx:scalingPolicy: xxxxxxxxxxxxxxxxxxxxxx:autoScalingGroupName/awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingGroup-xxxxxxxxxxx:policyName/awseb-e-xxxxxxxxxxx-stack-AWSEBAutoScalingScaleUpPolicy-xxxxxxxxxxx
INFO: Added instance [i-xxxxxxxx] to your environment.
INFO: Environment health has transitioned from Pending to Ok.
INFO: Successfully launched environment: eb-sample-app-dev

作成した環境の確認

ステータスを確認する。StatusがReadyなので、環境の準備が整ったことがわかる。
尚、Beanstalkのログを見るにはeb eventsコマンドを利用する。

$ eb status
Environment details for: eb-sample-app-dev
  Application name: eb-sample-app
  Region: ap-northeast-1
  Deployed Version: Sample Application
  Environment ID: e-xxxxxxxxxxx
  Platform: 64bit Amazon Linux 2015.09 v2.0.4 running Python 2.7
  Tier: WebServer-Standard
  CNAME: eb-sample-app-dev.elasticbeanstalk.com
  Updated: 2015-11-xx 15:12:02.790000+00:00
  Status: Ready
  Health: Green

Beanstalk内のEC2のログを見るにはeb logsコマンドを利用する。複数台あるときは--instanceでインスタンスIDを指定するか、--allで全てのEC2のログを確認する。
ここで確認できるログは以下のとおり。

  • /var/log/eb-version-deployment.log
  • /var/log/httpd/error_log
  • /opt/python/log/supervisord.log
  • /var/log/httpd/access_log
  • /var/log/eb-activity.log
  • /opt/python/log/sample-app.log
  • /var/log/eb-commandprocessor.log

例えばこんな内容が確認できる。

$ eb logs
============= i-xxxxxxxx ==============
-------------------------------------
/var/log/eb-version-deployment.log
-------------------------------------
2015-11-xx 15:10:20,051 [INFO] Found the latest version manifest file <Key: elasticbeanstalk-ap-northeast-1-xxxxxxxxxxx,resources/environments/e-xxxxxxxxxxx/_runtime/versions/manifest_ xxxxxxxxxxx> from bucket elasticbeanstalk-ap-northeast-1-xxxxxxxxxxx and prefix resources/environments/e-xxxxxxxxxxx/_runtime/versions/
(snip)

環境に利用可能なオプションは、eb configで確認できる。
試してみたところ、viとかではなくいきなりnanoエディタで起動してきたので注意。

$ eb config
ApplicationName: eb-sample-app
DateUpdated: 2015-11-xx 15:07:36+00:00
EnvironmentName: eb-sample-app-dev
SolutionStackName: 64bit Amazon Linux 2015.09 v2.0.4 running Python 2.7
settings:
  AWSEBAutoScalingScaleDownPolicy.aws:autoscaling:trigger:
    LowerBreachScaleIncrement: '-1'
  AWSEBAutoScalingScaleUpPolicy.aws:autoscaling:trigger:
    UpperBreachScaleIncrement: '1'
  AWSEBCloudwatchAlarmHigh.aws:autoscaling:trigger:
    UpperThreshold: '6000000'
  AWSEBCloudwatchAlarmLow.aws:autoscaling:trigger:
    BreachDuration: '5'
    EvaluationPeriods: '1'
    LowerThreshold: '2000000'
    MeasureName: NetworkOut
    Period: '5'
    Statistic: Average
    Unit: Bytes
(snip)

デプロイしてみる

サンプルアプリをダウンロードしてきて、Beanstalkにデプロイしてみる。

$ eb deploy
Creating application version archive "app-151118_003504".
Uploading eb-sample-app/app-151118_003504.zip to S3. This may take a while.
Upload Complete.
INFO: Environment update is starting.
INFO: Deploying new version to instance(s).
INFO: Deleted log fragments for this environment.
INFO: New application version was deployed to running EC2 instances.
INFO: Environment update completed successfully.

ステータスを確認する。Deployed Versionが変わっていることが確認できる。

$ eb status
Environment details for: eb-sample-app-dev
  Application name: eb-sample-app
  Region: ap-northeast-1
  Deployed Version: Sample Application
  Environment ID: e-xxxxxxxxxxx
  Platform: 64bit Amazon Linux 2015.09 v2.0.4 running Python 2.7
  Tier: WebServer-Standard
  CNAME: eb-sample-app-dev.elasticbeanstalk.com
  Updated: 2015-11-xx 15:12:02.790000+00:00
  Status: Ready
  Health: Green

動作確認

以下のコマンドで、ブラウザを起動して作成したアプリケーションへアクセスする。

$ eb open

こんな画面が表示されればOK。

Kobito.MF0N7x.png

とりあえずアプリケーションをデプロイするところまでが確認出来た。

EC2にログインしてみる

設定変更などはEC2にログインして行うのではなく、Beanstalkの仕組みを用いて行うため、通常はEC2にログインしないが、中の動きを見るためにログインしてみる。

motdの内容が、いつものEC2とは異なっている…

 _____ _           _   _      ____                       _        _ _
| ____| | __ _ ___| |_(_) ___| __ )  ___  __ _ _ __  ___| |_ __ _| | | __
|  _| | |/ _` / __| __| |/ __|  _ \ / _ \/ _` | '_ \/ __| __/ _` | | |/ /
| |___| | (_| \__ \ |_| | (__| |_) |  __/ (_| | | | \__ \ || (_| | |   <
|_____|_|\__,_|___/\__|_|\___|____/ \___|\__,_|_| |_|___/\__\__,_|_|_|\_\
                                       Amazon Linux AMI

This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH
WILL BE LOST if the instance is replaced by auto-scaling. For more information
on customizing your Elastic Beanstalk environment, see our documentation here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

ぱっと見たところ、このあたりのプロセスが動いていることが確認できた。

$ ps auxfww | grep -e httpd -e dhclient -e sendmail -e pytohn -e sshd -e ntpd -e crond
root      1992  0.0  0.1   9336  1248 ?        Ss   15:09   0:00 /sbin/dhclient -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0
root      2231  0.0  0.2  77780  2572 ?        Ss   15:09   0:00 /usr/sbin/sshd
root      3574  0.0  0.6 115656  6700 ?        Ss   15:43   0:00  \_ sshd: ec2-user [priv]
ec2-user  3576  0.0  0.3 115656  3680 ?        S    15:43   0:00      \_ sshd: ec2-user@pts/0
ec2-user  3668  0.0  0.2 110476  2180 pts/0    D+   15:47   0:00              \_ grep --color=auto -e httpd -e dhclient -e sendmail -e pytohn -e sshd -e ntpd -e crond
ntp       2256  0.0  0.4  29260  4264 ?        Ss   15:10   0:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root      2271  0.0  0.3  88924  4004 ?        Ss   15:10   0:00 sendmail: accepting connections
smmsp     2279  0.0  0.3  80384  3776 ?        Ss   15:10   0:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
root      2287  0.0  0.2 119524  2436 ?        Ss   15:10   0:00 crond
root      3395  0.0  0.8 215772  8880 ?        S    15:35   0:00  \_ /usr/sbin/httpd -DFOREGROUND
root      3397  0.0  0.2  27308  2244 ?        S    15:35   0:00      \_ /usr/sbin/rotatelogs /var/log/httpd/healthd/application.log.%Y-%m-%d-%H 3600
apache    3399  0.0  0.6 215908  6732 ?        S    15:35   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3400  0.0  0.6 215772  6732 ?        S    15:35   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3401  0.0  0.6 215772  6732 ?        S    15:35   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3402  0.0  0.6 215772  6732 ?        S    15:35   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3403  0.0  0.6 215772  6732 ?        S    15:35   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3461  0.0  0.6 215908  6732 ?        S    15:36   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3462  0.0  0.6 215772  6732 ?        S    15:36   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3463  0.0  0.6 215772  6732 ?        S    15:36   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3533  0.0  0.6 215772  6732 ?        S    15:40   0:00      \_ /usr/sbin/httpd -DFOREGROUND
apache    3535  0.0  0.6 215772  6732 ?        S    15:40   0:00      \_ /usr/sbin/httpd -DFOREGROUND

まとめ

とりあえず、コマンド数個で簡単にELB・EC2などのコンポーネントの作成が出来、コマンド一つでデプロイまで出来るのは便利。
ただしここから細かくEC2内の設定などを変えていく場合は、.ebextensionsなどの方法で行う必要があるため、今度はもう少しそのあたりを試してみる。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4