AWS Secrets Manager

Overview

AWS Secrets Manager stores credentials so that you can replace a hard-coded password with an API call that returns it. Because you chose Managed in AWS Secrets Manager when you created rdspg-dev-db, RDS created the secret for you, keeps it in sync with the database, and rotates the password automatically.

Self managed password Managed in Secrets Manager
Who creates the password You RDS
Where it is kept Wherever you put it (config file, environment variable, wiki…) A secret named rds!db-<id>, encrypted with AWS KMS
Rotation Manual Automatic, every 7 days by default
How the application gets it Copy it into the configuration GetSecretValue, allowed by IAM

Look at the secret in the console

  1. Open the AWS Secrets Manager console:

    • (1) The secret rds!db-<id> was created by RDS. The name is not something you choose.
    • (2) Its description links it to the ARN of the DB instance rdspg-dev-db.

    Secrets

  2. Choose the secret:

    • (1) The banner says that the secret is managed by Amazon RDS: you cannot change its value yourself, so the password in the secret and the password in the database cannot get out of sync.
    • (2) The secret is encrypted with the KMS key aws/secretsmanager.
    • (3) Open the Rotation tab.

    Secret details

  3. In the Rotation tab:

    • (1) Rotation status is Enabled.
    • (2) Rotation schedule is 7 days.
    • (3) Next rotation date shows when RDS changes the password next.

    Rotation

Because the password changes every week, an application must not read it once and keep it forever. In chapter 3 the code reads the secret when it opens a new connection, with a short cache, so a rotation does not break it.

Read the secret from the dev server

  1. In the Session Manager terminal (as ec2-user), find the ARN of the secret from the DB instance, then read the secret. The command prints only the user name:

    SECRET_ARN=$(aws rds describe-db-instances --db-instance-identifier rdspg-dev-db \
        --query 'DBInstances[0].MasterUserSecret.SecretArn' --output text)
    aws secretsmanager get-secret-value --secret-id "$SECRET_ARN" --query SecretString --output text \
        | jq '{username, password: "(hidden)"}'
    aws secretsmanager list-secrets 2>&1 | cut -c1-110
    
    • (1) The role of the server can read the secret: it contains username (postgres) and password.
    • (2) list-secrets fails with AccessDeniedException: the role can read only the RDS-managed secrets, nothing else.

    Read the secret

MasterUserSecret.SecretArn is the reliable way to find the secret: the name rds!db-<id> is generated by RDS and changes if you recreate the DB instance. Never print the password in logs or terminal output.

(Optional) AWS CLI

Turn on the RDS-managed secret for an existing DB instance