http://jawsug-cli.doorkeeper.jp/events/19360 でのハンズオン資料です。
AWS CLIを利用して、ElasticBeanstalkでアプリケーションを更新してみます。
前提条件
IAMへの権限
- ElasticBeanstalkに対してフル権限があること。
{
"GroupName": "beanstalk_full",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"elasticbeanstalk:*",
"ec2:*",
"elasticloadbalancing:*",
"autoscaling:*",
"cloudwatch:*",
"s3:*",
"sns:*",
"cloudformation:*",
"rds:*",
"sqs:*",
"iam:CreateRole",
"iam:CreateInstanceProfile",
"iam:GetUser",
"iam:ListInstanceProfiles",
"iam:ListRoles",
"iam:PassRole"
],
"Resource": "*",
"Effect": "Allow"
}
]
},
"PolicyName": "beanstalk_full"
}
AWS CLIのバージョン
-
以下のバージョンで動作確認済
- AWS CLI 1.7.12
- 準備
=======
変数の確認
cat << ETX
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}
AWS_DEFAULT_PROFILE: ${AWS_DEFAULT_PROFILE}
EB_APP_NAME: "${EB_APP_NAME}"
EB_ENV_NAME: "${EB_ENV_NAME}"
EB_ENV_ID: "${EB_ENV_ID}"
S3_BUCKET_NAME: ${S3_BUCKET_NAME}
ETX
EB_APP_NAME
EB_APP_NAME='My First Elastic Beanstalk Application'
EB_ENV_NAME
EB_ENV_NAME='Default-Environment'
EB_ENV_ID=`aws elasticbeanstalk describe-environments --application-name "${EB_APP_NAME}" --environment-names "${EB_ENV_NAME}" --query 'Environments[].EnvironmentId' --output text` \
&& echo ${EB_ENV_ID}
e-xxxxxxxxxx
ノート: Environmentは、Environment IDとEnvironment Nameの2つで特定で
きますが 、ここではなるべくEnvironment IDを利用するようにします。
S3_BUCKET_NAME
AWS_ID=`aws iam get-user --query 'User.Arn' --output text | sed 's/^.*:://' | sed 's/:.*$//'` \
&& echo ${AWS_ID}
S3_BUCKET_NAME="elasticbeanstalk-${AWS_DEFAULT_REGION}-${AWS_ID}"
echo ${S3_BUCKET_NAME}
- 新バージョンの作成
=====================
1.1. 新しいversion名の決定
EB_VER_LABEL='Sample Application Second Version'
1.2. 新versionのソースコード準備
SRC_EB_VER='https://elasticbeanstalk-samples-us-east-1.s3.amazonaws.com/php-secondsample.zip'
FILE_EB_VER=`echo ${SRC_EB_VER} | sed 's/^.*\///'` \
&& echo ${FILE_EB_VER}
curl ${SRC_EB_VER} > ${FILE_EB_VER}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2539 100 2539 0 0 1965 0 0:00:01 0:00:01 --:--:-- 2959
1.3. ソースコードのS3バケットへの転送
S3_OBJECT_NAME="${FILE_EB_VER}"
aws s3 cp ${FILE_EB_VER} s3://${S3_BUCKET_NAME}/${S3_OBJECT_NAME}
upload: ./php-secondsample.zip to s3://elasticbeanstalk-us-west-2-XXXXXXXXXXXX/php-secondsample.zip
1.4. 新versionの作成
-[ バージョンの存在確認 (describe-application-versions) ]-
aws elasticbeanstalk describe-application-versions \
--application-name "${EB_APP_NAME}" \
--version-labels "${EB_VER_LABEL}"
{
"ApplicationVersions": []
}
-[ 新versionの作成 (create-application-version) ]-
cat << ETX
EB_APP_NAME: "${EB_APP_NAME}"
EB_VER_LABEL: "${EB_VER_LABEL}"
S3_BUCKET_NAME: ${S3_BUCKET_NAME}
S3_OBJECT_NAME: ${S3_OBJECT_NAME}
ETX
aws elasticbeanstalk create-application-version \
--application-name "${EB_APP_NAME}" \
--version-label "${EB_VER_LABEL}" \
--source-bundle "S3Bucket=${S3_BUCKET_NAME},S3Key=${S3_OBJECT_NAME}"
{
"ApplicationVersion": {
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"SourceBundle": {
"S3Bucket": "elasticbeanstalk-us-west-2-XXXXXXXXXXXX",
"S3Key": "php-secondsample.zip"
},
"DateUpdated": "2015-03-02T07:32:28.238Z",
"DateCreated": "2015-03-02T07:32:28.238Z"
}
}
-[ バージョンの存在確認 (describe-application-versions) ]-
aws elasticbeanstalk describe-application-versions \
--application-name "${EB_APP_NAME}" \
--version-labels "${EB_VER_LABEL}"
{
"ApplicationVersion": {
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"SourceBundle": {
"S3Bucket": "elasticbeanstalk-us-west-2-XXXXXXXXXXXX",
"S3Key": "php-secondsample.zip"
},
"DateUpdated": "2015-03-02T07:32:28.238Z",
"DateCreated": "2015-03-02T07:32:28.238Z"
}
}
-[ イベント確認 (describe-events) ]-
EB_MAX_ITEMS='3'
aws elasticbeanstalk describe-events \
--application-name "${EB_APP_NAME}" \
--max-items ${EB_MAX_ITEMS}
{
"NextToken": "None___3",
"Events": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Severity": "INFO",
"RequestId": "47471280-c0ae-11e4-923c-6bf74ef94e2e",
"Message": "createApplicationVersion completed successfully.",
"EventDate": "2015-03-02T07:32:28.316Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Severity": "INFO",
"RequestId": "47471280-c0ae-11e4-923c-6bf74ef94e2e",
"Message": "Created new Application Version: Sample Application Second Version",
"EventDate": "2015-03-02T07:32:28.239Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Severity": "INFO",
"RequestId": "47471280-c0ae-11e4-923c-6bf74ef94e2e",
"Message": "createApplicationVersion is starting.",
"EventDate": "2015-03-02T07:32:28.196Z"
}
]
}
- 新バージョンのリリース (Environmentの更新)
=============================================
2.1. 新バージョンのリリース
-[ Environmentの内容確認 (describe-environments) ]-
aws elasticbeanstalk describe-environments \
--application-name "${EB_APP_NAME}" \
--environment-names "${EB_ENV_NAME}"
{
"Environments": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"VersionLabel": "Sample Application",
"Status": "Ready",
"EnvironmentId": "e-pm2m8tsyuc",
"EndpointURL": "awseb-e-p-AWSEBLoa-1JIB95ZPILT5G-1170500997.us-west-2.elb.amazonaws.com",
"SolutionStackName": "64bit Amazon Linux 2014.09 v1.2.0 running PHP 5.5",
"CNAME": "Default-Environment-dy6a8ifwuv.elasticbeanstalk.com",
"Health": "Green",
"Tier": {
"Version": " ",
"Type": "Standard",
"Name": "WebServer"
},
"DateUpdated": "2015-03-02T07:19:13.016Z",
"DateCreated": "2015-03-02T07:15:58.547Z"
}
]
}
-[ Environmentの更新 (update-environment) ]-
cat << ETX
EB_ENV_ID: ${EB_ENV_ID}
EB_VER_LABEL: "${EB_VER_LABEL}"
ETX
aws elasticbeanstalk update-environment \
--environment-id ${EB_ENV_ID} \
--version-label "${EB_VER_LABEL}"
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"VersionLabel": "Sample Application Second Version",
"Status": "Updating",
"EnvironmentId": "e-pm2m8tsyuc",
"EndpointURL": "awseb-e-p-AWSEBLoa-1JIB95ZPILT5G-1170500997.us-west-2.elb.amazonaws.com",
"SolutionStackName": "64bit Amazon Linux 2014.09 v1.2.0 running PHP 5.5",
"CNAME": "Default-Environment-dy6a8ifwuv.elasticbeanstalk.com",
"Health": "Grey",
"Tier": {
"Version": " ",
"Type": "Standard",
"Name": "WebServer"
},
"DateUpdated": "2015-03-02T07:38:44.798Z",
"DateCreated": "2015-03-02T07:15:58.547Z"
}
-[ Environmentのステータス確認 (describe-environments) ]-
EB_ENV_STATUS=`aws elasticbeanstalk describe-environments --application-name "${EB_APP_NAME}" --environment-ids "${EB_ENV_ID}" --query 'Environments[].Status' --output text` \
&& echo ${EB_ENV_STATUS}
Ready
2.2. 新バージョンの確認
-[ Environmentの内容確認 (describe-environments) ]-
aws elasticbeanstalk describe-environments \
--application-name "${EB_APP_NAME}" \
--environment-names "${EB_ENV_NAME}"
{
"Environments": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"VersionLabel": "Sample Application Second Version",
"Status": "Ready",
"EnvironmentId": "e-pm2m8tsyuc",
"EndpointURL": "awseb-e-p-AWSEBLoa-1JIB95ZPILT5G-1170500997.us-west-2.elb.amazonaws.com",
"SolutionStackName": "64bit Amazon Linux 2014.09 v1.2.0 running PHP 5.5",
"CNAME": "Default-Environment-dy6a8ifwuv.elasticbeanstalk.com",
"Health": "Green",
"Tier": {
"Version": " ",
"Type": "Standard",
"Name": "WebServer"
},
"DateUpdated": "2015-03-02T07:39:23.031Z",
"DateCreated": "2015-03-02T07:15:58.547Z"
}
]
}
-[ イベント確認 (describe-events) ]-
EB_MAX_ITEMS='4'
aws elasticbeanstalk describe-events \
--application-name "${EB_APP_NAME}" \
--environment-name ${EB_ENV_NAME} \
--max-items ${EB_MAX_ITEMS}
{
"NextToken": "None___4",
"Events": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"Severity": "INFO",
"RequestId": "27a71ad0-c0af-11e4-b0ba-53a9554007d2",
"Message": "Environment update completed successfully.",
"EventDate": "2015-03-02T07:39:22.880Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"Severity": "INFO",
"RequestId": "27a71ad0-c0af-11e4-b0ba-53a9554007d2",
"Message": "New application version was deployed to running EC2 instances.",
"EventDate": "2015-03-02T07:39:22.835Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"Severity": "INFO",
"RequestId": "27a71ad0-c0af-11e4-b0ba-53a9554007d2",
"Message": "Deploying new version to instance(s).",
"EventDate": "2015-03-02T07:39:10.447Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"EnvironmentName": "Default-Environment",
"Severity": "INFO",
"RequestId": "27a71ad0-c0af-11e4-b0ba-53a9554007d2",
"Message": "Environment update is starting.",
"EventDate": "2015-03-02T07:38:44.748Z"
}
]
}
-[ URLの取得 ]-
EB_CNAME_URL=`aws elasticbeanstalk describe-environments --application-name "${EB_APP_NAME}" --environment-names "${EB_ENV_NAME}" --query 'Environments[].CNAME' --output text` \
&& echo ${EB_CNAME_URL}
Default-Environment-xxxxxxxxxxxx.elasticbeanstalk.com
-[ アクセス確認 ]-
ブラウザでアクセスして、アプリケーションが表示されることを確認しましょ
う。
ノート: キャッシュによって旧コンテンツが見えている場合があります。
その場合 は、キャッシュをクリアしてみてください。
- version更新
==============
versionのdescriptionを更新します。
-[ versionの内容確認 (describe-application-versions) ]-
aws elasticbeanstalk describe-application-versions \
--application-name "${EB_APP_NAME}" \
--version-labels "${EB_VER_LABEL}"
{
"ApplicationVersions": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"SourceBundle": {
"S3Bucket": "elasticbeanstalk-us-west-2-XXXXXXXXXXXX",
"S3Key": "php-secondsample.zip"
},
"DateUpdated": "2015-03-02T07:32:28.238Z",
"DateCreated": "2015-03-02T07:32:28.238Z"
}
]
}
-[ versionの更新 (update-application-version) ]-
EB_VER_DESC='hands-on version'
cat << ETX
EB_APP_NAME: "${EB_APP_NAME}"
EB_VER_LABEL: "${EB_VER_LABEL}"
EB_VER_DESC: "${EB_VER_DESC}"
ETX
aws elasticbeanstalk update-application-version \
--application-name "${EB_APP_NAME}" \
--version-label "${EB_VER_LABEL}" \
--description "${EB_VER_DESC}"
{
"ApplicationVersion": {
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Description": "hands-on version",
"DateCreated": "2015-03-02T07:32:28.238Z",
"DateUpdated": "2015-03-02T07:45:58.767Z",
"SourceBundle": {
"S3Bucket": "elasticbeanstalk-us-west-2-XXXXXXXXXXXX",
"S3Key": "php-secondsample.zip"
}
}
}
-[ versionの内容確認 (describe-application-versions) ]-
aws elasticbeanstalk describe-application-versions \
--application-name "${EB_APP_NAME}" \
--version-labels "${EB_VER_LABEL}"
{
"ApplicationVersions": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Description": "hands-on version",
"DateCreated": "2015-03-02T07:32:28.238Z",
"DateUpdated": "2015-03-02T07:45:58.767Z",
"SourceBundle": {
"S3Bucket": "elasticbeanstalk-us-west-2-XXXXXXXXXXXX",
"S3Key": "php-secondsample.zip"
}
}
]
}
-[ イベント確認 (describe-events) ]-
EB_MAX_ITEMS='2'
aws elasticbeanstalk describe-events \
--application-name "${EB_APP_NAME}" \
--max-items ${EB_MAX_ITEMS}
{
"NextToken": "None___2",
"Events": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Severity": "INFO",
"RequestId": "2a66bab1-c0b0-11e4-99d0-e3965e27cbd3",
"Message": "updateApplicationVersion completed successfully.",
"EventDate": "2015-03-02T07:45:58.828Z"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"VersionLabel": "Sample Application Second Version",
"Severity": "INFO",
"RequestId": "2a66bab1-c0b0-11e4-99d0-e3965e27cbd3",
"Message": "updateApplicationVersion is starting.",
"EventDate": "2015-03-02T07:45:58.722Z"
}
]
}
- アプリケーションの更新
=========================
アプリケーションのdescriptionを更新します。
-[ アプリケーションの内容確認 (describe-applications) ]-
aws elasticbeanstalk describe-applications \
--application-name "${EB_APP_NAME}"
{
"Applications": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"ConfigurationTemplates": [],
"DateUpdated": "2015-03-02T07:08:07.952Z",
"Versions": [
"Sample Application Second Version",
"Sample Application"
],
"DateCreated": "2015-03-02T07:08:07.952Z"
}
]
}
EB_APP_DESC='hands-on app'
-[ アプリケーションのDescription更新 (update-application) ]-
cat << ETX
EB_APP_NAME: "${EB_APP_NAME}"
EB_APP_DESC: "${EB_APP_DESC}"
ETX
aws elasticbeanstalk update-application \
--application-name "${EB_APP_NAME}" \
--description "${EB_APP_DESC}"
{
"Application": {
"ApplicationName": "My First Elastic Beanstalk Application",
"Description": "hands-on app",
"Versions": [
"Sample Application Second Version",
"Sample Application"
],
"DateCreated": "2015-03-02T07:08:07.952Z",
"ConfigurationTemplates": [],
"DateUpdated": "2015-03-02T07:50:35.104Z"
}
}
-[ アプリケーションの内容確認 (describe-applications) ]-
aws elasticbeanstalk describe-applications \
--application-name "${EB_APP_NAME}"
{
"Applications": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"Description": "hands-on app",
"Versions": [
"Sample Application Second Version",
"Sample Application"
],
"DateCreated": "2015-03-02T07:08:07.952Z",
"ConfigurationTemplates": [],
"DateUpdated": "2015-03-02T07:50:35.104Z"
}
]
}
-[ イベント確認 (describe-events) ]-
EB_MAX_ITEMS='2'
aws elasticbeanstalk describe-events \
--application-name "${EB_APP_NAME}" \
--max-items ${EB_MAX_ITEMS}
{
"NextToken": "None___2",
"Events": [
{
"ApplicationName": "My First Elastic Beanstalk Application",
"Message": "updateApplication completed successfully.",
"Severity": "INFO",
"EventDate": "2015-03-02T07:50:35.278Z",
"RequestId": "cf2605c8-c0b0-11e4-923c-6bf74ef94e2e"
},
{
"ApplicationName": "My First Elastic Beanstalk Application",
"Message": "updateApplication is starting.",
"Severity": "INFO",
"EventDate": "2015-03-02T07:50:35.061Z",
"RequestId": "cf2605c8-c0b0-11e4-923c-6bf74ef94e2e"
}
]
}
完了
次に、アプリケーションの廃止と後始末をしてみましょう。