How to Connect to My Amazon RDS for PostgreSQL Using IAM Authentication

AWS allows the user to create DB instances using the Amazon RDS dashboard and connect them to the pgAdmin or other clients. It also allows the user to create a secure connection using either a user password or an IAM authentication key. The key is usable for only 15 minutes and after that, the user needs to create a new key to be able to connect to the server.

This guide will explain the use of IAM credentials to connect the AWS RDS PostgreSQL database.

How to Connect PostgreSQL AWS RDS Database Using IAM Authentication?

To connect to the IAM identification, visit the RDS dashboard from the AWS Management Console:

img

After that, head into the database instance by clicking on its name:

img

Head into the “Configuration” section from the database page:

img

Make sure that the IAM DB authentication is “Enabled”:

img

Visit the IAM dashboard from the AWS dashboard:

img

Click on the “Roles” page from the left panel:

img

Create an IAM role for the RDS:

img

Use the following policy for the IAM role by simply changing the resource section containing the Account ID and DB Resource ID:

{
  "Version": "2012-10-17",
  "Statement": [
  {
  "Effect": "Allow",
  "Action": [
  "rds-db:connect"
  ],
  "Resource": [
  "arn:aws:rds-db:ap-southeast-1:***********3:dbuser:<DB   Resource ID>/Master <Username>"
  ]
  }
  ]
   }

After that, visit the “Connectivity & security” section and copy the Endpoint:

img

Use the following command by changing the username and endpoint copied from the RDS page:

psql -h database-1.c6d50j4forkq.ap-southeast-1.rds.amazonaws.com -U postgres
img

Create a new user named “iamuser” using the LOGIN keyword:

CREATE USER iamuser WITH   LOGIN;

After that, grant the IAM role to the user created previously:

GRANT rds_iam TO iamuser;

The role has been created and attached to the user:

img

Visit the EC2 dashboard and create an EC2 instance:

img

Select the instance and expand the “Actions” menu, hover over the “Security” list, and select the “Modify IAM role” button:

img

Select the role and click on the “Update IAM role” button:

img

Once the role is attached to the instance, select it and click on the “Connect” button:

img

Copy the command provided in the “SSH client” section:

img

Paste the command on the terminal and change the path of the key pair file:

img

Use the following commands to get the password used to connect to the PostgreSQL database:

export RDSHOST="aurorapg-ssl.cluster-XXXXXXXXXXX.us-west-2.rds.amazonaws.com"
 export PGPASSWORD="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region   ap-southeast-1 --username iamuser)"
   echo $PGPASSWORD

The above command will provide the SSL token password:

img

Open the pgAdmin client from the local system and click on the “Add New Server” button:

img

Type the name of the server and turn off the “Connect now” button from the “General” tab:

img

Head into the “Connection” tab to type the Hostname and username of the DB instance from AWS RDS:

img

Visit the “Parameter” page to change the SSL mode value to “require” and click on the “Save” button:

img

Execute the database server and paste the password obtained from connecting to the EC2 instance and click on the “OK” button:

img

The connection has been established successfully:

img

That’s all about connecting to the AWS RDS PostgreSQL database using IAM authentication.

Conclusion

To connect to the DB instance using IAM authentication, create an RDS database instance with IAM authentication enabled. Create an IAM role with a policy allowing the DB connection using IAM and then attach it to the EC2 instance. Connect to the EC2 instance using its command from the SSH client and get the password from there. Use the obtained password to connect to the AWS RDS on the pgAdmin client application from the local system.