RDS Database에 접속할 때 암호를 사용해도 되지만, IAM 인증을 사용해도 됩니다. IAM 인증에서는 RDS가 생성하는 인증 토큰을 사용하며 각 토큰의 수명은 15분입니다. SSL 또는 TLS를 사용하여 DB 인스턴스에 대한 연결을 암호화합니다. 오늘은 EC2 Instance에서 RDS MySQL Instance로 IAM 인증을 사용하여 접속해봅시다.
1. 준비
RDS Database를 생성할 때나 Database Modify를 통해 IAM database authentication 옵션에 체크합니다.
EC2와 연결되는 IAM Role에 다음과 같은 Policy가 있어야 합니다.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds-db:connect"
],
"Resource": [
"arn:aws:rds-db:(Region):(Account ID):dbuser:*/*"
]
}
]
}
Database에 User 설정을 해주기 위해 암호를 통해 접속합니다.
mysql -h (endpoint) -u (master db username) -p
[ec2-user@ip-10-0-0-124 ~]$ mysql -h skills-db.cluster-cacgnhyyutg6.ap-northeast-2.rds.amazonaws.com -u admin -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 242
Server version: 8.0.28 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
IAM 인증 접속을 허용하는 'rds_iam'이라는 이름의 User을 생성합니다. SSL 접속도 허용하도록 설정합니다.
CREATE USER rds_iam IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';
ALTER USER 'rds_iam'@'%' REQUIRE SSL;
2. 접속
MySQL에서 빠져나와 인증 토큰을 발급합니다.
RDSHOST="(DB Instance Hostname)"
TOKEN="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 3306 --username rds_iam)"
[ec2-user@ip-10-0-0-124 ~]$ RDSHOST="skills-db-instance-1.cacgnhyyutg6.ap-northeast-2.rds.amazonaws.com"
[ec2-user@ip-10-0-0-124 ~]$ TOKEN="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 3306 --username rds_iam)"
[ec2-user@ip-10-0-0-124 ~]$ echo $TOKEN
skills-db-instance-1.cacgnhyyutg6.ap-northeast-2.rds.amazonaws.com:3306/?Action=connect&DBUser=rds_iam&X-Amz-Algorithm=
...
SSL 접속을 위해 모든 리전에서 작동하는 루트 인증서를 다운로드합니다.
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
다음 명령어를 통해 IAM 인증을 사용해 접속합니다.
mysql --host=$RDSHOST --ssl-ca=./global-bundle.pem --enable-cleartext-plugin --user=rds_iam --password=$TOKEN
성공적으로 접속한 것을 볼 수 있습니다.
[ec2-user@ip-10-0-0-124 ~]$ mysql --host=$RDSHOST --ssl-ca=./global-bundle.pem --enable-cleartext-plugin --user=rds_iam --password=$TOKEN
WARNING: option '--enable-cleartext-plugin' is obsolete.
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 421
Server version: 8.0.28 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
오늘의 글은 여기까지입니다. 감사합니다!
'AWS' 카테고리의 다른 글
[AWS] Managed Service for Apache Flink로 스트리밍 데이터 분석 - Studio notebooks (1) (0) | 2023.10.13 |
---|---|
[AWS] Athena에서 Table 생성부터 데이터 조회까지 (0) | 2023.10.12 |
[AWS] Kinesis Agent로 Kinesis Data Stream에 로그 전송 (2) | 2023.10.10 |
[AWS] CloudFront with Lambda Function URL (1) | 2023.10.09 |
[AWS] Terraform VPC Module 사용 (0) | 2023.10.06 |