Amazon RDS(PostgreSQL)とRDS Proxyの使用

Amazon

はじめに

  • API Gateway + LambdaからRDS Proxy経由でRDSを操作するためのインフラ構築の一部

ハマりポイント

  • PostgreSQLクライアントのバージョンが10未満の場合、SCRAM認証に対応していない。バージョンアップしたほうがよい
  • RDS ProxyのTarget Groupが有効にならない場合、ProxyとRDSのセキュリティグループがあっていない場合がある。ProxyとRDSは別々に作成するため注意が必要。

RDSの作成

  • Standard create
  • Engine Type: PostgreSQL
  • Engine Version: PostgreSQL 16.3-R2
  • Templates: Dev/Test
  • Deployment Option: Single DB Instance
  • DB Instance ID: 自由入力
  • Master username: 自由入力
  • Credential management: AWS Secrets Manager
  • Encryption key: 選択 or 新規
  • DB instance class: 選択
  • Storage Type: 選択
  • Allocate Storage: サイズ入力
  • Provisioned IOPS: サイズ入力
  • Autoscaling: Yes/No
  • EC2接続: Yes
  • VPC: 選択
  • DB subnet group: 選択
  • Public Access: No
  • VPC security group:
  • AZ: 選択
  • Database Auth: Password Authentication
  • Initial database name: 自由入力
  • DB parameter group: 選択
  • Encryption: Yes / No

Secrets Manager secret

  • RDS作成時に作成し、Proxyでも同じものを指定する
  • このユーザーとパスワードをpsqlでも使用する

RDS Proxyの作成

  • Engine family: PosgreSQL
  • Proxy ID: 自由入力
  • Database Instance: 選択
  • Connection pool mac connections: %入力
  • Auth: IAM作成
  • Secrets Manager secret: 作成
  • Client Auth type: SCRAM SHA 256
  • IAM auth: Not Allowed
  • Require TLS: No
  • Subnets: 選択
  • VPC security group: 選択

接続確認

EC2からRDSへの接続確認

psql \
-U <username> \
-h<rds endpoint> \
-p 5432 \
-d <dbname> \
--password

パスワードプロンプトに入力

EC2からRDS Proxyへの接続確認

psql \
-U <username> \
-h <proxy endpoint> \
-p 5432 \
-d <dbname> \
--password

パスワードプロンプトに入力

RDS Proxy ターゲットの確認

aws rds describe-db-proxy-targets \
    --db-proxy-name <proxy id> \
    --target-group-name default

{
    "Targets": [
        {
            "Endpoint": "xxx",
            "RdsResourceId": "yyy",
            "Port": 5432,
            "Type": "RDS_INSTANCE",
            "Role": "READ_WRITE",
            "TargetHealth": {
                "State": "AVAILABLE"
            }
        }
    ]
}

RDS Proxyの確認

aws rds describe-db-proxies --db-proxy-name <proxy id>

{
    "DBProxies": [
        {
            "DBProxyName": "xxx",
            "DBProxyArn": "yyy",
            "Status": "available",
            "EngineFamily": "POSTGRESQL",
            "VpcId": "zzz",
            "VpcSecurityGroupIds": [
                "aaa"
            ],
            "VpcSubnetIds": [
                ...
            ],
            "Auth": [
                {
                    "AuthScheme": "SECRETS",
                    "SecretArn": "xxx",
                    "IAMAuth": "DISABLED",
                    "ClientPasswordAuthType": "POSTGRES_SCRAM_SHA_256"
                }
            ],
            "RoleArn": "xxx",
            "Endpoint": "xxx",
            "RequireTLS": false,
            "IdleClientTimeout": 1800,
            "DebugLogging": true,
            "CreatedDate": "2024-08-14T15:53:53.825000+00:00",
            "UpdatedDate": "2024-08-14T18:52:15.949000+00:00"
        }
    ]
}

EC2からポート開放を確認

% nc -zx <rds endpoint> 5432

Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to x.x.x.x:5432.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.

% nc -zx <rds proxy endpoint> 5432

Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to x.x.x.x:5432.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.

参考

関連記事

カテゴリー

アーカイブ

Lang »