{Aurora}Amazon RDS Proxy による接続の管理

 

https://qiita.com/ttkn9a/items/2d325fc5e170721102d2

RDS Proxy は MySQL エンジンファミリーおよび PostgreSQL エンジンファミリーで使用できます。

特定のユーザーとしてプロキシを介して接続するには、シークレットに関連付けられているパスワードが、
そのユーザーのデータベースパスワードと一致していることを確認してください。


前提:
Aurora MySQL構築済
KSMキーはAWSマネージド型キーを使用

 

-- 1.AWS Secrets Manager でのシークレットの設定

aws secretsmanager create-secret \
--name secret01 \
--description "secret01" \
--secret-string '{"username":"root","password":"root"}'

aws secretsmanager list-secrets
aws secretsmanager list-secrets --query '*[].[Name,ARN]' --output table

 

-- 2.AWS Identity and Access Management (IAM) ポリシーの設定

-- ロールの作成
aws iam create-role --role-name role01 \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":["rds.amazonaws.com"]},"Action":"sts:AssumeRole"}]}'

 

-- インラインポリシー追加
vi a.json

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetSecretValue",
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": [
"arn:aws:secretsmanager:ap-northeast-1:123456789012:secret:secret01-XXXXXX"
]
},
{
"Sid": "DecryptSecretValue",
"Action": [
"kms:Decrypt"
],
"Effect": "Allow",
"Resource": [
"arn:aws:kms:ap-northeast-1:123456789012:key/11111111-2222-3333-4444-555555555555"
],
"Condition": {
"StringEquals": {
"kms:ViaService": "secretsmanager.ap-northeast-1.amazonaws.com"
}
}
}
]
}

aws iam put-role-policy --role-name role01 \
--policy-name policy01 --policy-document file://a.json

 


-- 3.RDS Proxy の作成

aws rds create-db-proxy \
--db-proxy-name proxy01 \
--engine-family MYSQL \
--auth SecretArn=arn:aws:secretsmanager:ap-northeast-1:123456789012:secret:secret01-XXXXXX \
--role-arn arn:aws:iam::123456789012:role/role01 \
--vpc-subnet-ids "subnet-11111111111111111" "subnet-22222222222222222" "subnet-33333333333333333"

aws rds register-db-proxy-targets \
--db-proxy-name proxy01 \
--target-group-name default \
--db-cluster-identifiers cluster01


-- 4.RDS Proxy の表示
aws rds describe-db-proxies
aws rds describe-db-proxies --query '*[*].{DBProxyName:DBProxyName,Endpoint:Endpoint}'

aws rds describe-db-proxy-targets --db-proxy-name proxy01


-- 5.RDS Proxy を介したデータベースへの接続
mysql -h proxy01.proxy-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -P 3306 -u root -p


-- 6.RDS Proxy の削除

aws rds delete-db-proxy --db-proxy-name proxy01

-- 7. ロールの削除

-- ロールの一覧
aws iam list-roles | grep role01

-- ロールの削除

aws iam delete-role-policy \
--role-name role01 \
--policy-name policy01

aws iam delete-role --role-name role01


-- 8. シークレットの削除


-- シークレットの一覧
aws secretsmanager list-secrets

-- シークレットの削除
aws secretsmanager delete-secret --secret-id secret01