Sub topic 4
Secrets Manager basics: creating and storing secrets
This chapter assumes you can use the AWS CLI, read JSON, and have the IAM and Parameter Store basics from the earlier chapters. Here you create a secret, see how it is structured, and read it from code.
What a secret is
A secret in AWS Secrets Manager is a named container for a protected value. The value is usually a string, often a JSON document, and it can be up to 65,536 bytes. Secrets Manager encrypts the value with an AWS KMS key. It also keeps versions of the value and can rotate it automatically. Those features are the main reason to choose it over Parameter Store, and later chapters cover them.
A secret has these parts:
| Part | Purpose |
|---|---|
| Name | A path-like identifier such as `myapp/prod/db`. It may contain letters, digits, and `/_+=.@-`. |
| ARN | The full identifier. AWS appends a random six-character suffix to the name. |
| Value | The secret string or binary data, stored in a version. |
| KMS key | The key that encrypts the value. The default is the AWS managed key `aws/secretsmanager`. |
| Tags and description | Metadata for organization and cost tracking. |
Create a JSON secret
Applications often need several related values together, such as a username and password. Store them as one JSON secret instead of creating one secret per field.
aws secretsmanager create-secret \
--name myapp/prod/db \
--secret-string '{"username":"app","password":"REPLACE_ME"}'The command returns the ARN, the name, and a version ID. The value itself is not returned.
Read the secret in code
The get_secret_value call returns the current version. For a JSON secret, parse SecretString yourself.
import json
import boto3
secrets = boto3.client("secretsmanager")
response = secrets.get_secret_value(SecretId="myapp/prod/db")
creds = json.loads(response["SecretString"])
print(creds["username"])The script prints only the username. Never log the password or the whole response.
You can pass either the name or the ARN as SecretId. In the same account, the name is enough. Across accounts you must use the full ARN, which the cross-account chapter covers.
Versions and the current value
Every change creates a new version. Secrets Manager marks the active version with the staging label AWSCURRENT. If you call get_secret_value without a version, you get the AWSCURRENT version. To change the value, call put-secret-value. The new version becomes current, and the old one moves to AWSPREVIOUS.
Deleting a secret
Deletion is not immediate. By default, Secrets Manager schedules deletion after a recovery window of 30 days. You can set a window of 7 to 30 days, and you can restore the secret before the window ends. During that time the secret cannot be read. Pick the window with care, because a deleted name cannot be reused until deletion completes.
Common errors
ResourceNotFoundException: the name is wrong, the Region is wrong, or the secret is scheduled for deletion.AccessDeniedException: the caller lackssecretsmanager:GetSecretValue, or lackskms:Decryptwhen the secret uses a customer managed key.InvalidRequestExceptionon a secret pending deletion: restore it first.
Exercise
A teammate stored a database username and password as two separate secrets. Give one reason to combine them into one JSON secret. Name one cost of doing so.
Show answer
One secret keeps the related values together. It needs one API call to read and one IAM resource to grant, and it rotates as a unit. The cost is that access is all-or-nothing, because anyone who can read the secret gets both fields. Your code must also parse the JSON.