Storing and Rotating Credentials with AWS Secrets Manager
This lesson explains how Secrets Manager stores credentials such as database passwords and API keys, encrypts them with KMS and rotates them automatically, so applications never hard-code secrets.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the features of Secrets Manager.
- Explain how rotation works.
- Describe how applications and services retrieve secrets.
Features
Secrets Manager stores credentials, such as database passwords and API keys, so they are not kept in code or configuration files. Every secret is encrypted with a KMS key, either the AWS managed key or a customer managed key that you control. Its most important feature is automatic rotation: on a schedule, a rotation function creates a new credential, updates it in the database or service and stores it, while applications continue to read the current version. Rotation is built in for RDS, Aurora, Redshift and DocumentDB, and a custom Lambda function handles any other credential.
Secrets can be replicated to other Regions, so applications there, or in a disaster recovery Region, read a local copy. Cross-account access needs a resource policy on the secret and permission to use its customer managed KMS key, because the AWS managed key cannot be shared. Every retrieval is logged in CloudTrail. Secrets are charged per secret per month plus API calls. The following table summarises the features.
| Feature | Detail |
|---|---|
| Encryption | Every secret is encrypted with a KMS key, AWS managed or customer managed |
| Automatic rotation | Native for RDS, Aurora, Redshift and DocumentDB; Lambda rotation functions for anything else |
| Replication | Replica secrets in other Regions for disaster recovery and multi-Region apps |
| Cross-account access | A resource policy on the secret plus access to its customer managed KMS key |
| Auditing | Every retrieval is logged in CloudTrail |
| Cost | Per secret per month plus API calls |
How rotation works
Secrets Manager rotates a secret by means of a Lambda function that proceeds through a fixed sequence of steps. The steps are as follows.
-
Create
Create a new credential, labelled
AWSPENDING. -
Set
Set it on the database or service.
-
Test
Check that the new credential works.
-
Finish
Mark it current (
AWSCURRENT); the old one becomesAWSPREVIOUS.
RDS and Aurora can also manage the master password in Secrets Manager themselves. Applications that fetch the secret at run time pick up new values without redeploying, so never copy secrets into code, AMIs or images.
The following diagram shows the four rotation steps and the labels they move.

Rotating a secret
Getting secrets into applications
Applications and services retrieve secrets from Secrets Manager in several ways, so that no secret is written into code or configuration. Applications call GetSecretValue with their IAM role, ideally through a caching client that keeps the value in memory for a period and reduces cost and latency.
Several services integrate directly. Amazon ECS task definitions inject secrets into containers as environment variables through the secrets field. Lambda functions read them through the AWS Parameters and Secrets Lambda Extension, which caches values. AWS CloudFormation dynamic references, written as {{resolve:secretsmanager:...}}, pass secrets to resources such as database instances without exposing them in templates.
Getting secrets in
Rotating a database password without downtime
An Aurora database password is stored in an application's configuration file and has not changed in three years. The security team requires 30-day rotation and a copy in the DR Region.
| Step | Action |
|---|---|
| 1 | Store the credentials in Secrets Manager with managed rotation every 30 days |
| 2 | Change the application to read the secret at runtime, with a caching client |
| 3 | Replicate the secret to the DR Region |
| 4 | Remove the password from the configuration file |
Rotation creates and tests the new password before marking it current, and the caching client picks it up within its refresh interval, so the application never fails to connect.
How can applications in a disaster recovery Region read the same secret?
Show answer
With multi-Region secrets: Secrets Manager replicates the secret to the chosen Regions and keeps the replicas in sync, including after rotation.
Exam Tips
Remember for the exam
- For automatic rotation of credentials, use Secrets Manager.
- To make secrets available in a disaster recovery Region, use Secrets Manager replication.
- To share a secret with another account, use a resource policy on the secret plus access to its customer managed KMS key.
- So that applications pick up rotated passwords automatically, retrieve the secret at run time, with caching.