Sub topic 8
Versions and staging labels
This chapter assumes you can create a secret and a parameter, and read them with the AWS CLI. Both services keep a history of values. Secrets Manager tracks it with version IDs and staging labels. Parameter Store tracks it with version numbers and optional labels. The two work differently, so this chapter covers each separately.
Secrets Manager: versions and labels
Every time you change a secret's value, Secrets Manager creates a new version instead of overwriting the old one. A version has two parts:
- A version ID, a unique string (a UUID) that never changes.
- Zero or more staging labels, which are movable pointers attached to a version.
Applications rarely ask for a version ID. They ask for a label, and the label decides which version they get. Three labels are reserved and managed by AWS:
| Label | Meaning |
|---|---|
| `AWSCURRENT` | The version applications should use. This is the default when you call `GetSecretValue` without a version. |
| `AWSPREVIOUS` | The version that was `AWSCURRENT` before the latest change. |
| `AWSPENDING` | A version that has been created but not yet promoted. Rotation uses this label while it prepares a new credential. |
Three rules govern labels:
- A label is attached to at most one version of a secret at a time. Attaching it to another version moves it.
- A version can carry several labels at once.
- A version with no labels is deprecated. It still exists for a while, but you can only fetch it by version ID. Secrets Manager may delete deprecated versions automatically. It keeps at most about 100 versions, and it removes only unlabeled ones older than 24 hours. Do not rely on a deprecated version being available.
What a normal update does
When you call put-secret-value without specifying a stage, Secrets Manager creates a new version and attaches AWSCURRENT to it. The version that held AWSCURRENT receives AWSPREVIOUS. The version that held AWSPREVIOUS loses its label and is deprecated.
aws secretsmanager put-secret-value \
--secret-id myapp/prod/db \
--secret-string '{"username":"app","password":"NEW_VALUE"}'
aws secretsmanager list-secret-version-ids \
--secret-id myapp/prod/db \
--include-deprecatedThe listing shows each version ID with its VersionStages. After two updates you will see one version with AWSCURRENT, one with AWSPREVIOUS, and possibly an unlabeled version if you pass --include-deprecated.
Reading a specific version
You can select a version by label or by ID:
aws secretsmanager get-secret-value \
--secret-id myapp/prod/db \
--version-stage AWSPREVIOUS
aws secretsmanager get-secret-value \
--secret-id myapp/prod/db \
--version-id EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLEIf you pass both --version-id and --version-stage, the label must be attached to that version, or the call fails with ResourceNotFoundException.
Idempotent writes
put-secret-value accepts a --client-request-token, which is a unique string up to 64 characters that becomes the new version ID. It makes retries safe. If a call times out and you resend the same token with the same value, Secrets Manager does not create a duplicate. If you resend the same token with a different value, the call fails. The CLI and SDKs generate a token for you if you omit it. Set one yourself when your own code retries.
Custom labels
You can invent your own labels, such as canary or blue. Use update-secret-version-stage to attach, move, or remove a label:
aws secretsmanager update-secret-version-stage \
--secret-id myapp/prod/db \
--version-stage canary \
--move-to-version-id EXAMPLE1-90ab-cdef-fedc-ba987EXAMPLECustom labels have two uses.
- Staged rollout. One group of clients reads
canary, and the rest readAWSCURRENT. You promote the new value by movingAWSCURRENTafter the canary clients succeed. - Protecting a version from cleanup. A labeled version is never deprecated, so a custom label keeps a known-good value around.
Moving AWSCURRENT yourself is also the manual rollback. When you move AWSCURRENT to another version, Secrets Manager moves AWSPREVIOUS to the version that was current just before.
aws secretsmanager update-secret-version-stage \
--secret-id myapp/prod/db \
--version-stage AWSCURRENT \
--move-to-version-id OLD-VERSION-ID \
--remove-from-version-id BAD-VERSION-IDRolling back only changes what the secret returns. It does not change the password inside your database. If the bad version was a real credential change, the database may still expect the new password. Check the target system before you roll back.
Reading versions from code
import json
import boto3
client = boto3.client("secretsmanager")
def get_creds(stage="AWSCURRENT"):
response = client.get_secret_value(
SecretId="myapp/prod/db",
VersionStage=stage,
)
return response["VersionId"], json.loads(response["SecretString"])
version_id, current = get_creds()
print("current version:", version_id)
try:
prev_id, previous = get_creds("AWSPREVIOUS")
print("previous version:", prev_id)
except client.exceptions.ResourceNotFoundException:
print("no previous version exists yet")A secret that has only ever had one value has no AWSPREVIOUS, so reading it raises ResourceNotFoundException. Handle that case.
How rotation uses labels
Rotation, covered in the next chapter, is built on labels. The rotation function creates a new version labeled AWSPENDING, sets the new credential in the database, tests it, and finally moves AWSCURRENT to the new version. Because the move is a single label change, clients that read AWSCURRENT switch from the old value to the new one at one point in time. Clients that already hold the old value keep working until the database stops accepting it. That is why some rotation strategies keep two valid users and alternate between them.
If a rotation fails partway through, an AWSPENDING version may remain. A later rotation attempt reuses or replaces it. You do not need to delete it by hand.
Parameter Store: versions and labels
Parameter Store handles versions differently. A parameter has an integer version that starts at 1 and increases by one each time you overwrite the value. There are no automatic CURRENT or PREVIOUS labels. Reading a parameter without a version always returns the latest one.
aws ssm put-parameter \
--name /myapp/prod/db/host \
--value db2.example.internal \
--type String \
--overwrite
aws ssm get-parameter-history --name /myapp/prod/db/hostWithout --overwrite, put-parameter fails on an existing name. With it, you create a new version.
You can request an older version by appending :version to the name. You can also attach your own parameter labels to a version and request it by label:
aws ssm label-parameter-version \
--name /myapp/prod/db/host \
--parameter-version 2 \
--labels stable
aws ssm get-parameter --name /myapp/prod/db/host:stable
aws ssm get-parameter --name /myapp/prod/db/host:1Label rules differ from Secrets Manager's:
- A label can be attached to one version of a parameter at a time. Labeling another version moves it.
- A version can have up to 10 labels.
- Labels are 1 to 100 characters, using letters, numbers,
.,-, and_. They cannot begin withawsorssm, in any letter case, and cannot start with a number. - Labels are not automatic. Nothing moves them during a write.
Parameter Store keeps the 100 most recent versions of a parameter. When you write a 101st version, it removes the oldest. If that oldest version has a label, the write fails with ParameterMaxVersionLimitExceeded. You must remove the label first, using unlabel-parameter-version, or delete the version's label some other way, and then retry. Teams that use many labels on old versions hit this error without warning.
Comparison
| Aspect | Secrets Manager | Parameter Store |
|---|---|---|
| Version identifier | UUID-style version ID | Integer, increasing by 1 |
| Automatic labels | `AWSCURRENT`, `AWSPREVIOUS`, `AWSPENDING` | None |
| Custom labels | Yes, many per version | Yes, up to 10 per version |
| Read an older version | `--version-id` or `--version-stage` | `name:version` or `name:label` |
| History kept | Labeled versions, plus unlabeled ones for a limited time | 100 most recent versions |
Common errors
- Pinning to a version ID in production code. The ID goes stale on the next update, and the version may be deleted. Read by label unless you have a reason to pin.
- Expecting
AWSPREVIOUSto hold history. It holds one version, the one just before the current one. Use a custom label for anything you need to keep longer. - Assuming a rollback fixes the system. Moving a label changes the secret, not the database.
- Forgetting that cached values hide label moves. Clients that cache a secret keep the old value until the cache expires. The caching chapter covers this.
- Labeled old parameter versions blocking writes. See
ParameterMaxVersionLimitExceededabove.
Exercise
A secret has been updated three times with plain put-secret-value calls, and you have attached no custom labels. Which versions can you still read by label, and what happens to the oldest version?
Show answer
Only two versions have labels: the newest has AWSCURRENT and the one before it has AWSPREVIOUS. The first version lost its last label on the third update, so it is deprecated. You can read it only by its version ID, and Secrets Manager may delete it automatically. To keep it, attach a custom label with update-secret-version-stage before it loses its labels.
Exercise
A deployment script runs put-parameter --overwrite on a parameter that has had 100 versions, and the call fails with ParameterMaxVersionLimitExceeded. What is the likely cause, and how do you fix it?
Show answer
The oldest version has a label, so Parameter Store cannot remove it to make room. Remove that label with unlabel-parameter-version, or move the label to a newer version, then retry the write.