[JAWS-UG CLI] CodeDeploy #2 アプリケーションの確認 (CodePipelineでデプロイした場合)

AWS CLIを利用して、CodePipelineでデプロイしたCodeDeploy上のアプリケーションを確認します。


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


CodeDeployへの権限
------------------

Code Deployに対してフル権限があること。


AWS CLIのバージョン
-------------------

以下のバージョンで動作確認済

- AWS CLI 1.11.2

```bash:コマンド:
aws --version
```

```text:結果(例):
         aws-cli/1.11.2 Python/2.7.11 Darwin/15.6.0 botocore/1.4.60
```

バージョンが古い場合は最新版に更新しましょう。

```bash:コマンド:
sudo -H pip install -U awscli
```


0. 準備
=======


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

プロファイルが想定のものになっていることを確認します。


```bash:コマンド:
aws configure list
```

```bash:結果(例)
               Name                    Value             Type    Location
               ----                    -----             ----    --------
            profile         iamFull-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
         access_key     ****************XXXX shared-credentials-file
         secret_key     ****************XXXX shared-credentials-file
             region                         us-east-1  env    AWS_DEFAULT_REGION
```

AssumeRoleを利用している場合はprofileが '<not set>'と表示されます。それ以外のときにprofileが '<not set>' と表示される場合は、以下を実行してください。

```bash:変数の設定:
export AWS_DEFAULT_PROFILE=<IAMユーザ名>
```


0.2. アプリケーション名の指定
-----------------------------

```bash:変数の設定
CODED_APP_NAME="handson-app-20161010"
```


0.3. デプロイメントグループ名の指定
-----------------------------------

```bash:変数の設定
CODED_DEPLOY_GROUP_NAME="handson-app-20161010-deployment-group"
```


1. 事前作業
===========


デプロイメントの確認
--------------------

```bash:コマンド:
aws deploy list-deployments 
```

```json:結果(例):
      {
        "deployments": [
          "d-xxxxxxxxx"
        ]
      }
```

```bash:変数の設定
CODED_DEPLOY_ID='<デプロイメントID>'
```

```bash:コマンド:
aws deploy get-deployment \
        --deployment-id ${CODED_DEPLOY_ID}
```

```json:結果(例):
      {
        "deploymentInfo": {
          "applicationName": "handson-app-20161010",
          "status": "Succeeded",
          "deploymentOverview": {
              "Failed": 0,
              "InProgress": 0,
              "Skipped": 0,
              "Succeeded": 1,
              "Pending": 0
          },
          "deploymentConfigName": "CodeDeployDefault.OneAtATimeCodeDeployDefault.OneAtATime",
          "creator": "user",
          "deploymentId": "d-xxxxxxxxx",
          "ignoreApplicationStopFailures": false,
          "autoRollbackConfiguration": {
              "enabled": false
          },
          "updateOutdatedInstancesOnly": false,
          "deploymentGroupName": "handson-app-20161010-deployment-group",
          "createTime": 1234567890.123,
          "completeTime": 1234567890.123,
          "revision": {
              "revisionType": "S3",
              "s3Location": {
                  "bundleType": "zip",
                  "eTag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-x",
                  "bucket": "codepipeline-us-east-1-xxxxxxxxxxxx",
                  "key": "MyFirstPipeline/MyApp/xxxxxxx"
              }
          }
        }
      }
```


2. デプロイメントインスタンスの確認
===================================


2.1. インスタンス一覧の確認
---------------------------

```bash:コマンド:
aws deploy list-deployment-instances \
        --deployment-id ${CODED_DEPLOY_ID}
```

```json:結果(例):
      {
        "instancesList": [
          "i-xxxxxxxx"
        ]
      }
```


2.2. インスタンスの指定
-----------------------

```bash:変数の設定
EC2_INSTANCE_ID='<インスタンスID>'
```


2.3. インスタンスの確認
-----------------------

```bash:コマンド:
aws deploy get-deployment-instance \
        --deployment-id ${CODED_DEPLOY_ID} \
        --instance-id ${EC2_INSTANCE_ID}
```

```json:結果(例):
      {
        "instanceSummary": {
          "instanceId": "arn:aws:ec2:us-east-1:XXXXXXXXXXXX:instance/i-72729742",
          "status": "Succeeded",
          "deploymentId": "d-xxxxxxxxx",
          "lastUpdatedAt": 1234567890.123,
          "lifecycleEvents": [
              {
                  "endTime": 1234567890.12,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567890.123,
                  "lifecycleEventName": "ApplicationStop"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567890.123,
                  "lifecycleEventName": "DownloadBundle"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567890.123,
                  "lifecycleEventName": "BeforeInstall"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567836.286,
                  "lifecycleEventName": "Install"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567837.518,
                  "lifecycleEventName": "AfterInstall"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567839.023,
                  "lifecycleEventName": "ApplicationStart"
              },
              {
                  "endTime": 1234567890.123,
                  "status": "Succeeded",
                  "diagnostics": {
                      "errorCode": "Success",
                      "scriptName": "",
                      "logTail": "",
                      "message": "Succeeded"
                  },
                  "startTime": 1234567890.123,
                  "lifecycleEventName": "ValidateService"
              }
          ]
        }
      }
```


3. アプリのリビジョン設定の確認
===============================

```bash:コマンド:
aws deploy list-application-revisions \
        --application-name ${CODED_APP_NAME}
```

```json:結果(例):
      {
        "revisions": [
          {
              "revisionType": "S3",
              "s3Location": {
                  "bundleType": "zip",
                  "eTag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-x",
                  "bucket": "codepipeline-us-east-1-xxxxxxxxxxxx",
                  "key": "MyFirstPipeline/MyApp/babsqoL"
              }
          }
        ]
      }
```

```bash:コマンド:
CODED_LOCATION_BUCKET=$( \
        aws deploy list-application-revisions \
          --application-name ${CODED_APP_NAME} \
          --query "revisions[?revisionType == \`S3\`].s3Location.bucket" \
          --output text \
) \
        && echo ${CODED_LOCATION_BUCKET}
```


```json:結果(例):
      codepipeline-us-east-1-123456789012
```

```bash:コマンド:
CODED_LOCATION_BUNDLETYPE=$( \
        aws deploy list-application-revisions \
          --application-name ${CODED_APP_NAME} \
          --query "revisions[?revisionType == \`S3\`].s3Location.bundleType" \
          --output text \
) \
        && echo ${CODED_LOCATION_BUNDLETYPE}
```


```json:結果(例):
      zip
```

```bash:コマンド:
CODED_LOCATION_ETAG=$( \
        aws deploy list-application-revisions \
          --application-name ${CODED_APP_NAME} \
          --query "revisions[?revisionType == \`S3\`].s3Location.eTag" \
          --output text \
) \
        && echo ${CODED_LOCATION_ETAG}
```


```json:結果(例):
      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-x
```

```bash:コマンド:
CODED_LOCATION_KEY=$( \
        aws deploy list-application-revisions \
          --application-name ${CODED_APP_NAME} \
          --query "revisions[?revisionType == \`S3\`].s3Location.key" \
          --output text \
) \
        && echo ${CODED_LOCATION_KEY}
```


```json:結果(例):
      handson-pipeline-20161010/MyApp/xxxxxxx
```

```bash:変数の設定
STR_CODED_APP_REVISION="bucket=${CODED_LOCATION_BUCKET},bundleType=${CODED_LOCATION_BUNDLETYPE},eTag=${CODED_LOCATION_ETAG},key=${CODED_LOCATION_KEY}" \
        && echo ${STR_CODED_APP_REVISION}
```

```bash:コマンド:
aws deploy get-application-revision \
        --application-name ${CODED_APP_NAME} \
        --s3-location ${STR_CODED_APP_REVISION}
```

```json:結果(例):
      {
        "applicationName": "handson-app-20161010",
        "revisionInfo": {
          "lastUsedTime": 1234567890.123,
          "firstUsedTime": 1234567890.123,
          "registerTime": 1234567890.123,
          "description": "Application revision registered by Deployment ID: d-xxxxxxxxx",
          "deploymentGroups": [
              "handson-app-20161010-deployment-group"
          ]
        },
        "revision": {
          "revisionType": "S3",
          "s3Location": {
              "bundleType": "zip",
              "eTag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-x",
              "bucket": "codepipeline-us-east-1-xxxxxxxxxxxx",
              "key": "MyFirstPipeline/MyApp/xxxxxxx"
          }
        }
      }
```


9. 事後作業
===========


完了
====