Lesson 1 of 3 Secrets and Sensitive Data

Storing and Rotating Secrets with Secrets Manager and Parameter Store

This lesson explains the two AWS services for storing secrets, such as database passwords and API keys, which must never be hard-coded or left unrotated, and how their features differ.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between Secrets Manager and Parameter Store.
  • Describe the four steps of rotation and the role of staging labels.
  • Choose a rotation strategy and explain the network access that rotation functions need.

Secrets Manager or Parameter Store

Both services store sensitive values, but Secrets Manager is built for secrets. Secrets Manager encrypts each secret with KMS and rotates it automatically on a schedule. Resource policies allow access from other accounts, and secrets can be replicated to other Regions, each replica encrypted with a local key. It charges per secret and per API call.

Parameter Store, part of Systems Manager, stores configuration values in a hierarchy, with SecureString parameters encrypted by KMS. It has no built-in rotation (advanced parameters can send expiration notifications), access is through IAM only, and standard parameters cost nothing. A requirement for automatic rotation, cross-account sharing or multi-Region replication points to Secrets Manager. The following table summarises the differences.

Secrets Manager
StoresSecrets, KMS-encrypted
RotationAutomatic, scheduled
AccessIAM and resource policies, including cross-account
Multi-RegionReplication, each replica with a local key
CostPer secret and per API call
Parameter Store
StoresConfiguration and SecureString parameters, in a hierarchy
RotationNone built in (advanced parameters have expiration notifications)
AccessIAM
Multi-Region—
CostLittle or none

Applications retrieve values at runtime through the SDK, usually with client-side caching.

Sort into groups

Secrets Manager or Parameter Store?

Exam skills 5.3.1

Choose the service that meets each requirement.

Log in to try this activity Interactive activities are free with an account.

How rotation works

Lambda rotation functions (templates for RDS, Aurora, Redshift, DocumentDB) follow four steps, create, set, test, finish, using staging labels.

Staging labels mark the versions of a secret so that applications are never left without a working credential. In the create step, the function generates a new value and stores it as a new version labelled AWSPENDING. In the set step, it changes the password in the database, and in the test step, it signs in with the new value. In the finish step, it moves AWSCURRENT, the label applications read, to the new version. The old version becomes AWSPREVIOUS, kept so that it can be restored if something goes wrong. The following table summarises the labels.

LabelMeaning
AWSPENDINGNew value during rotation
AWSCURRENTActive value
AWSPREVIOUSLast value

Managed rotation (e.g. RDS ManageMasterUserPassword) needs no Lambda function.

The following diagram shows the three parts of removing a hard-coded database password from an application.

Three parts to removing a hard-coded database password. RDS manages the master password in Secrets Manager with AWS-run rotation; the application reads it at run time through a caching client, so rotation needs no redeploy; and the leaked value is treated as compromised and purged from history.
The scenario is illustrative.
Put in order

The four rotation steps

Exam skills 5.3.1

Arrange the steps of a Secrets Manager rotation function.

Log in to try this activity Interactive activities are free with an account.

Rotation strategies and networking

Secrets Manager supports two rotation strategies, and rotation functions require network access. The following list describes both aspects.

  • Single user: changes one user's password; brief risk of failed connections.
  • Alternating users: switches between two users so a valid credential always exists.
  • Rotation functions in a VPC need network access to the database and Secrets Manager (interface endpoint or NAT).
Troubleshooting

Rotation that fails

Exam skills 5.3.1

Find the fix for each rotation problem.

Log in to try this activity Interactive activities are free with an account.
Worked example

Removing a hard-coded database password

A code review finds a database password in an application's configuration file. The fix has three parts. First, RDS manages the master password in Secrets Manager, with rotation handled by AWS:

aws rds modify-db-instance --db-instance-identifier orders \
  --manage-master-user-password --master-user-secret-kms-key-id alias/orders-secrets --apply-immediately

Second, the application reads the secret at runtime and caches it, so rotation never requires a redeploy:

from aws_secretsmanager_caching import SecretCache
import json

cache = SecretCache()

def db_credentials():
    secret = json.loads(cache.get_secret_string("rds!db-0a1b2c3d-orders"))
    return secret["username"], secret["password"]

Third, the leaked password is treated as compromised: the old value is already invalid after the first rotation, and the repository history is purged. Inspector's Lambda code scanning and pipeline secret detection catch the next attempt to commit a credential.

Check yourself

Which rotation strategy avoids downtime during rotation?

Show answer

The alternating users strategy, which switches between two database users.

Exam Tips

Remember for the exam

  • To rotate database credentials automatically, use Secrets Manager.
  • To rotate RDS master passwords with the least effort, use managed rotation.
  • To avoid downtime during rotation, use the alternating users strategy.
  • When secrets are needed in a disaster recovery Region, use Secrets Manager replication.
  • A rotation function that times out in a VPC lacks a route or endpoint to Secrets Manager or the database.
  • For cheap configuration storage without rotation, use Parameter Store.