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

http://jawsug-cli.doorkeeper.jp/events/19360 でのハンズオン資料です。

AWS CLIを利用して、ElasticBeanstalkでアプリケーションを更新してみます。


前提条件
========


IAMへの権限
-----------

* ElasticBeanstalkに対してフル権限があること。

```json:ポリシードキュメント
{
    "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




0. 準備
=======


変数の確認
----------

```bash:変数の確認:
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
-----------

```bash:コマンド:
EB_APP_NAME='My First Elastic Beanstalk Application'
```


EB_ENV_NAME
-----------

```bash:コマンド:
EB_ENV_NAME='Default-Environment'
```

```bash:コマンド:
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
--------------

```bash:コマンド:
AWS_ID=`aws iam get-user --query 'User.Arn' --output text | sed 's/^.*:://' | sed 's/:.*$//'` \
        && echo ${AWS_ID}
```

```bash:コマンド:
S3_BUCKET_NAME="elasticbeanstalk-${AWS_DEFAULT_REGION}-${AWS_ID}"
      echo ${S3_BUCKET_NAME}
```


1. 新バージョンの作成
=====================


1.1. 新しいversion名の決定
--------------------------

```bash:コマンド:
EB_VER_LABEL='Sample Application Second Version'
```


1.2. 新versionのソースコード準備
--------------------------------

```bash:コマンド(更新):
SRC_EB_VER='https://elasticbeanstalk-samples-us-east-1.s3.amazonaws.com/php-secondsample.zip'
```

```bash:コマンド:
FILE_EB_VER=`echo ${SRC_EB_VER} | sed 's/^.*\///'` \
        && echo ${FILE_EB_VER}
```

```bash:コマンド:
curl ${SRC_EB_VER} > ${FILE_EB_VER}
```

```text:結果(例):
        % 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バケットへの転送
-------------------------------------

```bash:コマンド:
S3_OBJECT_NAME="${FILE_EB_VER}"
```

```bash:コマンド:
aws s3 cp ${FILE_EB_VER} s3://${S3_BUCKET_NAME}/${S3_OBJECT_NAME}
```

```text:結果(例):
      upload: ./php-secondsample.zip to s3://elasticbeanstalk-us-west-2-XXXXXXXXXXXX/php-secondsample.zip
```


1.4. 新versionの作成
--------------------

-[ バージョンの存在確認 (describe-application-versions) ]-

```bash:コマンド:
aws elasticbeanstalk describe-application-versions \
        --application-name "${EB_APP_NAME}" \
        --version-labels "${EB_VER_LABEL}"
```

```json:結果:
      {
          "ApplicationVersions": []
      }
```

-[ 新versionの作成 (create-application-version) ]-

```bash:変数の確認:
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
```

```bash:コマンド:
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}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
aws elasticbeanstalk describe-application-versions \
        --application-name "${EB_APP_NAME}" \
        --version-labels "${EB_VER_LABEL}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
EB_MAX_ITEMS='3'
```

```bash:コマンド:
aws elasticbeanstalk describe-events \
        --application-name "${EB_APP_NAME}" \
        --max-items ${EB_MAX_ITEMS}
```

```json:結果(例):
      {
        "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"
          }
        ]
      }
```


2. 新バージョンのリリース (Environmentの更新)
=============================================


2.1.  新バージョンのリリース
----------------------------

-[ Environmentの内容確認 (describe-environments) ]-

```bash:コマンド:
aws elasticbeanstalk describe-environments \
        --application-name "${EB_APP_NAME}" \
        --environment-names "${EB_ENV_NAME}"
```

```json:結果(例):
      {
        "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) ]-

```bash:変数の確認:
cat << ETX

        EB_ENV_ID:     ${EB_ENV_ID}
        EB_VER_LABEL: "${EB_VER_LABEL}"

ETX
```

```bash:コマンド:
aws elasticbeanstalk update-environment \
        --environment-id ${EB_ENV_ID} \
        --version-label "${EB_VER_LABEL}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
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}
```

```text:結果:
      Ready
```


2.2.  新バージョンの確認
------------------------

-[ Environmentの内容確認 (describe-environments) ]-

```bash:コマンド:
aws elasticbeanstalk describe-environments \
        --application-name "${EB_APP_NAME}" \
        --environment-names "${EB_ENV_NAME}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
EB_MAX_ITEMS='4'
```

```bash:コマンド:
aws elasticbeanstalk describe-events \
        --application-name "${EB_APP_NAME}" \
        --environment-name ${EB_ENV_NAME} \
        --max-items ${EB_MAX_ITEMS}
```

```json:結果(例):
      {
        "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の取得 ]-

```bash:コマンド:
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}
```

```text:結果:
      Default-Environment-xxxxxxxxxxxx.elasticbeanstalk.com
```

-[ アクセス確認 ]-

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

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


3. version更新
==============

versionのdescriptionを更新します。

-[ versionの内容確認 (describe-application-versions) ]-

```bash:コマンド:
aws elasticbeanstalk describe-application-versions \
        --application-name "${EB_APP_NAME}" \
        --version-labels "${EB_VER_LABEL}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
EB_VER_DESC='hands-on version'
```

```bash:変数の確認:
cat << ETX

      EB_APP_NAME:  "${EB_APP_NAME}"
      EB_VER_LABEL: "${EB_VER_LABEL}"
      EB_VER_DESC:  "${EB_VER_DESC}"

ETX
```

```bash:コマンド:
aws elasticbeanstalk update-application-version \
        --application-name "${EB_APP_NAME}" \
        --version-label "${EB_VER_LABEL}" \
        --description "${EB_VER_DESC}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
aws elasticbeanstalk describe-application-versions \
        --application-name "${EB_APP_NAME}" \
        --version-labels "${EB_VER_LABEL}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
EB_MAX_ITEMS='2'
```

```bash:コマンド:
aws elasticbeanstalk describe-events \
        --application-name "${EB_APP_NAME}" \
        --max-items ${EB_MAX_ITEMS}
```

```json:結果(例):
      {
        "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"
          }
        ]
      }
```


4. アプリケーションの更新
=========================

アプリケーションのdescriptionを更新します。

-[ アプリケーションの内容確認 (describe-applications) ]-

```bash:コマンド:
aws elasticbeanstalk describe-applications \
        --application-name "${EB_APP_NAME}"
```

```json:結果(例):
      {
        "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"
          }
        ]
      }
```

```bash:コマンド:
EB_APP_DESC='hands-on app'
```

-[ アプリケーションのDescription更新 (update-application) ]-

```bash:変数の確認:
cat << ETX

      EB_APP_NAME: "${EB_APP_NAME}"
      EB_APP_DESC: "${EB_APP_DESC}"

ETX
```

```bash:コマンド:
aws elasticbeanstalk update-application \
        --application-name "${EB_APP_NAME}" \
        --description "${EB_APP_DESC}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
aws elasticbeanstalk describe-applications \
        --application-name "${EB_APP_NAME}"
```

```json:結果(例):
      {
        "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) ]-

```bash:コマンド:
EB_MAX_ITEMS='2'
```

```bash:コマンド:
aws elasticbeanstalk describe-events \
        --application-name "${EB_APP_NAME}" \
        --max-items ${EB_MAX_ITEMS}
```

```json:結果(例):
      {
        "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"
          }
        ]
      }
```


完了
====

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

http://qiita.com/tcsh/items/8ab7eb7dd06d1c575c2e