2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RDS IAM認証を試す

Posted at

概要

IAM 認証を使用して DB インスタンスへ接続する。

  • パスワード無しでログイン。
  • ユーザごとにアクセスできる DB を制限する。

手順

RDS DB インスタンスで IAM DB 認証をアクティブ化

「パスワードと IAM データベース認証」にチェックして、RDS を構成。
スクリーンショット 2023-03-27 18.12.29.png

AWS 認証トークンを使用するデータベースユーザーアカウントを作成します。

まず、通常通り「パスワード認証」で、EC2 から RDS にログイン。

mysql -h {エンドポイント} -P 3306 -u admin -p

ユーザー(iam_db_user)を作成。

CREATE USER iam_db_user  IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';

ユーザー(iam_db_user)が作成されているか確認。

select Host, User from mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | admin            |
| %         | iam_db_user      |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | rdsadmin         |
+-----------+------------------+
6 rows in set (0.02 sec)

DB(iam_db) の作成。

create database iam_db;

DB(iam_db) が作成されているか確認。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| iam_db             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.03 sec)

iam_db_user が iam_db にログインできるように権限を付与。

GRANT ALL PRIVILEGES ON iam_db.* TO 'iam_db_user '@'%';

アクセストークン取得用IAMポリシー、ロールの作成とアタッチ

  • データベースユーザーを IAM ロールにマップする IAM ポリシーを作成。

rds_許可
{
   "Version": "2012-10-17",
   "Statement": [
      {
         "Effect": "Allow",
         "Action": [
             "rds-db:connect"
         ],
         "Resource": [
             "arn:aws:rds-db:ap-northeast-1:[アカウントID]:dbuser:[RDSのリソースID]/iam_db_user"
         ]
      }
   ]
}
  • Amazon EC2 から Amazon RDS へのアクセスを許可する IAM ロールを作成。
ec2_信頼関係
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "ec2.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
  • IAM ロールを Amazon EC2 インスタンスにアタッチ。

ログイン確認

-p 以下で認証トークンを取ってくる。

mysql -u iam_db_user -h {エンドポイント} -p`aws rds generate-db-auth-token --hostname {エンドポイント} --port 3306 --username iam_db_user --region ap-northeast-1` --enable-cleartext-plugin
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?