[JAWS-UG CLI] ElasticBeanstalk #3 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. 準備
=======


0.1. 変数の確認
---------------

```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}"

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}
```

```text:結果(例):
      e-xxxxxxxxxx
```

ノート: Environmentは、Environment IDとEnvironment Nameの2つで特定で
  きますが 、ここではなるべくEnvironment IDを利用するようにします。


1. Environmentの破棄
====================

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

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

```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"
          }
        ]
      }
```

-[ Environmentのリソース確認 (describe-environment-resources) ]-

```bash:コマンド:
aws elasticbeanstalk describe-environment-resources \
        --environment-id ${EB_ENV_ID}
```

```json:結果(例):
      {
          "EnvironmentResources": {
              "EnvironmentName": "Default-Environment", 
              "AutoScalingGroups": [
                  {
                      "Name": "awseb-e-mvmwrxyipz-stack-AWSEBAutoScalingGroup-xxxxxxxxxxxxx"
                  }
              ], 
              "Triggers": [], 
              "LoadBalancers": [
                  {
                      "Name": "awseb-e-m-AWSEBLoa-xxxxxxxxxxxx"
                  }
              ], 
              "Queues": [], 
              "Instances": [
                  {
                      "Id": "i-xxxxxxxx"
                  }
              ], 
              "LaunchConfigurations": [
                  {
                      "Name": "awseb-e-mvmwrxyipz-stack-AWSEBAutoScalingLaunchConfiguration-xxxxxxxxxxxxx"
                  }
              ]
          }
      }
```

-[ 破棄 (terminate-environment) ]-

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

      EB_ENV_ID:  ${EB_ENV_ID}

ETX
```

```bash:コマンド:
aws elasticbeanstalk terminate-environment \
        --environment-id ${EB_ENV_ID}
```

```json:結果(例):
      {
        "ApplicationName": "My First Elastic Beanstalk Application",
        "EnvironmentName": "Default-Environment",
        "Status": "Terminating",
        "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-02T08:03:49.612Z",
        "DateCreated": "2015-03-02T07:15:58.547Z"
      }
```

-[ Environmentのステータス確認 (describe-environments) ]-

statusを確認する。

```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:結果(例):
      Terminated
```

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

```json:結果(例):
      {
        "Environments": [
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "VersionLabel": "Sample Application Second Version",
              "Status": "Terminated",
              "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-02T08:07:32.598Z",
              "DateCreated": "2015-03-02T07:15:58.547Z"
          }
        ]
      }
```

-[ Environmentのリソース確認 (describe-environment-resources) ]-

```bash:コマンド:
aws elasticbeanstalk describe-environment-resources \
        --environment-name ${EB_ENV_NAME}
```

```text:結果(例):
A client error (InvalidParameterValue) occurred when calling the DescribeEnvironmentResources operation: No Environment found for EnvironmentName = 'Default-Environment'.
```

-[ イベント確認 (describe-events) ]-

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

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

```json:結果(例):
      {
        "NextToken": "None___13",
        "Events": [
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "terminateEnvironment completed successfully.",
              "EventDate": "2015-03-02T08:07:32.734Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleting SNS topic for environment Default-Environment.",
              "EventDate": "2015-03-02T08:07:31.148Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted security group named: sg-ee7e518b",
              "EventDate": "2015-03-02T08:07:14.647Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted load balancer named: awseb-e-p-AWSEBLoa-1JIB95ZPILT5G",
              "EventDate": "2015-03-02T08:06:00.579Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted security group named: awseb-e-pm2m8tsyuc-stack-AWSEBSecurityGroup-A21GRYC1XDZR",
              "EventDate": "2015-03-02T08:05:58.135Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted Auto Scaling launch configuration named: awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingLaunchConfiguration-1ATRW47YSHDS9",
              "EventDate": "2015-03-02T08:05:55.462Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted Auto Scaling group named: awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingGroup-1FL6R7WEIXQE1",
              "EventDate": "2015-03-02T08:05:53.075Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Waiting for EC2 instances to terminate. This may take a few minutes.",
              "EventDate": "2015-03-02T08:04:05.504Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:XXXXXXXXXXXX:scalingPolicy:332d7aa6-c84d-497d-a4ed-fb8c5a1389e2:autoScalingGroupName/awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingGroup-1FL6R7WEIXQE1:policyName/awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingScaleDownPolicy-92IEAFZFDKNJ",
              "EventDate": "2015-03-02T08:04:03.948Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:XXXXXXXXXXXX:scalingPolicy:6b5fbcd5-111d-47bb-bb1d-27d84eb75906:autoScalingGroupName/awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingGroup-1FL6R7WEIXQE1:policyName/awseb-e-pm2m8tsyuc-stack-AWSEBAutoScalingScaleUpPolicy-1UIJ44XP4FQGY",
              "EventDate": "2015-03-02T08:04:02.471Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted CloudWatch alarm named: awseb-e-pm2m8tsyuc-stack-AWSEBCloudwatchAlarmLow-1RM59RCZ9QV4H ",
              "EventDate": "2015-03-02T08:04:01.672Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "Deleted CloudWatch alarm named: awseb-e-pm2m8tsyuc-stack-AWSEBCloudwatchAlarmHigh-CLNQSP4AHG1J ",
              "EventDate": "2015-03-02T08:04:00.155Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "EnvironmentName": "Default-Environment",
              "Severity": "INFO",
              "RequestId": "a88138db-c0b2-11e4-ac14-7f1caa62b011",
              "Message": "terminateEnvironment is starting.",
              "EventDate": "2015-03-02T08:03:49.643Z"
          }
        ]
      }
```


2. version削除
==============

不要になった過去のバージョンを削除します。


2.1. バージョンの一覧取得
-------------------------

```bash:コマンド:
aws elasticbeanstalk describe-application-versions \
        --application-name "${EB_APP_NAME}" \
        --query 'ApplicationVersions[].VersionLabel'
```

```json:結果(例):
      [
          "Sample Application Second Version",
          "Sample Application"
      ]
```


2.2. 1st versionの削除
----------------------

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

-[ 削除 (delete-application-version) ]-

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

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

ETX
```

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

```text:結果:
      (戻り値なし)
```

-[ イベント確認 (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",
              "Severity": "INFO",
              "RequestId": "fc6e6627-c0b3-11e4-ac14-7f1caa62b011",
              "Message": "deleteApplicationVersions completed successfully.",
              "EventDate": "2015-03-02T08:13:19.739Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "VersionLabel": "Sample Application",
              "Severity": "INFO",
              "RequestId": "fc6e6627-c0b3-11e4-ac14-7f1caa62b011",
              "Message": "Deleted Application Version: Sample Application",
              "EventDate": "2015-03-02T08:13:19.675Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "VersionLabel": "Sample Application",
              "Severity": "INFO",
              "RequestId": "fc6e6627-c0b3-11e4-ac14-7f1caa62b011",
              "Message": "deleteApplicationVersions is starting.",
              "EventDate": "2015-03-02T08:13:19.555Z"
          }
        ]
      }
```


2.3. 2nd versionの削除
----------------------

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

-[ 削除 (delete-application-version) ]-

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

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

ETX
```

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

```text:結果:
      (戻り値なし)
```

-[ イベント確認 (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": "1f509bb8-c0b4-11e4-99d0-e3965e27cbd3",
              "Message": "deleteApplicationVersions completed successfully.",
              "EventDate": "2015-03-02T08:14:18.166Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "VersionLabel": "Sample Application Second Version",
              "Severity": "INFO",
              "RequestId": "1f509bb8-c0b4-11e4-99d0-e3965e27cbd3",
              "Message": "Deleted Application Version: Sample Application Second Version",
              "EventDate": "2015-03-02T08:14:18.110Z"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "VersionLabel": "Sample Application Second Version",
              "Severity": "INFO",
              "RequestId": "1f509bb8-c0b4-11e4-99d0-e3965e27cbd3",
              "Message": "deleteApplicationVersions is starting.",
              "EventDate": "2015-03-02T08:14:18.062Z"
          }
        ]
      }
```


2.4. バージョンの不存在確認
---------------------------

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

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

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


3. アプリケーションの削除
=========================

アプリケーションを削除します。

-[ アプリケーションの存在確認 (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:50:35.104Z",
              "Description": "hands-on app",
              "DateCreated": "2015-03-02T07:08:07.952Z"
          }
        ]
      }
```

-[ 削除 (delete-application) ]-

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

      EB_APP_NAME: "${EB_APP_NAME}"

ETX
```

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

```text:結果:
      (戻り値なし)
```

-[ アプリケーションの不存在確認 (describe-applications) ]-

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

```json:結果(例):
      {
          "Applications": []
      }
```

-[ イベント確認 (describe-events) ]-

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

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

```json:結果(例):
      {
        "NextToken": "None___4",
        "Events": [
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "Message": "The application has been deleted successfully.",
              "Severity": "INFO",
              "EventDate": "2015-03-02T08:17:01.296Z",
              "RequestId": "7fd57823-c0b4-11e4-84bf-d7f83bbc1d71"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "Message": "The environment termination step is done.",
              "Severity": "INFO",
              "EventDate": "2015-03-02T08:17:00.522Z",
              "RequestId": "7fd57823-c0b4-11e4-84bf-d7f83bbc1d71"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "Message": "No environment needs to be terminated.",
              "Severity": "INFO",
              "EventDate": "2015-03-02T08:17:00.333Z",
              "RequestId": "7fd57823-c0b4-11e4-84bf-d7f83bbc1d71"
          },
          {
              "ApplicationName": "My First Elastic Beanstalk Application",
              "Message": "deleteApplication is starting.",
              "Severity": "INFO",
              "EventDate": "2015-03-02T08:17:00.087Z",
              "RequestId": "7fd57823-c0b4-11e4-84bf-d7f83bbc1d71"
          }
        ]
      }
```

4. ソースコードの削除
======================

```bash:コマンド:
aws s3 rm s3://${S3_BUCKET_NAME}/php-sample.zip
aws s3 rm s3://${S3_BUCKET_NAME}/php-secondsample.zip
```

完了
====