Lambda rotation functions - AWS Secrets Manager (original) (raw)

In Rotation by Lambda function, an AWS Lambda function rotates the secret. AWS Secrets Manager uses staging labels to identify secret versions during rotation.

If AWS Secrets Manager doesn't provide a rotation function template for your secret type, you can create a custom rotation function. Follow these guidelines when writing your rotation function:

Best practices for custom rotation functions

Four steps in a rotation function

Topics

createSecret: Create a new version of the secret

The method createSecret first checks if a secret exists by calling get_secret_value with the passed-in ClientRequestToken. If there's no secret, it creates a new secret with create_secret and the token as the VersionId. Then it generates a new secret value with get_random_password. Next it calls put_secret_value to store it with the staging label`AWSPENDING`. Storing the new secret value in `AWSPENDING` helps ensure idempotency. If rotation fails for any reason, you can refer to that secret value in subsequent calls. See How do I make my Lambda function idempotent.

Tips for writing your own rotation function

setSecret: Change the credentials in the database or service

The method setSecret changes the credential in the database or service to match the new secret value in the AWSPENDING version of the secret.

Tips for writing your own rotation function

testSecret: Test the new secret version

Next, the Lambda rotation function tests the AWSPENDING version of the secret by using it to access the database or service. Rotation functions based on Rotation function templates test the new secret by using read access.

finishSecret: Finish the rotation

Finally, the Lambda rotation function moves the label AWSCURRENT from the previous secret version to this version, which also removes the AWSPENDING label in the same API call. Secrets Manager adds the AWSPREVIOUS staging label to the previous version, so that you retain the last known good version of the secret.

The method finish_secret uses update_secret_version_stage to move the staging label AWSCURRENT from the previous secret version to the new secret version. Secrets Manager automatically adds theAWSPREVIOUS staging label to the previous version, so that you retain the last known good version of the secret.

Tips for writing your own rotation function

When rotation is successful, the AWSPENDING staging label might be attached to the same version as the AWSCURRENT version, or it might not be attached to any version. If theAWSPENDING staging label is present but not attached to the same version as AWSCURRENT, then any later invocation of rotation assumes that a previous rotation request is still in progress and returns an error. When rotation is unsuccessful, the AWSPENDING staging label might be attached to an empty secret version. For more information, see Troubleshoot rotation.