Secrets Manager and Parameter Store
Overview
Applications need configuration and secrets such as database passwords, API tokens and feature flags. Hard-coding them in code or plain environment variables is risky. AWS offers two managed stores: AWS Secrets Manager for secrets with built-in automatic rotation, and SSM Parameter Store for hierarchical configuration and secrets at lower cost.
Think of a hotel reception. Parameter Store is the notice board and key cabinet: simple, cheap, organised by floors (paths). Secrets Manager is the concierge safe: it changes the combination on a schedule and tells the right guests.
The exam asks which service to pick (rotation and cost are the usual differentiators) and how to fetch values securely and efficiently from Lambda and containers.
Key Concepts
- Secrets Manager stores secrets up to 64 KB, encrypted with KMS, with built-in rotation (Lambda-based; native templates for RDS, Aurora, Redshift, DocumentDB) and cross-Region replication; priced per secret per month plus API calls.
- Parameter Store has Standard (4 KB, free) and Advanced (8 KB, charged, policies like expiration/TTL) parameters; types String, StringList, SecureString (KMS).
- Parameter Store uses hierarchies (
/myapp/prod/db/host) and supportsGetParametersByPath, versions and labels; no native rotation. - Both integrate with IAM and CloudTrail; restrict by resource path and require
kms:Decryptfor encrypted values. - CloudFormation dynamic references (
{{resolve:ssm:...}},{{resolve:secretsmanager:...}}) pull values at deploy time without exposing them in templates. - Secrets Manager can generate random passwords and supports multi-user rotation strategies (alternating users) for zero downtime.
- In ECS and Lambda, reference secrets directly; for Lambda use the AWS Parameters and Secrets Lambda Extension to cache values locally.
- Cache secrets in your application for minutes, not per request, to cut latency, cost and throttling.
Detailed Notes
Secrets Manager vs Parameter Store
| Feature | Secrets Manager | Parameter Store |
|---|---|---|
| Primary purpose | Secrets with lifecycle | Configuration and simple secrets |
| Automatic rotation | Yes | No (DIY with Lambda/EventBridge) |
| Max size | 64 KB | 4 KB standard / 8 KB advanced |
| Cost | Per secret/month + API | Standard free; advanced per parameter |
| Cross-Region replication | Yes | No |
| Versioning | Staging labels (AWSCURRENT, AWSPREVIOUS) | Versions and labels |
| Encryption | Always KMS | Optional (SecureString) |
| Cross-account sharing | Resource policy | Advanced parameters via RAM |
Fetching in Python
import boto3, json
sm = boto3.client("secretsmanager")
secret = json.loads(sm.get_secret_value(SecretId="prod/orders/db")["SecretString"])
# {"username": "...", "password": "...", "host": "..."}
ssm = boto3.client("ssm")
param = ssm.get_parameter(Name="/orders/prod/feature-x", WithDecryption=True)["Parameter"]["Value"]
page = ssm.get_parameters_by_path(Path="/orders/prod/", Recursive=True, WithDecryption=True)
config = {p["Name"].split("/")[-1]: p["Value"] for p in page["Parameters"]}
import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";
const sm = new SecretsManagerClient({});
const { SecretString } = await sm.send(new GetSecretValueCommand({ SecretId: "prod/orders/db" }));
Fetch during initialisation (outside the handler) and refresh periodically. If a rotation occurs while connections are open, catch authentication failures and re-fetch the secret.
Rotation
Secrets Manager invokes a rotation Lambda through four steps: createSecret, setSecret, testSecret, finishSecret. New credentials are staged as AWSPENDING, tested, then promoted to AWSCURRENT.
aws secretsmanager create-secret --name prod/orders/db \
--secret-string '{"username":"app","password":"change-me"}'
aws secretsmanager rotate-secret --secret-id prod/orders/db \
--rotation-lambda-arn arn:aws:lambda:eu-west-1:111122223333:function:RotateRds \
--rotation-rules AutomaticallyAfterDays=30
SAM / CloudFormation
Resources:
DbSecret:
Type: AWS::SecretsManager::Secret
Properties:
GenerateSecretString:
SecretStringTemplate: '{"username":"app"}'
GenerateStringKey: password
PasswordLength: 24
ExcludeCharacters: '"@/\\'
Fn:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.12
Handler: app.handler
Environment:
Variables:
SECRET_ARN: !Ref DbSecret
LOG_LEVEL: "{{resolve:ssm:/orders/prod/log-level}}"
Policies:
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Ref DbSecret
Pass the ARN of the secret, not its value, in environment variables so the value never appears in the console.
IAM policy for a path
{
"Effect": "Allow",
"Action": ["ssm:GetParameter", "ssm:GetParametersByPath"],
"Resource": "arn:aws:ssm:eu-west-1:111122223333:parameter/orders/prod/*"
}
Decrypting a SecureString also needs kms:Decrypt on the key used (not needed for the default AWS managed key aws/ssm in the same account when the key policy allows).
Lambda extension
The AWS Parameters and Secrets Lambda Extension exposes a local HTTP endpoint (http://localhost:2773) with in-memory caching, so your function avoids repeated service calls and SDK code.
Decision guide
- Needs automatic rotation or cross-Region replica -> Secrets Manager.
- Plain config, feature flags, many values, free -> Parameter Store.
- Both are acceptable for secrets; choose by rotation, size and cost.
Retrieving values in code
import json, boto3
sm = boto3.client("secretsmanager")
secret = json.loads(sm.get_secret_value(SecretId="prod/orders/db")["SecretString"])
ssm = boto3.client("ssm")
p = ssm.get_parameter(Name="/orders/prod/feature-x", WithDecryption=True)["Parameter"]["Value"]
batch = ssm.get_parameters_by_path(Path="/orders/prod/", Recursive=True, WithDecryption=True)
aws secretsmanager rotate-secret --secret-id prod/orders/db --rotation-lambda-arn arn:aws:lambda:eu-west-1:111122223333:function:rotate
aws secretsmanager replicate-secret-to-regions --secret-id prod/orders/db --add-replica-regions Region=eu-central-1
| Feature | Secrets Manager | Parameter Store |
|---|---|---|
| Automatic rotation | Yes (Lambda, built in for RDS) | No (build your own) |
| Max size | 64 KB | 4 KB standard, 8 KB advanced |
| Cross-Region replication | Yes | No |
| Cost | Per secret per month plus API calls | Standard tier free |
| Versioning | Staging labels (AWSCURRENT, AWSPREVIOUS) | Versions and labels |
Rotation follows four Lambda steps: createSecret, setSecret, testSecret and finishSecret. Cache secrets in the execution environment (the AWS-provided caching clients or the Lambda extension) to cut latency and cost, and reference secrets in ECS task definitions, CloudFormation dynamic references ({{resolve:secretsmanager:...}}) and CodeBuild instead of hard-coding them. Restrict access with resource policies and KMS key policies.
Real World Example
A SaaS company keeps database credentials in Secrets Manager with 30-day rotation using the RDS template, so no human ever knows the production password. Application settings (feature flags, endpoints) live in Parameter Store under /saas/prod/, and CloudFormation reads non-sensitive values through dynamic references. Lambda functions use the Parameters and Secrets extension to cache values for five minutes, reducing API calls by 95% and avoiding throttling at peak.
Architecture Diagram
The function reads the secret ARN from the environment and retrieves and caches the value at runtime.
- 1The function starts with only the secret's ARN in its environment.
- 2It calls Secrets Manager (or the extension cache) with its role permissions.
- 3Secrets Manager decrypts the value with KMS and returns it.
- 4A rotation Lambda changes the database password on schedule and updates the secret.
Best Practices
- ✓Never store secrets in code, images or plain environment variables.
- ✓Use Secrets Manager rotation for database credentials.
- ✓Organise parameters in a hierarchy and scope IAM by path.
- ✓Cache values and re-fetch on authentication failure.
- ✓Use CloudFormation dynamic references so secrets never appear in templates.
Common Interview Questions
Exam Tips
DVA-C02 focus
- Automatic rotation keyword -> Secrets Manager.
- Cheapest hierarchical config store -> Parameter Store.
- SecureString requires KMS decrypt permission.
- Reduce secret fetch latency and cost -> cache (extension or in-memory).
Finished this lesson?
0 of 46 lessons done · 0% of the course

