Help us understand the problem. What is going on with this article?

RDSとIAMでパスワードレス認証(Python)

MySQLのパスワードベタ書きしないでよくなります
{}内は適宜置き換えてね

1. ユーザ作成

CREATE USER '{MySQLユーザ名}'@'%' IDENTIFIED WITH AWSAuthenticationPlugin as 'RDS'; # ユーザ作成
GRANT ALL PRIVILEGES ON {MySQLDB名}.* TO '{MySQLユーザ名}'@'%' REQUIRE SSL; # 権限

2. IAMポリシー

こんな感じでポリシーを作る

jsonで直接アタッチするならこんな感じ

{
    "Effect": "Allow",
    "Action": "rds-db:connect",
    "Resource": "arn:aws:rds-db:{AWS::Region}:{AWS::AccountId}:dbuser:cluster-{リソースID}/{MySQLユーザ名}",
}

CloudFormationならこんな感じ

iamRole:
    - Effect: Allow
        Action:
        - "rds-db:connect"
        Resource:
        - 'Fn::Join':
            - ':'
            - - 'arn:aws:rds-db'
                - Ref: 'AWS::Region'
                - Ref: 'AWS::AccountId'
                - 'dbuser:cluster-{リソースID}/{MySQLユーザ名}'

3. 一時パスワードで接続!

こんな感じでトークン(一時パスワード)を発行する

client = boto3.client("rds")

# トークン取得
token = client.generate_db_auth_token(
    DBHostname=RDS_HOST,
    Port=RDS_PORT,
    DBUsername=RDS_USER_NAME
)

db_connection = mysql.connector.connect(
    host=RDS_HOST,
    port=RDS_PORT,
    database=RDS_DB_NAME,
    user=RDS_USER_NAME,
    passwd=token
)

たったこんだけ
かんたんにゃ〜〜

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした