AWS Fundamentals Series

🔐 The Top 1% Hands-On for Never Letting an App Write a Password With RDS and Secrets Manager: Eliminating Hardcoded DB Credentials

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 6th article in the AWS Fundamentals Series.

Prerequisite Knowledge

The Big Picture

This hands-on consists of four steps.

graph LR
    Step1["Step1<br/>Build RDS in a<br/>private subnet"]
    Step2["Step2<br/>Store the password<br/>in Secrets Manager"]
    Step3["Step3<br/>Grant fetch permission<br/>to the EC2 IAM role"]
    Step4["Step4<br/>Fetch it at runtime<br/>from the app and connect"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Build RDS in a private subnet

Create a MySQL-compatible RDS instance in the private subnet built previously.

aws rds create-db-instance \
  --db-instance-identifier my-app-db \
  --db-instance-class db.t3.micro \
  --engine mysql \
  --master-username admin \
  --master-user-password "TemporaryP@ss123!" \
  --allocated-storage 20 \
  --db-subnet-group-name my-private-subnet-group \
  --no-publicly-accessible

Notice the explicit --no-publicly-accessible flag. Never giving RDS a public IP, so it’s only reachable from within the private subnet, is a baseline assumption for production operation. The master-user-password specified here is just the initial value at creation time — you’ll switch to Secrets Manager-based operation right after this.

Step 2: Store the password in Secrets Manager

Store the full set of information needed for the DB connection as a Secrets Manager secret.

aws secretsmanager create-secret \
  --name my-app-db-credentials \
  --secret-string '{"username":"admin","password":"TemporaryP@ss123!","host":"my-app-db.xxxx.ap-northeast-1.rds.amazonaws.com","port":3306}'

At this point, the password never appears in the application’s code or config files at all. All the app needs is “permission to fetch information from this secret name” — not the password value itself.

Step 3: Grant fetch permission to the EC2 IAM role

Grant the IAM role attached to the app’s EC2 instance permission to read only this one secret.

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": "secretsmanager:GetSecretValue",
    "Resource": "arn:aws:secretsmanager:ap-northeast-1:123456789012:secret:my-app-db-credentials-*"
  }]
}

Scoping Resource down to just this one secret’s ARN is the important part. Use a wildcard covering every secret instead, and if this EC2 instance is ever compromised, another app’s DB password would be readable too.

Step 4: Fetch it at runtime from the app and connect

At app startup, fetch this secret from Secrets Manager via the SDK and use it for the DB connection.

import boto3
import json
import pymysql

client = boto3.client("secretsmanager", region_name="ap-northeast-1")
secret = json.loads(client.get_secret_value(SecretId="my-app-db-credentials")["SecretString"])

conn = pymysql.connect(
    host=secret["host"], user=secret["username"],
    password=secret["password"], port=secret["port"]
)

This code contains neither an access key nor a DB password. boto3.client automatically uses the temporary credentials from the IAM role attached to the EC2 instance, and only through those credentials does it fetch the DB password from Secrets Manager for the first time.

What a Pro Sees Here (Top 1% Understanding)

Password rotation becomes possible with zero code changes

Secrets Manager can periodically and automatically rotate (swap in a new value for) a stored secret. Combined with RDS, a Lambda function automatically keeps the password change on the RDS side and the secret update on the Secrets Manager side in sync. The app’s code stays exactly as-is — “fetch from Secrets Manager at runtime” — with zero changes needed, and the app doesn’t even need to notice the password quietly rotating underneath it. This shows that the same design philosophy behind rotating access keys periodically — shortening a credential’s lifetime to narrow its exposure window — applies just as well to an entirely different kind of credential: a password.

Why RDS belongs in a private subnet

As covered in The Top 1% Hands-On for Building a VPC With Public/Private Subnets Yourself, a private subnet is one that “can’t have a connection initiated into it from outside.” A database like RDS has no legitimate reason to accept connections from anything other than its proper application server, so placing it in a private subnet with public access fully disabled is the standard real-world design. Leaving RDS publicly accessible “because it’s easier during development” and carrying that straight into production is a serious, recurring design mistake in real-world work.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. The app’s access to Secrets Manager gets AccessDenied: Check whether the IAM role attached to the EC2 instance has secretsmanager:GetSecretValue permission for that secret’s ARN.
  2. The app’s connection to RDS times out: Check whether RDS and EC2 are in the same VPC, and whether RDS’s security group permits the connecting IP or security group from EC2.
  3. After password rotation, the app fails to connect with an old password: Check whether the app is caching the secret instead of re-fetching it, and whether it’s designed to re-fetch on every rotation.

Summary

Takeaways to Apply Today

  1. If an existing app writes a DB password into an environment variable or config file, consider migrating it to fetch from Secrets Manager instead.
  2. When granting permissions to an IAM role, always scope it down to just the minimum resources that role genuinely needs.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).