

# Amazon Aurora のカスタムエンドポイントの AWS CLI 例
<a name="Aurora.Endpoint.Tutorial"></a>

次のチュートリアルでは、AWS CLI による Unix シェル構文の例を使用して、いくつかの「小さい」DB インスタンスと少数の「大きい」DB インスタンスで構成されたクラスターを定義し、カスタムエンドポイントを作成して DB インスタンスの各セットに接続する方法を示します。自分のシステムで同様のコマンドを実行するには、リージョン、サブネットグループ、VPC セキュリティグループなどのパラメータに独自の値を指定するために、Aurora クラスターと AWS CLI の基本的な使用法に精通している必要があります。

この例では、初期のセットアップステップである、Aurora クラスターの作成とクラスターへの DB インスタンスの追加を示します。これは異種クラスターです。つまり、すべての DB インスタンスが同じ容量を持っているわけではありません。ほとんどのインスタンスは AWS インスタンスクラス `db.r4.4xlarge` を使用しますが、最後の 2 つの DB インスタンスは `db.r4.16xlarge` を使用します。以下の各 `create-db-instance` コマンドでは、出力を画面に表示し、JSON のコピーをファイルに保存して後で検査できるようにします。

```
aws rds create-db-cluster --db-cluster-identifier custom-endpoint-demo --engine aurora-mysql \
     --engine-version 8.0.mysql_aurora.3.04.0 --master-username $MASTER_USER --manage-master-user-password \
     --db-subnet-group-name $SUBNET_GROUP  --vpc-security-group-ids $VPC_SECURITY_GROUP \
     --region $REGION

for i in 01 02 03 04 05 06 07 08
do
  aws rds create-db-instance --db-instance-identifier custom-endpoint-demo-${i} \
     --engine aurora --db-cluster-identifier custom-endpoint-demo --db-instance-class db.r4.4xlarge \
     --region $REGION \
     | tee custom-endpoint-demo-${i}.json
done

for i in 09 10
do
  aws rds create-db-instance --db-instance-identifier custom-endpoint-demo-${i} \
     --engine aurora --db-cluster-identifier custom-endpoint-demo --db-instance-class db.r4.16xlarge \
     --region $REGION \
     | tee custom-endpoint-demo-${i}.json
done
```

より大きいインスタンスは特種なレポートクエリ用に予約されています。これらのインスタンスがプライマリインスタンスに昇格されないように、次の例では昇格階層を最低の優先度に変更します。この例では、マスターユーザーパスワードを生成して Secrets Manager で管理する `--manage-master-user-password` オプションを指定しています。詳細については、「[Amazon Aurora および AWS Secrets Manager によるパスワード管理](rds-secrets-manager.md)」を参照してください。または、`--master-password` オプションを使用して、自分でパスワードを指定して管理することもできます。

```
for i in 09 10
do
  aws rds modify-db-instance --db-instance-identifier custom-endpoint-demo-${i} \
    --region $REGION --promotion-tier 15
done
```

例えば、2 つの「大きい」インスタンスを、リソースを最も集中的に使用するクエリ専用にするとします。これを行うには、初期にカスタムの読み取り専用エンドポイントを作成します。次に、このエンドポイントから上記の DB インスタンスにのみ接続するように、メンバーの静的なリストを追加できます。これらのインスタンスは、既に最低の昇格階層にあるため、いずれのインスタンスもプライマリインスタンスに昇格される可能性はありません。いずれかがプライマリインスタンスに昇格されたとしても、タイプとして `READER` ではなく `ANY` を指定しているため、このエンドポイントを通じてインスタンスに到達することはできなくなります。

次の例では、エンドポイントの作成コマンドと変更コマンドを示します。JSON 出力例は、カスタムエンドポイントの当初の状態と変更後の状態を示しています。

```
$ aws rds create-db-cluster-endpoint --region $REGION \
    --db-cluster-identifier custom-endpoint-demo \
    --db-cluster-endpoint-identifier big-instances --endpoint-type reader
{
    "EndpointType": "CUSTOM",
    "Endpoint": "big-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
    "DBClusterEndpointIdentifier": "big-instances",
    "DBClusterIdentifier": "custom-endpoint-demo",
    "StaticMembers": [],
    "DBClusterEndpointResourceIdentifier": "cluster-endpoint-W7PE3TLLFNSHXQKFU6J6NV5FHU",
    "ExcludedMembers": [],
    "CustomEndpointType": "READER",
    "Status": "creating",
    "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:111122223333:cluster-endpoint:big-instances"
}

$ aws rds modify-db-cluster-endpoint --db-cluster-endpoint-identifier big-instances \
    --static-members custom-endpoint-demo-09 custom-endpoint-demo-10 --region $REGION
{
    "EndpointType": "CUSTOM",
    "ExcludedMembers": [],
    "DBClusterEndpointIdentifier": "big-instances",
    "DBClusterEndpointResourceIdentifier": "cluster-endpoint-W7PE3TLLFNSHXQKFU6J6NV5FHU",
    "CustomEndpointType": "READER",
    "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:111122223333:cluster-endpoint:big-instances",
    "StaticMembers": [
        "custom-endpoint-demo-10",
        "custom-endpoint-demo-09"
    ],
    "Status": "modifying",
    "Endpoint": "big-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
    "DBClusterIdentifier": "custom-endpoint-demo"
}
```

クラスターのデフォルトの `READER` エンドポイントは、「小さい」DB インスタンスまたは「大きい」DB インスタンスに接続できます。クラスターがビジー状態になると、クエリのパフォーマンスとスケーラビリティは予測不可能になります。DB インスタンスのセット間でワークロードを明確に分けるには、デフォルトの `READER` エンドポイントを無視して、他のすべての DB インスタンスに接続する 2 番目のカスタムエンドポイントを作成できます。次の例では、これを行うために、カスタムエンドポイントを作成して除外リストを追加します。後でクラスターに追加した他の DB インスタンスは、このエンドポイントに自動的に追加されます。`ANY` タイプでは、このエンドポイントに合計 8 個のインスタンス (プライマリインスタンスと 7 個の Aurora レプリカ) が関連付けられます。この例で `READER` タイプを使用したとすると、カスタムエンドポイントは 7 個のみの Aurora レプリカに関連付けられます。

```
$ aws rds create-db-cluster-endpoint --region $REGION --db-cluster-identifier custom-endpoint-demo \
    --db-cluster-endpoint-identifier small-instances --endpoint-type any
{
    "Status": "creating",
    "DBClusterEndpointIdentifier": "small-instances",
    "CustomEndpointType": "ANY",
    "EndpointType": "CUSTOM",
    "Endpoint": "small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
    "StaticMembers": [],
    "ExcludedMembers": [],
    "DBClusterIdentifier": "custom-endpoint-demo",
    "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:111122223333:cluster-endpoint:small-instances",
    "DBClusterEndpointResourceIdentifier": "cluster-endpoint-6RDDXQOC3AKKZT2PRD7ST37BMY"
}

$ aws rds modify-db-cluster-endpoint --db-cluster-endpoint-identifier small-instances \
    --excluded-members custom-endpoint-demo-09 custom-endpoint-demo-10 --region $REGION
{
    "DBClusterEndpointIdentifier": "small-instances",
    "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:c7tj4example:cluster-endpoint:small-instances",
    "DBClusterEndpointResourceIdentifier": "cluster-endpoint-6RDDXQOC3AKKZT2PRD7ST37BMY",
    "CustomEndpointType": "ANY",
    "Endpoint": "small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
    "EndpointType": "CUSTOM",
    "ExcludedMembers": [
        "custom-endpoint-demo-09",
        "custom-endpoint-demo-10"
    ],
    "StaticMembers": [],
    "DBClusterIdentifier": "custom-endpoint-demo",
    "Status": "modifying"
}
```

次の例では、このクラスターのエンドポイントの状態をチェックします。クラスターには、まだ元のクラスターエンドポイントがあります。`EndPointType` の `WRITER` は、引き続き管理、ETL、その他の書き込みオペレーションに使用することにします。元の `READER` エンドポイントは、「小さい」DB インスタンスまたは「大きい」DB インスタンスに振り向けられる可能性があるため、使用しないことにします。カスタムエンドポイントでは、この動作が予測可能になり、指定したエンドポイントに基づいて「小さい」DB インスタンスまたは「大きい」DB インスタンスのいずれかが接続で確実に使用されます。

```
$ aws rds describe-db-cluster-endpoints --region $REGION
{
    "DBClusterEndpoints": [
        {
            "EndpointType": "WRITER",
            "Endpoint": "custom-endpoint-demo.cluster-c7tj4example.ca-central-1.rds.amazonaws.com",
            "Status": "available",
            "DBClusterIdentifier": "custom-endpoint-demo"
        },
        {
            "EndpointType": "READER",
            "Endpoint": "custom-endpoint-demo.cluster-ro-c7tj4example.ca-central-1.rds.amazonaws.com",
            "Status": "available",
            "DBClusterIdentifier": "custom-endpoint-demo"
        },
        {
            "Endpoint": "small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
            "CustomEndpointType": "ANY",
            "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:111122223333:cluster-endpoint:small-instances",
            "ExcludedMembers": [
                "custom-endpoint-demo-09",
                "custom-endpoint-demo-10"
            ],
            "DBClusterEndpointResourceIdentifier": "cluster-endpoint-6RDDXQOC3AKKZT2PRD7ST37BMY",
            "DBClusterIdentifier": "custom-endpoint-demo",
            "StaticMembers": [],
            "EndpointType": "CUSTOM",
            "DBClusterEndpointIdentifier": "small-instances",
            "Status": "modifying"
        },
        {
            "Endpoint": "big-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com",
            "CustomEndpointType": "READER",
            "DBClusterEndpointArn": "arn:aws:rds:ca-central-1:111122223333:cluster-endpoint:big-instances",
            "ExcludedMembers": [],
            "DBClusterEndpointResourceIdentifier": "cluster-endpoint-W7PE3TLLFNSHXQKFU6J6NV5FHU",
            "DBClusterIdentifier": "custom-endpoint-demo",
            "StaticMembers": [
                "custom-endpoint-demo-10",
                "custom-endpoint-demo-09"
            ],
            "EndpointType": "CUSTOM",
            "DBClusterEndpointIdentifier": "big-instances",
            "Status": "available"
        }
    ]
}
```

最後の例では、カスタムエンドポイントへの以降のデータベース接続が Aurora クラスターのさまざまな DB インスタンスに接続されることを示します。`small-instances` エンドポイントは、`db.r4.4xlarge` DB インスタンス (このクラスターの下位数字のホスト) に常に接続します。

```
$ mysql -h small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com -u $MYUSER -p
mysql> select @@aurora_server_id;
+-------------------------+
| @@aurora_server_id      |
+-------------------------+
| custom-endpoint-demo-02 |
+-------------------------+

$ mysql -h small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com -u $MYUSER -p
mysql> select @@aurora_server_id;
+-------------------------+
| @@aurora_server_id      |
+-------------------------+
| custom-endpoint-demo-07 |
+-------------------------+

$ mysql -h small-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com -u $MYUSER -p
mysql> select @@aurora_server_id;
+-------------------------+
| @@aurora_server_id      |
+-------------------------+
| custom-endpoint-demo-01 |
+-------------------------+
```

`big-instances` エンドポイントは、`db.r4.16xlarge` DB インスタンス (このクラスターの最上位数字のホスト 2 つ) に常に接続します。

```
$ mysql -h big-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com -u $MYUSER -p
mysql> select @@aurora_server_id;
+-------------------------+
| @@aurora_server_id      |
+-------------------------+
| custom-endpoint-demo-10 |
+-------------------------+

$ mysql -h big-instances.cluster-custom-c7tj4example.ca-central-1.rds.amazonaws.com -u $MYUSER -p
mysql> select @@aurora_server_id;
+-------------------------+
| @@aurora_server_id      |
+-------------------------+
| custom-endpoint-demo-09 |
+-------------------------+
```