Difficulty: Intermediate
Why would you use AWS Secrets Manager instead of Systems Manager Parameter Store for database passwords and API keys?
Use Secrets Manager when a secret needs automatic rotation, cross-Region replication, cross-account access through resource policies, or larger values, and accept that it costs money per secret. Use Parameter Store (SecureString) for configuration and for secrets that rarely change, because its standard tier is free. Both encrypt with AWS KMS and are controlled by IAM, so the deciding factor is usually rotation: Secrets Manager can change an RDS password on a schedule and update the secret without application downtime.
| Feature | Secrets Manager | Parameter Store |
|---|---|---|
| Primary purpose | Secrets with a lifecycle | Configuration and simple secrets |
| Encryption | Always KMS-encrypted | SecureString uses KMS; String is plaintext |
| Automatic rotation | Built in, Lambda-based, with managed rotation for RDS, Aurora, Redshift, DocumentDB | No native rotation |
| Max size | 64 KB | 4 KB standard, 8 KB advanced |
| Cross-Region replication | Yes | No |
| Resource-based policy (cross-account) | Yes | No (advanced parameters can be shared via AWS RAM) |
| Versioning | Staging labels AWSCURRENT, AWSPENDING, AWSPREVIOUS | Numbered versions and labels |
| Cost | About $0.40 per secret per month plus API calls | Standard tier free; advanced parameters and higher throughput are charged |
Rotation uses a Lambda function with four steps, and the staging labels let applications keep working throughout:
AWSPENDING.AWSCURRENT to the new version; the old one becomes AWSPREVIOUS.Applications always read AWSCURRENT. For RDS and Aurora you can go further and let RDS manage the master password in Secrets Manager (--manage-master-user-password), which handles rotation for you. For applications that cannot tolerate a brief window of mismatched credentials, the alternating users strategy rotates between two database users.
secretsmanager:GetSecretValue on specific secret ARNs only, and use a customer-managed KMS key if you need tighter control or cross-account access.Keep non-sensitive config (feature flags, URLs, timeouts) in Parameter Store hierarchies like /prod/payments/, and put credentials that should rotate (database passwords, a payment gateway API key) in Secrets Manager. Parameter Store can even reference a Secrets Manager secret through the /aws/reference/secretsmanager/ path, so code can read everything through one API.
import json
import boto3
from botocore.exceptions import ClientError
sm = boto3.client("secretsmanager")
_cache = {}
def get_db_credentials(secret_id="prod/payments/db", refresh=False):
if refresh or secret_id not in _cache:
try:
resp = sm.get_secret_value(SecretId=secret_id) # AWSCURRENT by default
except ClientError as e:
raise RuntimeError(f"Cannot read secret: {e.response['Error']['Code']}")
_cache[secret_id] = json.loads(resp["SecretString"])
return _cache[secret_id]
creds = get_db_credentials()
# On an auth failure, call get_db_credentials(refresh=True) once and retry
Caching avoids an API call per request, and refreshing on auth failure handles the moment just after a rotation.
aws secretsmanager rotate-secret \
--secret-id prod/payments/db \
--rotation-lambda-arn arn:aws:lambda:ap-south-1:111122223333:function:pg-rotation \
--rotation-rules AutomaticallyAfterDays=30
# Parameter Store: free standard tier SecureString, no rotation
aws ssm put-parameter --name /prod/payments/gateway-url \
--type String --value https://api.example-gateway.in
aws ssm get-parameter --name /prod/payments/api-key --with-decryption
Secrets Manager rotates on a schedule using the Lambda; Parameter Store simply stores and returns values, which is enough for config.
AWS Secrets Manager, SSM Parameter Store, Secret Rotation, KMS Encryption, Cross-Region Replication