Lesson 4 of 4 Identity and Access Management at Scale

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.

  1. createSecret: generate a new credential and store it as a new version labelled AWSPENDING.
  2. setSecret: change the credential in the target system, such as setting the new password in the database.
  3. testSecret: verify that the AWSPENDING credential works.
  4. 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.

The four steps of a Secrets Manager rotation function. createSecret generates a new credential and stores it as a new version labelled AWSPENDING. setSecret changes the credential in the target system, such as the database. testSecret verifies that the pending credential works. finishSecret moves the AWSCURRENT label to the new version, and the old version becomes AWSPREVIOUS.
Applications always read the AWSCURRENT version.
Put in order

A rotation run

Exam skills 6.1.3

Put the steps of a Secrets Manager rotation in order.

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

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.

Single user
Rotation functionYes
Impact during rotationPossible brief authentication failures
EffortLow
Choose when Simple applications with retries.
Alternating users
Rotation functionYes, plus superuser secret
Impact during rotationNone for cached credentials
EffortMedium
Choose when Applications that cannot tolerate failures.
Managed rotation
Rotation functionNo
Impact during rotationDepends on service
EffortLowest
Choose when Supported services such as the RDS master password.

The following diagram compares the two strategies across several rotations.

The two rotation strategies over successive rotations. With a single user, the password changes in place, so an application that cached the old password can fail to authenticate until it fetches the new value and retries. With alternating users, two users with the same permissions take turns: each rotation changes the user that is not in use and then makes it current, so the previous user's credentials stay valid until the next rotation. Alternating users needs a superuser secret for the rotation function.
Alternating users keeps the previous credentials valid through each rotation.
Sort into groups

Choose the rotation strategy

Exam skills 6.1.3

Choose the rotation strategy for each situation.

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

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.

CredentialPreferred approachIf it must remain
Database passwordSecrets Manager managed rotation or rotation functionNot applicable
Third-party API keySecrets Manager with a custom rotation functionNot applicable
IAM access keyReplace with roles, OIDC or Roles Anywhereaccess-keys-rotated rule with automated rotation
ACM certificateACM-issued with DNS validation (automatic renewal)Imported: monitor expiry and reimport
KMS key materialAutomatic rotationOn-demand rotation
Worked example

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.

  1. Master password

    Set ManageMasterUserPassword on the cluster so RDS manages and rotates the master password.

  2. 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.

  3. Consumption

    Change the application to read the secret through a caching client with a five-minute refresh and a retry on authentication failure.

  4. Monitoring

    Alert on RotationFailed events and deploy the secretsmanager rotation Config rules.

Check yourself

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.