[JAWS-UG CLI] RDS:#2 DBインスタンスの削除

# 前提条件

## RDSへの権限

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

## AWS CLI

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

- AWS CLI 1.11.14

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

結果(例)

    aws-cli/1.11.14 Python/2.7.10 Darwin/15.6.0 botocore/1.4.71

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

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

# 0. 準備

## 0.1. リージョンの決定

```bash:変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'
```

## 0.2. 変数の確認

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

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

結果(例)

            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       rdsFull-prjz-mbp13        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.3. 削除するDBインスタンスの決定

```bash:変数の設定
RDS_INSTANCE_IDENT="<削除するインスタンス名>"
```

# 1. 事前作業

## 1.1. 削除するDBインスタンスの確認

```bash:コマンド
aws rds describe-db-instances \
        --db-instance-identifier ${RDS_INSTANCE_IDENT}
```

結果(例)

      {
        "DBInstances": [
          {
              "PubliclyAccessible": true,
              "MasterUsername": "pgadmin",
              "LicenseModel": "postgresql-license",
              "VpcSecurityGroups": [
                  {
                      "Status": "active",
                      "VpcSecurityGroupId": "sg-xxxxxxxx"
                  }
              ],
              "InstanceCreateTime": "2015-03-16T06:59:18.215Z",
              "OptionGroupMemberships": [
                  {
                      "Status": "in-sync",
                      "OptionGroupName": "default:postgres-9-3"
                  }
              ],
              "PendingModifiedValues": {},
              "Engine": "postgres",
              "MultiAZ": false,
              "LatestRestorableTime": "2015-03-16T08:00:29Z",
              "DBSecurityGroups": [],
              "DBParameterGroups": [
                  {
                      "DBParameterGroupName": "default.postgres9.3",
                      "ParameterApplyStatus": "in-sync"
                  }
              ],
              "AutoMinorVersionUpgrade": true,
              "PreferredBackupWindow": "14:23-14:53",
              "DBSubnetGroup": {
                  "Subnets": [
                      {
                          "SubnetStatus": "Active",
                          "SubnetIdentifier": "subnet-xxxxxxxx",
                          "SubnetAvailabilityZone": {
                              "Name": "ap-northeast-1c"
                          }
                      },
                      {
                          "SubnetStatus": "Active",
                          "SubnetIdentifier": "subnet-xxxxxxxx",
                          "SubnetAvailabilityZone": {
                              "Name": "ap-northeast-1a"
                          }
                      }
                  ],
                  "DBSubnetGroupName": "default",
                  "VpcId": "vpc-50b34035",
                  "DBSubnetGroupDescription": "default",
                  "SubnetGroupStatus": "Complete"
              },
              "ReadReplicaDBInstanceIdentifiers": [],
              "AllocatedStorage": 5,
              "BackupRetentionPeriod": 1,
              "DBName": "handson",
              "PreferredMaintenanceWindow": "sat:17:40-sat:18:10",
              "Endpoint": {
                  "Port": 5432,
                  "Address": "postgre-handson-ap-northeast-1.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com"
              },
              "DBInstanceStatus": "available",
              "EngineVersion": "9.3.5",
              "AvailabilityZone": "ap-northeast-1c",
              "StorageType": "standard",
              "DbiResourceId": "db-XNOYXXXXXXXXXXXXXXXXXXXXXX",
              "StorageEncrypted": false,
              "DBInstanceClass": "db.t1.micro",
              "DBInstanceIdentifier": "postgre-handson-ap-northeast-1"
          }
        ]
      }

# 2. DBインスタンスの削除

## 2.1. DBインスタンスの削除

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

        RDS_INSTANCE_IDENT ${RDS_INSTANCE_IDENT}

ETX
```

```bash:コマンド
aws rds delete-db-instance \
        --db-instance-identifier ${RDS_INSTANCE_IDENT} \
        --skip-final-snapshot
```

結果(例)

      {
        "DBInstance": {
          "PubliclyAccessible": true,
          "MasterUsername": "pgadmin",
          "LicenseModel": "postgresql-license",
          "VpcSecurityGroups": [
              {
                  "Status": "active",
                  "VpcSecurityGroupId": "sg-xxxxxxxx"
              }
          ],
          "InstanceCreateTime": "2015-03-16T06:59:18.215Z",
          "OptionGroupMemberships": [
              {
                  "Status": "in-sync",
                  "OptionGroupName": "default:postgres-9-3"
              }
          ],
          "PendingModifiedValues": {},
          "Engine": "postgres",
          "MultiAZ": false,
          "LatestRestorableTime": "2015-03-16T08:00:29Z",
          "DBSecurityGroups": [],
          "DBParameterGroups": [
              {
                  "DBParameterGroupName": "default.postgres9.3",
                  "ParameterApplyStatus": "in-sync"
              }
          ],
          "AutoMinorVersionUpgrade": true,
          "PreferredBackupWindow": "14:23-14:53",
          "DBSubnetGroup": {
              "Subnets": [
                  {
                      "SubnetStatus": "Active",
                      "SubnetIdentifier": "subnet-xxxxxxxx",
                      "SubnetAvailabilityZone": {
                          "Name": "ap-northeast-1c"
                      }
                  },
                  {
                      "SubnetStatus": "Active",
                      "SubnetIdentifier": "subnet-xxxxxxxx",
                      "SubnetAvailabilityZone": {
                          "Name": "ap-northeast-1a"
                      }
                  }
              ],
              "DBSubnetGroupName": "default",
              "VpcId": "vpc-50b34035",
              "DBSubnetGroupDescription": "default",
              "SubnetGroupStatus": "Complete"
          },
          "ReadReplicaDBInstanceIdentifiers": [],
          "AllocatedStorage": 5,
          "BackupRetentionPeriod": 1,
          "DBName": "handson",
          "PreferredMaintenanceWindow": "sat:17:40-sat:18:10",
          "Endpoint": {
              "Port": 5432,
              "Address": "postgre-handson-ap-northeast-1.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com"
          },
          "DBInstanceStatus": "deleting",
          "EngineVersion": "9.3.5",
          "AvailabilityZone": "ap-northeast-1c",
          "StorageType": "standard",
          "DbiResourceId": "db-XNOYXXXXXXXXXXXXXXXXXXXXXX",
          "StorageEncrypted": false,
          "DBInstanceClass": "db.t1.micro",
          "DBInstanceIdentifier": "postgre-handson-ap-northeast-1"
        }
      }

## 2.2. DBインスタンスの状態確認

```bash:変数の設定
RDS_INSTANCE_STATUS=$( \
        aws rds describe-db-instances \
          --db-instance-identifier ${RDS_INSTANCE_IDENT}\
          --query 'DBInstances[].DBInstanceStatus'\
          --output text \
) \
          && echo ${RDS_INSTANCE_STATUS}
```

結果

      deleting

# 3. 事後確認

## 3.1. DBインスタンスの状態確認

```bash:変数の設定
RDS_INSTANCE_STATUS=$( \
        aws rds describe-db-instances\
          --db-instance-identifier ${RDS_INSTANCE_IDENT}\
          --query 'DBInstances[].DBInstanceStatus'\
          --output text \
) \
        && echo ${RDS_INSTANCE_STATUS}
```

結果

      A client error (DBInstanceNotFound) occurred when calling the DescribeDBInstances operation: DBInstance postgre-handson not found.

## 3.2. 残留スナップショットの確認

```bash:コマンド
aws rds describe-db-snapshots \
          --db-instance-identifier ${RDS_INSTANCE_IDENT}
```

結果

      {
          "DBSnapshots": []
      }

# 完了