Skip to main content

Command Palette

Search for a command to run...

Enabling IAM DB Authentication for RDS in AWS

Updated
4 min read
Enabling IAM DB Authentication for RDS in AWS
G

I am a Software Developer from Italy.

When working with Amazon RDS, securing database access is essential. Traditionally, databases are accessed using static usernames and passwords. However, managing these credentials at scale can become complex and potentially insecure. This is where IAM DB Authentication comes in.

IAM DB Authentication allows you to use AWS Identity and Access Management (IAM) to authenticate your applications and users to your Amazon RDS MySQL and PostgreSQL databases. Instead of hardcoded credentials, you can use temporary, short-lived tokens for database access.

In this blog post, we’ll explore what IAM DB Authentication is, its benefits, and how to enable it step by step.

What is IAM DB Authentication?

IAM DB Authentication provides an alternative to traditional user authentication by allowing you to authenticate your database connections using IAM roles and policies instead of static passwords.

  • Token-based authentication: IAM DB Authentication issues a token that lasts for 15 minutes. The token replaces the need for a password.

  • Simplified credential management: You can control database access using IAM policies without managing credentials directly within the database.

Benefits of IAM DB Authentication

  1. Enhanced Security:

    • No more hardcoding database credentials in your application code.

    • Tokens are time-limited and automatically expire, reducing exposure if they are compromised.

  2. Centralized Access Control:

    • Access control is managed through IAM policies, giving you centralized and consistent management across AWS services.
  3. Integration with AWS Services:

    • Easily integrates with other AWS services like AWS Lambda, EC2, and AWS Secrets Manager for seamless authentication workflows.
  4. Auditability:

    • You can use AWS CloudTrail to monitor and log access attempts to your RDS databases.

Prerequisites for IAM DB Authentication

Before enabling IAM DB Authentication, ensure the following:

  • You are using Amazon RDS MySQL or Amazon RDS PostgreSQL, as IAM DB Authentication is currently supported for these databases.

  • Your database version is compatible:

    • MySQL 5.6 or later

    • PostgreSQL 9.5 or later

  • You have an IAM Role with the appropriate permissions.

How to Enable IAM DB Authentication

Step 1: Modify Your RDS Instance to Enable IAM Authentication

  1. Log in to the AWS Management Console and go to the RDS dashboard.

  2. Find the DB instance you want to enable IAM DB Authentication for.

  3. Select the instance, click on Modify, and scroll down to the Database options section.

  4. Set IAM DB Authentication to Enabled.

  5. Review your settings and click Continue.

  6. Choose whether you want the change to be applied immediately or during the next maintenance window, then click Modify DB Instance.

Step 2: Configure IAM Role and Permissions

Next, you need to grant the necessary permissions for users or applications to connect to your RDS instance.

  1. Go to the IAM console and create a new IAM policy. Add the following permission to the policy to allow IAM authentication:

     {
       "Version": "2012-10-17",
       "Statement": [
         {
           "Effect": "Allow",
           "Action": [
             "rds-db:connect"
           ],
           "Resource": "arn:aws:rds-db:<region>:<account>:dbuser:<db-instance-id>/<db-username>"
         }
       ]
     }
    
  2. Attach this policy to an IAM role or user that will be used to connect to the database.

Step 3: Create a MySQL/PostgreSQL Database User with IAM Authentication

Now, create a database user in your RDS instance that will use IAM authentication:

  1. Connect to your database (e.g., using the MySQL command-line client or PostgreSQL).

  2. Run the following SQL commands to create a user with the necessary permissions:

    For MySQL:

     CREATE USER 'db_user'@'%' IDENTIFIED WITH AWSAuthenticationPlugin as 'RDS';
     GRANT ALL PRIVILEGES ON *.* TO 'db_user'@'%';
    

    For PostgreSQL:

     CREATE ROLE db_user WITH LOGIN;
     GRANT rds_iam TO db_user;
    

Step 4: Generate an IAM Token for Database Authentication

Now that IAM DB Authentication is enabled, you'll need to use the AWS CLI or an AWS SDK to generate an authentication token when connecting to the database.

For example, to generate a token using the AWS CLI:

aws rds generate-db-auth-token \
    --hostname <db-instance-endpoint> \
    --port 3306 \
    --username db_user \
    --region <your-region>

This command returns a token string, which you can use as the password when connecting to your database.

Step 5: Connect to the RDS Instance Using the IAM Token

Finally, you can connect to your database using the token as the password.

For example, to connect using MySQL:

mysql -h <db-instance-endpoint> \
    -P 3306 \
    -u db_user \
    --enable-cleartext-plugin \
    -p

You will be prompted to enter a password. Paste the generated IAM token here.

For PostgreSQL, you can similarly use the token as the password when connecting.

Best Practices

  • Use IAM roles: Assign IAM roles to AWS resources (e.g., EC2, Lambda) to authenticate automatically.

  • Use CloudTrail for Auditing: Monitor access attempts and ensure that only authorized users are granted access.

  • Rotate IAM tokens frequently: Since IAM DB Authentication tokens are valid for only 15 minutes, make sure your application handles token generation efficiently.

Implementing IAM DB Authentication in your AWS infrastructure provides a robust, scalable solution for managing secure database access. Start today and take your RDS security to the next level!

More from this blog

D

Dev Diaries

42 posts