Sub topic 9
Rotating secrets automatically
This chapter assumes you can create a Secrets Manager secret and know that versions carry staging labels such as AWSCURRENT. It also assumes you can read basic Lambda and IAM configuration.
What rotation does
Rotation replaces a secret's value on a schedule, so a leaked credential stops working after a limited time. Secrets Manager supports rotation. Parameter Store does not. If you need automatic rotation, that is a reason to choose Secrets Manager.
Rotation changes two things. It changes the credential in the secret, and it changes the credential in the system that checks it, such as a database user. A Lambda function called a rotation function does both.
The four steps
Secrets Manager invokes the rotation function once per step. Each call includes the secret ID, a version token, and the step name.
createSecretgenerates a new value and stores it as a new version labeledAWSPENDING.setSecretchanges the credential in the database or service to match the pending value.testSecretsigns in with the pending value to confirm it works.finishSecretmovesAWSCURRENTto the pending version. The old version getsAWSPREVIOUS.
Clients that ask for the default version never see a half-finished rotation. They get AWSCURRENT until finishSecret runs.
Turning rotation on
For Amazon RDS, Aurora, Redshift and DocumentDB, AWS provides rotation function templates. For some services, managed rotation creates and runs the function for you. For anything else, you write the function yourself.
First, let Secrets Manager invoke the function:
aws lambda add-permission \
--function-name myapp-db-rotation \
--statement-id SecretsManagerAccess \
--action lambda:InvokeFunction \
--principal secretsmanager.amazonaws.com \
--source-account 111122223333Then attach the function and set a schedule:
aws secretsmanager rotate-secret \
--secret-id myapp/prod/db \
--rotation-lambda-arn arn:aws:lambda:us-east-1:111122223333:function:myapp-db-rotation \
--rotation-rules AutomaticallyAfterDays=30Secrets Manager starts a rotation right away when you run this command. Later rotations follow the schedule.
Single user or alternating users
The single-user strategy changes the password of one database user. It is simple, but connections that open between setSecret and finishSecret can fail briefly.
The alternating-users strategy keeps two users and switches between them on each rotation. The user your clients currently use is never changed, so there is no failure window. It needs a second secret that holds administrator credentials, and it is the better choice for production.
Common errors
| Symptom | Likely cause |
|---|---|
| Rotation fails at `setSecret` | The function cannot reach the database. Check its VPC, subnets and security groups. |
| Function times out calling Secrets Manager | A function in a private subnet has no route to the service. Add a VPC endpoint or a NAT gateway. |
| Secrets Manager cannot invoke the function | The Lambda resource policy lacks the permission shown above. |
| Application gets authentication errors after rotation | The application cached the old value. Refresh the cache on failure. |
| Rotation fails at `testSecret` | The database user lacks privileges, or the new password failed the database's password rules. |
The caching error is the most frequent. Chapter "Caching values in your application" covers how to refresh a cached value when authentication fails.
Exercise
A rotation run fails at the testSecret step. Which version do applications that request the secret without a version label receive, and why?
Show answer
They receive the old value, because only finishSecret moves the AWSCURRENT label. The failed run leaves the new value labeled AWSPENDING, so clients are unaffected.