[JAWS-UG CLI] RDS:#3 DBインスタンスの更新 (拡張モニタリングの有効化)


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


RDSへの権限
-----------

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


EC2への権限
-----------

EC2への読み取り権限があること。


AWS CLI
-------

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

- AWS CLI 1.11.28

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

結果(例):

      aws-cli/1.11.28 Python/2.7.10 Darwin/15.6.0 botocore/1.4.85

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

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


AWSアカウントの属性
-------------------

AWSアカウントがEC2-Classicに対応していないこと。

```bash:コマンド:
AWS_SUPPORT_PLATFORMS=$( \
         aws ec2 describe-account-attributes \
           --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
           --output text \
) \
        && echo ${AWS_SUPPORT_PLATFORMS}
```

結果:

      VPC

注釈: 'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは
  利用し てください。


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


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


1.1. DB Instance Identifierの指定
---------------------------------

```bash:変数の設定:
RDS_INSTANCE_IDENT="postgre-handson-20161114"
```

```bash:コマンド:
aws rds describe-db-instances \
        --db-instance-identifier ${RDS_INSTANCE_IDENT} \
        --query 'DBInstances[].{MonitoringInterval:MonitoringInterval,MonitoringRoleArn:MonitoringRoleArn}'
```

結果(例):

      [
        {
          "MonitoringInterval": 0
        }
      ]


1.2. 監視周期の指定
-------------------

```bash:変数の設定:
RDS_MONITORING_INTERVAL="15"
```


1.3. IAMロールの指定
--------------------

```bash:変数の設定:
IAM_ROLE_NAME="RDSMonitoringRole"
```

ARNを取得します。

```bash:コマンド:
IAM_ROLE_ARN=$( \
        aws iam get-role \
          --role-name ${IAM_ROLE_NAME} \
          --query 'Role.Arn' \
          --output text \
) \
        && echo ${IAM_ROLE_ARN}
```

結果(例):

      arn:aws:iam::XXXXXXXXXXXX:role/RDSMonitoringRole


2. DBインスタンスの変更
=======================


2.1. 起動時刻の確認
-------------------

```bash:変数の設定:
DATETIME_UTC=$( date -u '+%Y-%m-%dT%H:%MZ' ) \
        && echo ${DATETIME_UTC}
```

結果(例):

      2016-12-18T01:23Z


2.2. DBインスタンスの変更
-------------------------

```bash:コマンド:
aws rds modify-db-instance \
        --db-instance-identifier ${RDS_INSTANCE_IDENT} \
        --monitoring-interval ${RDS_MONITORING_INTERVAL} \
        --monitoring-role-arn ${IAM_ROLE_ARN} \
        --apply-immediately
```

結果(例):

      {
        "DBInstance": {
          "PubliclyAccessible": false,
          "MasterUsername": "pgadmin",
          "MonitoringInterval": 15,
          "LicenseModel": "postgresql-license",
          "VpcSecurityGroups": [
              {
                  "Status": "active",
                  "VpcSecurityGroupId": "sg-xxxxxxxx"
              }
          ],
          "InstanceCreateTime": "2016-12-18T01:23:45.678Z",
          "CopyTagsToSnapshot": false,
          "OptionGroupMemberships": [
              {
                  "Status": "in-sync",
                  "OptionGroupName": "default:postgres-9-5"
              }
          ],
          "PendingModifiedValues": {},
          "Engine": "postgres",
          "MultiAZ": false,
          "LatestRestorableTime": "2016-12-18T01:23:45Z",
          "DBSecurityGroups": [],
          "DBParameterGroups": [
              {
                  "DBParameterGroupName": "default.postgres9.5",
                  "ParameterApplyStatus": "in-sync"
              }
          ],
          "AutoMinorVersionUpgrade": true,
          "PreferredBackupWindow": "19:06-19:36",
          "DBSubnetGroup": {
              "Subnets": [
                  {
                      "SubnetStatus": "Active",
                      "SubnetIdentifier": "subnet-xxxxxxxx",
                      "SubnetAvailabilityZone": {
                          "Name": "ap-northeast-1a"
                      }
                  },
                  {
                      "SubnetStatus": "Active",
                      "SubnetIdentifier": "subnet-xxxxxxxx",
                      "SubnetAvailabilityZone": {
                          "Name": "ap-northeast-1c"
                      }
                  }
              ],
              "DBSubnetGroupName": "postgrehandson",
              "VpcId": "vpc-xxxxxxxx",
              "DBSubnetGroupDescription": "PostgreSQL Redmine",
              "SubnetGroupStatus": "Complete"
          },
          "ReadReplicaDBInstanceIdentifiers": [],
          "AllocatedStorage": 5,
          "DBInstanceArn": "arn:aws:rds:ap-northeast-1:XXXXXXXXXXXX:db:postgre-handson-20161114",
          "BackupRetentionPeriod": 1,
          "DBName": "handson",
          "PreferredMaintenanceWindow": "tue:13:00-tue:13:30",
          "Endpoint": {
              "HostedZoneId": "ZXXXXXXXXXXXXX",
              "Port": 5432,
              "Address": "postgre-handson-20161114.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com"
          },
          "DBInstanceStatus": "available",
          "EngineVersion": "9.5.4",
          "AvailabilityZone": "ap-northeast-1c",
          "DomainMemberships": [],
          "MonitoringRoleArn": "arn:aws:iam::XXXXXXXXXXXX:role/RDSMonitoringRole",
          "StorageType": "standard",
          "DbiResourceId": "db-XXXXXXXXXXXXXXXXXXXXXXXXXX",
          "CACertificateIdentifier": "rds-ca-2015",
          "StorageEncrypted": false,
          "DBInstanceClass": "db.t2.micro",
          "DbInstancePort": 0,
          "DBInstanceIdentifier": "postgre-handson-20161114"
        }
      }


3. 事後確認
===========


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

```bash:変数の設定:
RDS_EVENT_TYPE='db-instance'
RDS_EVENT_START=${DATETIME_UTC}
RDS_MAX_ITEMS='1'
```

```bash:コマンド:
aws rds describe-events \
        --start-time ${RDS_EVENT_START} \
        --source-type ${RDS_EVENT_TYPE} \
        --max-items ${RDS_MAX_ITEMS}
```

結果(例):

      {
        "Events": [
          {
              "EventCategories": [
                  "configuration change"
              ],
              "SourceType": "db-instance",
              "SourceArn": "arn:aws:rds:ap-northeast-1:XXXXXXXXXXXX:db:postgre-handson-20161114",
              "Date": "2016-12-18T01:23:45.678Z",
              "Message": "Monitoring Interval changed to 15",
              "SourceIdentifier": "postgre-handson-20161114"
          }
        ]
      }


3.2. DBインスタンス情報の確認
-----------------------------

```bash:コマンド:
aws rds describe-db-instances \
        --db-instance-identifier ${RDS_INSTANCE_IDENT} \
        --query 'DBInstances[].{MonitoringInterval:MonitoringInterval,MonitoringRoleArn:MonitoringRoleArn}'
```

結果(例):

      [
        {
          "MonitoringRoleArn": "arn:aws:iam::XXXXXXXXXXXX:role/RDSMonitoringRole",
          "MonitoringInterval": 15
        }
      ]


完了
====