Sub topic 8
Versions and staging labels
This chapter assumes you can create a secret with the AWS CLI and read JSON output. It covers how Secrets Manager keeps old values and decides which one an application receives. Rotation, covered in the next chapter, depends on this model.
Versions
Every time you change a secret's value, Secrets Manager creates a new version. Each version has a unique VersionId and holds one complete copy of the secret value. The secret's name and ARN stay the same.
Versions are not addressed by number. You reach a version through a staging label, or by its VersionId if you know it.
Staging labels
A staging label is a text tag attached to a version. Secrets Manager uses three kinds of label:
AWSCURRENTmarks the version that applications get by default. Exactly one version holds it.AWSPREVIOUSmarks the version that heldAWSCURRENTjust before. Secrets Manager moves it for you.AWSPENDINGis used during rotation for a new value that is not yet live.
You can also create custom labels, such as canary. A label sits on only one version of a secret at a time. A single version can carry several labels.
When you call get-secret-value with no version information, Secrets Manager returns the version labeled AWSCURRENT.
What happens when you update a secret
In the last state, version A has no label. Secrets Manager treats a version with no staging label as deprecated and deletes it automatically. It keeps up to 100 unlabeled versions and removes older ones. Do not rely on unlabeled versions for rollback.
Working with versions in the CLI
Store a new value:
aws secretsmanager put-secret-value \
--secret-id myapp/prod/db \
--secret-string '{"username":"app","password":"NEW_VALUE"}'This creates a new version and moves AWSCURRENT to it. The old version receives AWSPREVIOUS.
List versions and their labels:
aws secretsmanager list-secret-version-ids \
--secret-id myapp/prod/dbRead the previous value by label:
aws secretsmanager get-secret-value \
--secret-id myapp/prod/db \
--version-stage AWSPREVIOUSYou can pass --version-id instead of --version-stage. The VersionStage and VersionId options select the same version only if they match. Otherwise the call fails.
Moving labels manually
The update-secret-version-stage command moves a label between versions. This is how you roll back:
aws secretsmanager update-secret-version-stage \
--secret-id myapp/prod/db \
--version-stage AWSCURRENT \
--move-to-version-id PREVIOUS_VERSION_ID \
--remove-from-version-id CURRENT_VERSION_IDMoving AWSCURRENT to the older version makes applications read the old value on their next fetch. Secrets Manager also moves AWSPREVIOUS to the version that just lost AWSCURRENT.
Common errors
- Applications still use the old value. Clients that cache a secret keep the old value until the cache expires. A label move does not push anything to clients.
- Rollback fails because the old version is gone. The version had no label and was cleaned up. Attach a custom label to any version you want to keep.
ResourceNotFoundExceptionfor a version stage. Check that the label exists on the secret withlist-secret-version-ids. A secret with only one version has noAWSPREVIOUS.
Parameter Store comparison
Parameter Store also keeps versions. Each update to a parameter increments a numeric version, and you can read a specific one with name:version or name:label. Parameter labels are user-defined and are not moved automatically as in Secrets Manager.
Exercise
A secret has versions A (AWSPREVIOUS) and B (AWSCURRENT). You call put-secret-value to create version C. Which labels does each version hold afterward, and what happens to A?
Show answer
C holds AWSCURRENT and B holds AWSPREVIOUS. A loses AWSPREVIOUS, so it has no label. Secrets Manager treats it as deprecated and may delete it later.
Exercise
You want to be able to roll back to a specific version later, even after several more updates. What should you do?
Show answer
Attach a custom staging label to that version, for example with update-secret-version-stage. A labeled version is not treated as deprecated, so it is kept.