[JAWS-UG CLI] ElastiCache #3 キャッシュクラスタの削除 (Redis編)



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


ElastiCacheへの権限
-------------------

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


AWS CLI
-------

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

* AWS CLI 1.10.44

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

```text:結果(例):
      aws-cli/1.10.44 Python/2.7.11 Darwin/15.5.0 botocore/1.4.34
```


0. 準備
=======


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

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

```bash:変数の確認:
aws configure list
```

```text:結果(例):
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       elasticache-prjx-mbpr13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        ap-northeast-1        env    AWS_DEFAULT_REGION
```


0.2. 削除対象クラスタ名の指定
-----------------------------

```bash:変数の設定
ECACHE_CLUSTER_ID="handson-$(date +%Y%m%d)" \
      && echo ${ECACHE_CLUSTER_ID}
```


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


削除対象の確認
--------------

```bash:コマンド:
aws elasticache describe-cache-clusters \
        --cache-cluster-id ${ECACHE_CLUSTER_ID}
```

```json:結果(例):
      {
        "CacheClusters": [
          {
              "Engine": "redis",
              "CacheParameterGroup": {
                  "CacheNodeIdsToReboot": [],
                  "CacheParameterGroupName": "default.redis2.8",
                  "ParameterApplyStatus": "in-sync"
              },
              "CacheClusterId": "handson-20160704",
              "CacheSecurityGroups": [],
              "NumCacheNodes": 1,
              "CacheClusterCreateTime": "2016-07-04T01:23:45.678Z",
              "AutoMinorVersionUpgrade": true,
              "CacheClusterStatus": "available",
              "PreferredAvailabilityZone": "ap-northeast-1a",
              "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:",
              "SecurityGroups": [
                  {
                      "Status": "active",
                      "SecurityGroupId": "sg-xxxxxxxx"
                  }
              ],
              "CacheSubnetGroupName": "subnet-group-handson-20160704",
              "EngineVersion": "2.8.24",
              "PendingModifiedValues": {},
              "PreferredMaintenanceWindow": "sun:11:00-sun:12:00",
              "CacheNodeType": "cache.t2.micro"
          }
        ]
      }
```


2. キャッシュクラスターの削除
=============================

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

        ECACHE_CLUSTER_ID: ${ECACHE_CLUSTER_ID}

ETX
```

```bash:コマンド:
aws elasticache delete-cache-cluster \
        --cache-cluster-id ${ECACHE_CLUSTER_ID}
```

```json:結果(例):
      {
        "CacheCluster": {
          "Engine": "redis",
          "CacheParameterGroup": {
              "CacheNodeIdsToReboot": [],
              "CacheParameterGroupName": "default.redis2.8",
              "ParameterApplyStatus": "in-sync"
          },
          "CacheClusterId": "handson-20160704",
          "CacheSecurityGroups": [],
          "NumCacheNodes": 1,
          "CacheClusterCreateTime": "2016-07-04T0l:23:45.678Z",
          "AutoMinorVersionUpgrade": true,
          "CacheClusterStatus": "deleting",
          "PreferredAvailabilityZone": "ap-northeast-1a",
          "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:",
          "SecurityGroups": [
              {
                  "Status": "active",
                  "SecurityGroupId": "sg-161efe72"
              }
          ],
          "CacheSubnetGroupName": "subnet-group-handson-20160704",
          "EngineVersion": "2.8.24",
          "PendingModifiedValues": {},
          "PreferredMaintenanceWindow": "sun:11:00-sun:12:00",
          "CacheNodeType": "cache.t2.micro"
        }
      }
```


3. 事後作業
===========


3.1. ステータスの確認
---------------------

```bash:コマンド:
ECACHE_CLUSTER_STATUS=$( \
        aws elasticache describe-cache-clusters \
          --cache-cluster-id ${ECACHE_CLUSTER_ID} \
          --query 'CacheClusters[].CacheClusterStatus' \
          --output text \
) && echo ${ECACHE_CLUSTER_STATUS}
```

```text:結果(例):
      deleting
```


3.2. キャッシュクラスター不存在の確認
-------------------------------------

削除に5-10分ほどかかります。

```bash:コマンド:
aws elasticache describe-cache-clusters \
        --cache-cluster-id ${ECACHE_CLUSTER_ID}
```

```text:結果(例):
      An error occurred (CacheClusterNotFound) when calling the DescribeCacheClusters operation: CacheCluster not found: handson-20160704
```


3.3. イベントの確認
-------------------

```bash:変数の設定:
MAX_ITEMS='2'
```

```bash:コマンド:
aws elasticache describe-events \
        --query "Events[?SourceIdentifier == \`${ECACHE_CLUSTER_ID}\`]" \
        --max-items ${MAX_ITEMS}
```

```json:結果(例):
      {
        "Events": [
          {
              "Date": "2016-07-04T01:23:45.678Z",
              "Message": "Cache cluster deleted",
              "SourceIdentifier": "handson-20160704",
              "SourceType": "cache-cluster"
          },
          {
              "Date": "2016-07-04T01:23:45.678Z",
              "Message": "Removed cache nodes 0001",
              "SourceIdentifier": "handson-20160704",
              "SourceType": "cache-cluster"
          }
        ]
      }
```


完了
====