4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JAWS-UG CLI] ElasticBeanstalk #2 ElasticBeanstalk入門 (更新リリース)

Last updated at Posted at 2015-03-02

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
  1. 準備
    =======

変数の確認

変数の確認
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.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"
          }
        ]
      }
  1. 新バージョンのリリース (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

-[ アクセス確認 ]-

ブラウザでアクセスして、アプリケーションが表示されることを確認しましょ
う。

ノート: キャッシュによって旧コンテンツが見えている場合があります。
その場合 は、キャッシュをクリアしてみてください。

  1. 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"
          }
        ]
      }
  1. アプリケーションの更新
    =========================

アプリケーションの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"
          }
        ]
      }

完了

次に、アプリケーションの廃止と後始末をしてみましょう。

4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?