Automating Credential Rotation for Machine Identities
Some machine credentials cannot be replaced by roles: database passwords, API keys for third-party services, and occasional IAM access keys for systems that support nothing else. These secrets must be rotated regularly and automatically, without downtime. This lesson explains rotation in AWS Secrets Manager, rotation strategies and functions, managed rotation, and how other credentials such as access keys and certificates are rotated or eliminated.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how Secrets Manager rotation works, including versions, staging labels and the four rotation steps.
- Distinguish between the single-user and alternating-users rotation strategies and managed rotation.
- Describe how applications retrieve rotated secrets without downtime.
- Describe automated detection and rotation of IAM access keys and certificates, and when to eliminate them instead.
How Secrets Manager Rotation Works
A secret in Secrets Manager has versions, and staging labels identify their roles: AWSCURRENT is the version applications use, AWSPENDING is a new version during rotation, and AWSPREVIOUS is the last version. Rotation is configured with a schedule, such as every 30 days or a cron expression, and either managed rotation or a Lambda rotation function. A rotation can also be started immediately with RotateSecret.
A rotation function is invoked four times, once for each step, explained below.
- createSecret: generate a new credential and store it as a new version labelled AWSPENDING.
- setSecret: change the credential in the target system, such as setting the new password in the database.
- testSecret: verify that the AWSPENDING credential works.
- finishSecret: move the AWSCURRENT label to the new version, which labels the old one AWSPREVIOUS.
AWS provides rotation function templates for Amazon RDS, Aurora, Redshift, DocumentDB and other databases, which can be deployed from the Serverless Application Repository or created automatically from the console. Custom functions rotate other kinds of secret, such as API keys of third-party services. The function needs network access to both Secrets Manager (through an endpoint or NAT) and the target system, which means placing it in the VPC with the database, and its role needs permissions on the secret and the KMS key.
The following diagram shows the four steps and the version labels they set.

A rotation run
Rotation Strategies and Managed Rotation
Two strategies handle the moment when the credential changes.
Single user
One database user's password is changed in place. It is simple, but connections opened with the old password between setSecret and the application retrieving the new value can fail, so applications must retry with a fresh secret value on authentication failure. It suits applications that tolerate brief retries.
Alternating users
Two users exist with the same permissions; each rotation updates the user that is not currently in use and then switches AWSCURRENT to it. The previous user's credentials remain valid until the next rotation, so applications that cached the previous value keep working. It needs a separate superuser secret that the rotation function uses to change the other users' passwords, and suits high-availability applications.
Managed rotation
For some services, rotation needs no function at all. Amazon RDS and Aurora can manage the master user password in Secrets Manager (ManageMasterUserPassword), with RDS creating and rotating the secret automatically. Other services offer similar managed secrets. Managed rotation is the lowest-overhead option when available.
The following diagram compares the two strategies across several rotations.

Choose the rotation strategy
Consuming Rotated Secrets
Rotation is only safe if applications always use the current secret. Applications should retrieve the secret at run time and cache it for a limited time, refreshing on authentication failure, rather than reading it once at start-up. AWS provides caching clients for several languages, the AWS Parameters and Secrets Lambda Extension for Lambda, and the Secrets Manager Agent for other compute. ECS tasks receive secrets at start-up, so a rotation should be followed by a service deployment, or the application should read the secret itself. CloudFormation dynamic references resolve at deployment time, so they suit values consumed by resources, such as the RDS master password, rather than application credentials that rotate.
Rotation events, such as RotationSucceeded and RotationFailed, are published to EventBridge through CloudTrail, so failed rotations can alert the team. The Config rules secretsmanager-rotation-enabled-check, secretsmanager-scheduled-rotation-success-check and secretsmanager-secret-unused verify rotation across accounts.
IAM Access Keys and Certificates
IAM access keys should be eliminated wherever possible with the techniques in the previous lesson. Where they must remain, rotation is automated: the AWS Config managed rule access-keys-rotated reports keys older than a maximum age, and a remediation, such as an Automation runbook or a Lambda function, creates a new key, stores it in Secrets Manager for the consuming system, and deactivates and later deletes the old key. Each IAM user can have two access keys at a time, which allows a new key to be distributed before the old one is deactivated. Unused keys should be deactivated, which IAM Access Analyzer unused access findings identify.
Certificates are also credentials. Public and private certificates issued by AWS Certificate Manager renew automatically when they are in use and DNS validation is in place, as described in the data protection module. Imported certificates do not renew automatically; ACM emits expiry events and the Config rule acm-certificate-expiration-check detects certificates close to expiry. AWS KMS keys rotate their key material automatically when rotation is enabled.
| Credential | Preferred approach | If it must remain |
|---|---|---|
| Database password | Secrets Manager managed rotation or rotation function | Not applicable |
| Third-party API key | Secrets Manager with a custom rotation function | Not applicable |
| IAM access key | Replace with roles, OIDC or Roles Anywhere | access-keys-rotated rule with automated rotation |
| ACM certificate | ACM-issued with DNS validation (automatic renewal) | Imported: monitor expiry and reimport |
| KMS key material | Automatic rotation | On-demand rotation |
Rotating Database Credentials Without Downtime
A high-traffic service connects to Aurora PostgreSQL with a password stored in a configuration file, and the security team requires rotation every 30 days without errors during rotation.
-
Master password
Set ManageMasterUserPassword on the cluster so RDS manages and rotates the master password.
-
Application user
Create an application secret with alternating-users rotation every 30 days, using the master secret as the superuser secret, with the rotation function in the database VPC.
-
Consumption
Change the application to read the secret through a caching client with a five-minute refresh and a retry on authentication failure.
-
Monitoring
Alert on RotationFailed events and deploy the secretsmanager rotation Config rules.
A rotation function times out during the setSecret step for a database in a private subnet. What is the likely cause?
Show answer
The function cannot reach the database or Secrets Manager: it must run in the VPC with security group access to the database and have a route to Secrets Manager through an interface endpoint or NAT gateway.
Exam Tips
Remember for the exam
- Secrets Manager rotation steps: createSecret, setSecret, testSecret, finishSecret; labels AWSPENDING, AWSCURRENT, AWSPREVIOUS.
- Alternating users avoids failures during rotation; single user needs application retries; managed rotation (such as RDS ManageMasterUserPassword) needs no function.
- Rotation functions need network access to both the target and Secrets Manager.
- Applications should fetch and cache secrets with refresh; ECS tasks need redeployment to pick up rotated secrets injected at start-up.
- Detect old access keys with access-keys-rotated and rotate automatically, or better, replace keys with roles.