後始末8. DBインスタンスの削除 (handson-rds-dbinstance)

作業の目的 [why]

DBインスタンス"handson-rds-dbinstance"を削除します。

完了条件/事前条件 [設計者用情報]

完了条件 [after]

主処理は、以下を満たしたときに成功したものとします。

完了条件1
DBインスタンス"handson-rds-dbinstance"が存在しない。

事前条件 [before]

主処理の実施は、以下の状態であることを前提とします。

事前条件1
replace:: DBインスタンス"handson-rds-dbinstance"が存在する。

作業対象 [what]

  • RDS DBインスタンス

標準時間(合計)

8分

パラメータ設定

パラメータ設定の標準時間:2分

作業に必要なモノ・情報 [resource]

作業開始には、以下が全て揃っていることが必要です。

リソース1: DBインスタンス名

  • 作成するDBインスタンスの名称です。
  • 今回は"handson-rds-dbinstance"とします。

パラメータの指定

作業に必要なパラメータを変数に格納をします。

0.0. リージョンの指定

変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

0.1. DBインスタンス名の指定

DBインスタンス名を指定します。

変数の設定:

RDS_INSTANCE_IDENT='handson-rds-dbinstance'

パラメータの保存

設定されている変数の保存先となるファイル名を指定します。

変数の設定:

DIR_PARAMETER="${HOME}/tmp/parameter-handson-rds"
FILE_PARAMETER="${DIR_PARAMETER}/$(date +%Y-%m-%d)-rds-db_instance-delete.env" \
  && echo ${FILE_PARAMETER}

結果(例):

${HOME}/tmp/parameter-handson-rds/2019-06-10-rds-db_instance-delete.env

各変数に正しいパラメータ値が格納されていることを確認しながら保存します。

変数の確認:

cat << EOF > ${FILE_PARAMETER}

  # 0.0. AWS_DEFAULT_REGION:"ap-northeast-1"
         AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 0.1. RDS_INSTANCE_IDENT:"handson-rds-dbinstance"
         RDS_INSTANCE_IDENT="${RDS_INSTANCE_IDENT}"

EOF

cat ${FILE_PARAMETER}

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

タスクの実施

タスク標準時間:6分

1. 前処理

処理対象の状態確認

主処理の実施は、以下の状態であることを前提とします。

前提と異なることが判明した場合、直ちに処理を中止します。

事前条件1: replace:: DBインスタンス"handson-rds-dbinstance"が存在する。

「replace:: DBインスタンス"handson-rds-dbinstance"が存在する。」ことを確認します。

コマンド:

aws rds describe-db-instances \
  --query "DBInstances[?DBInstanceIdentifier == \`${RDS_INSTANCE_IDENT}\`].DBInstanceIdentifier" \
  --output text

結果(例):

handson-rds-dbinstance

2. 主処理

DBインスタンスの削除

変数の確認:

cat << ETX

  # RDS_INSTANCE_IDENT:"handson-rds-dbinstance"
    RDS_INSTANCE_IDENT="${RDS_INSTANCE_IDENT}"

ETX

コマンド:

aws rds delete-db-instance \
  --db-instance-identifier ${RDS_INSTANCE_IDENT} \
  --skip-final-snapshot

結果(例):

{
  "DBInstance": {
    "PubliclyAccessible": false,
    "MasterUsername": "testadmin",
    "MonitoringInterval": 0,
    "LicenseModel": "postgresql-license",
    "VpcSecurityGroups": [
        {
            "Status": "active",
            "VpcSecurityGroupId": "sg-xxxxxxxxxxxxxxxxx"
        }
    ],
    "InstanceCreateTime": "2019-06-10T01:23:45.678Z",
    "CopyTagsToSnapshot": false,
    "OptionGroupMemberships": [
        {
            "Status": "in-sync",
            "OptionGroupName": "default:postgres-9-6"
        }
    ],
    "PendingModifiedValues": {},
    "Engine": "postgres",
    "MultiAZ": false,
    "LatestRestorableTime": "2019-06-10T01:23:45.678Z",
    "DBSecurityGroups": [],
    "DBParameterGroups": [
        {
            "DBParameterGroupName": "default.postgres9.6",
            "ParameterApplyStatus": "in-sync"
        }
    ],
    "PerformanceInsightsEnabled": false,
    "AutoMinorVersionUpgrade": true,
    "PreferredBackupWindow": "18:34-19:04",
    "DBSubnetGroup": {
        "Subnets": [
            {
                "SubnetStatus": "Active",
                "SubnetIdentifier": "subnet-xxxxxxxxxxxxxxxxx",
                "SubnetAvailabilityZone": {
                    "Name": "ap-northeast-1a"
                }
            },
            {
                "SubnetStatus": "Active",
                "SubnetIdentifier": "subnet-yyyyyyyyyyyyyyyyy",
                "SubnetAvailabilityZone": {
                    "Name": "ap-northeast-1c"
                }
            }
        ],
        "DBSubnetGroupName": "handson-rds-dbsubnet",
        "VpcId": "vpc-f7f81090",
        "DBSubnetGroupDescription": "<DBサブネットグループの説明>",
        "SubnetGroupStatus": "Complete"
    },
    "ReadReplicaDBInstanceIdentifiers": [],
    "AllocatedStorage": 5,
    "DBInstanceArn": "arn:aws:rds:ap-northeast-1:XXXXXXXXXXXX:db:handson-rds-dbinstance",
    "BackupRetentionPeriod": 1,
    "DBName": "handson20171119",
    "PreferredMaintenanceWindow": "sun:15:07-sun:15:37",
    "Endpoint": {
        "HostedZoneId": "Zxxxxxxxxxxxxx",
        "Port": 5432,
        "Address": "test-20171119b.cxxuuipg9jy6.ap-northeast-1.rds.amazonaws.com"
    },
    "DBInstanceStatus": "deleting",
    "IAMDatabaseAuthenticationEnabled": false,
    "EngineVersion": "9.6.3",
    "AvailabilityZone": "ap-northeast-1a",
    "DomainMemberships": [],
    "StorageType": "standard",
    "DbiResourceId": "db-xxxxxxxxxxxxxxxxxxxxxxxxxx",
    "CACertificateIdentifier": "rds-ca-2015",
    "StorageEncrypted": false,
    "DBInstanceClass": "db.t2.micro",
    "DbInstancePort": 0,
    "DBInstanceIdentifier": "handson-rds-dbinstance"
  }
}

3. 後処理

3.1. 状態確認に必要な情報の取得

DBインスタンスの状態確認

コマンド:

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 handson-rds-dbinstancenot found.

注釈

'deleting' -> 'DBInstanceNotFound'エラーまで、db.t2.microで5分程度かかります。

3.2. 完了条件の確認

主処理は、以下を満たしたときに成功したものとします。

完了条件1: DBインスタンス"handson-rds-dbinstance"が存在しない。

「DBインスタンス"handson-rds-dbinstance"が存在しない。」ことを確認します。

コマンド:

aws rds describe-db-instances \
  --query "DBInstances[?DBInstanceIdentifier == \`${RDS_INSTANCE_IDENT}\`].DBInstanceIdentifier" \
  --output text

結果(例):

(出力なし)

完了