AWS SDK Secrets Manager and SSM Parameter Store: Read Config Safely (Python, PHP, Node.js)
Passwords and API keys do not belong in code or .env files in git. We store a database password in Secrets Manager and app settings in SSM Parameter Store, read them from Python, PHP and Node.js, rotate a secret, read a whole path of parameters and cache values so you do not call AWS on every request. The example: a payments app for Lucas in São Paulo and Sneha in Chennai.
In this chapter
- Create and read a secret
- Update and rotate
- Store SecureString parameters
- Read parameters by path
- Cache values safely
1. The big picture: the hotel safe
You do not tape your passport to the hotel door. You lock it in the room safe and open it when needed. Secrets Manager is the safe for passwords; Parameter Store is the notice board for settings, with a locked section for sensitive ones.
GetSecretValue returns SecretString. GetParameter with WithDecryption reads a SecureString. Both are encrypted with KMS; IAM decides who can read.
2. Create and read a secret
Trace the sample payments/db secret. Its name identifies the resource the application requests. The SDK’s GetSecretValue response can contain a JSON string with more than one field. The Node.js example parses that string to use the configured fields. The application role must be permitted to retrieve it, and it should process the credential in memory instead of printing it into routine logs.
Test with a made-up value in the intended Region. Verify that your program selected the expected secret and can parse its required fields without displaying those values. If it reads the secret but finds an absent field, diagnose the sample data format. If the service reports access denial, inspect the app role, chosen resource and any associated encryption access. A successful retrieval proves only that this credential was retrieved, so test a safe connection separately if the lab has a target service.
Lucas needs the Postgres password for the Brazil database without anyone seeing it in code.
Store JSON in SecretString so one secret holds username, password, host. Read it and parse the JSON.
- Python: create the secret.
import boto3, json
sm = boto3.client("secretsmanager", region_name="sa-east-1")
r = sm.create_secret(Name="payments/db",
SecretString=json.dumps({"user": "pay_app", "password": "Xy7!q2-Lm9", "host": "db.internal", "port": 5432}))
print(r["ARN"])
Expected output:
arn:aws:secretsmanager:sa-east-1:123456789012:secret:payments/db-AbCdEf
- Node.js: read and parse it.
import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";
const sm = new SecretsManagerClient({ region: "sa-east-1" });
const { SecretString } = await sm.send(new GetSecretValueCommand({ SecretId: "payments/db" }));
const db = JSON.parse(SecretString);
console.log(db.user, db.host, db.port);
Expected output:
pay_app db.internal 5432
- PHP: read it and build a PDO DSN.
<?php
require "vendor/autoload.php";
$sm = new Aws\SecretsManager\SecretsManagerClient(["region" => "sa-east-1"]);
$db = json_decode($sm->getSecretValue(["SecretId" => "payments/db"])["SecretString"], true);
$dsn = "pgsql:host={$db["host"]};port={$db["port"]};dbname=payments";
echo $dsn, PHP_EOL;
Expected output:
pgsql:host=db.internal;port=5432;dbname=payments
- Verify: describe the secret (never prints the value).
aws secretsmanager describe-secret --secret-id payments/db --region sa-east-1 --query "[Name,LastChangedDate]" --output text
Expected output:
payments/db 2026-10-24T12:10:00+05:30
3. Update and rotate
The security team in Chennai rotates every database password each 30 days.
PutSecretValue writes a new version labelled AWSCURRENT; the old one becomes AWSPREVIOUS. RotateSecret runs a rotation Lambda on a schedule.
- Python: generate and store a new password.
pw = sm.get_random_password(PasswordLength=24, ExcludePunctuation=True)["RandomPassword"]
cur = json.loads(sm.get_secret_value(SecretId="payments/db")["SecretString"])
cur["password"] = pw
v = sm.put_secret_value(SecretId="payments/db", SecretString=json.dumps(cur))
print(v["VersionStages"])
Expected output:
['AWSCURRENT']
- Node.js: turn on automatic rotation.
import { RotateSecretCommand } from "@aws-sdk/client-secrets-manager";
await sm.send(new RotateSecretCommand({
SecretId: "payments/db",
RotationLambdaARN: "arn:aws:lambda:sa-east-1:123456789012:function:pg-rotator",
RotationRules: { ScheduleExpression: "rate(30 days)" },
}));
console.log("rotation on");
Expected output:
rotation on
- PHP: read the previous version (for a rollback).
<?php
$old = $sm->getSecretValue(["SecretId" => "payments/db", "VersionStage" => "AWSPREVIOUS"]);
echo json_decode($old["SecretString"])->user, " previous version ok", PHP_EOL;
Expected output:
pay_app previous version ok
- Verify: list versions.
aws secretsmanager list-secret-version-ids --secret-id payments/db --region sa-east-1 --query "Versions[].VersionStages" --output text
Expected output:
AWSPREVIOUS
AWSCURRENT
4. Parameter Store settings
Sneha keeps feature flags, the UPI gateway URL and a Stripe key per environment.
Name parameters as paths like /payments/prod/... . String for plain values, SecureString for secrets. GetParametersByPath reads the whole tree (10 per page, paginate).
- Python: put three parameters.
ssm = boto3.client("ssm", region_name="ap-south-2")
ssm.put_parameter(Name="/payments/prod/upi_url", Value="https://upi.example.in/v2", Type="String", Overwrite=True)
ssm.put_parameter(Name="/payments/prod/max_txn_inr", Value="200000", Type="String", Overwrite=True)
ssm.put_parameter(Name="/payments/prod/stripe_key", Value="sk_live_51abc", Type="SecureString", Overwrite=True)
print("3 saved")
Expected output:
3 saved
- Node.js: read one SecureString.
import { SSMClient, GetParameterCommand } from "@aws-sdk/client-ssm";
const ssm = new SSMClient({ region: "ap-south-2" });
const r = await ssm.send(new GetParameterCommand({ Name: "/payments/prod/stripe_key", WithDecryption: true }));
console.log(r.Parameter.Type, r.Parameter.Value.slice(0, 8) + "…");
Expected output:
SecureString sk_live_…
- PHP: read the whole path with a paginator.
<?php
$ssm = new Aws\Ssm\SsmClient(["region" => "ap-south-2"]);
$cfg = [];
foreach ($ssm->getPaginator("GetParametersByPath", ["Path" => "/payments/prod/", "WithDecryption" => true]) as $page)
foreach ($page["Parameters"] as $p) $cfg[basename($p["Name"])] = $p["Value"];
ksort($cfg); echo implode(",", array_keys($cfg)), PHP_EOL;
Expected output:
max_txn_inr,stripe_key,upi_url
- Verify: describe parameters.
aws ssm describe-parameters --parameter-filters Key=Path,Values=/payments/prod --region ap-south-2 --query "Parameters[].[Name,Type]" --output text
Expected output:
/payments/prod/max_txn_inr String
/payments/prod/stripe_key SecureString
/payments/prod/upi_url String
5. Cache values
A local cache keeps a recently retrieved secret in memory. Reuse reduces service calls and latency, but a cached value may be out of date after rotation. Set how long the application may reuse it. Design what the application does when a database rejects an old credential while the secret service is available for a fresh read.
Use a disposable sample and rotate it in the intended Region. Log timestamps and the retrieval outcome rather than the value. The application can use the cached version until its limit is reached, then retrieve the current version. A permissions error requires checking the application role, resource and relevant KMS access. Extending the cache lifetime will not grant access to a new value.
At 500 requests a second, calling AWS every time is slow, costly and hits throttling.
Read once and keep it in memory for a few minutes. Python and others have official caching libraries; a small TTL cache works everywhere. On Lambda, the Parameters and Secrets extension does it for you on localhost:2773.
- Python: official cache.
# pip install aws-secretsmanager-caching
from aws_secretsmanager_caching import SecretCache, SecretCacheConfig
cache = SecretCache(config=SecretCacheConfig(secret_refresh_interval=300), client=sm)
for _ in range(3):
print(json.loads(cache.get_secret_string("payments/db"))["user"])
Expected output:
pay_app
pay_app
pay_app (one API call)
- Node.js: a tiny TTL cache.
const cache = new Map();
async function param(name, ttl = 300_000) {
const hit = cache.get(name);
if (hit && hit.exp > Date.now()) return hit.v;
const r = await ssm.send(new GetParameterCommand({ Name: name, WithDecryption: true }));
cache.set(name, { v: r.Parameter.Value, exp: Date.now() + ttl });
return r.Parameter.Value;
}
console.log(await param("/payments/prod/max_txn_inr"), await param("/payments/prod/max_txn_inr"));
Expected output:
200000 200000 (second read from memory)
- PHP: APCu cache around the SDK call.
<?php
function param(Aws\Ssm\SsmClient $ssm, string $n): string {
$v = apcu_fetch($n, $ok);
if ($ok) return $v;
$v = $ssm->getParameter(["Name" => $n, "WithDecryption" => true])["Parameter"]["Value"];
apcu_store($n, $v, 300);
return $v;
}
echo param($ssm, "/payments/prod/upi_url"), PHP_EOL;
Expected output:
https://upi.example.in/v2
- Verify: on Lambda, read via the extension.
curl -s -H "X-Aws-Parameters-Secrets-Token: $AWS_SESSION_TOKEN" "localhost:2773/systemsmanager/parameters/get?name=/payments/prod/upi_url" | jq -r .Parameter.Value
Expected output:
https://upi.example.in/v2
6. The same call in Java, Go, .NET and the CLI
Teams often mix languages. Once you know the call in Python, PHP and Node.js, the other SDKs are the same API with different syntax.
All of them call the GetSecretValue API. Use the CLI line to test it quickly from a terminal.
| Stack | Code |
|---|---|
| AWS CLI | aws secretsmanager get-secret-value --secret-id payments/db --query SecretString --output text |
| Java v2 | String s = sm.getSecretValue(b -> b.secretId("payments/db")).secretString(); |
| Go v2 | out, _ := client.GetSecretValue(ctx, &secretsmanager.GetSecretValueInput{SecretId: aws.String("payments/db")}) |
| .NET | var r = await sm.GetSecretValueAsync(new GetSecretValueRequest { SecretId = "payments/db" }); |
7. Clean up so nothing keeps costing
Lab resources left running cost money and can be a security risk.
Delete in reverse order: children first, then parents.
- Delete the secret and parameters.
aws secretsmanager delete-secret --secret-id payments/db --force-delete-without-recovery --region sa-east-1
aws ssm delete-parameters --names /payments/prod/upi_url /payments/prod/max_txn_inr /payments/prod/stripe_key --region ap-south-2
Expected output:
(no output means success)
Ravindra Bagale's Tip
Never print or log a secret value, even in debug. Log the secret name and version id instead.
8. Interview questions and answers
Q. Secrets Manager vs Parameter Store?
Secrets Manager adds rotation and cross-account sharing at a per-secret price; Parameter Store is free for standard parameters and good for config.
Q. What does WithDecryption do?
Returns the plain value of a SecureString parameter using KMS.
Q. What are AWSCURRENT and AWSPREVIOUS?
Staging labels on secret versions; reads default to AWSCURRENT.
Q. Why cache secrets?
To cut latency, cost and throttling; refresh every few minutes to pick up rotation.
Q. How many parameters per GetParametersByPath page?
Up to 10; use the paginator.
Try at home
- Store /payments/dev/ values and load dev or prod config based on an APP_ENV variable in Node.js.
- Write an IAM policy that lets an app read only /payments/prod/*.
In short
- Secrets: Create/Get/Put.
- Rotate with a Lambda.
- SSM: SecureString + WithDecryption.
- GetParametersByPath + paginator.
- Cache with a TTL.
Log the secret name and outcome, never its value
Use dummy values and verify the app can read one designated secret while access to an adjacent secret is denied. Cache where appropriate, but decide when rotated values must be refreshed. Inspect decryption permissions separately.
Interview check What changes after rotation?
Applications may need to refresh cached credentials, and a secret version stage identifies the appropriate version. Rotation and application refresh are distinct steps.
Practise this chapter
Interview questions and answers
How would you design an application that caches a rotating database credential?
I would define a bounded cache lifetime and what to do when the database rejects an old value. I would test retrieval and connection after rotation without logging the credential. I would keep role permissions scoped to the needed secret and applicable key.
Check your understanding
Choose an answer, then compare your reasoning with the explanation.
Practice Tasks
Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.
About this scenarioSahyadri Clinic is a healthcare business in Satara. Dr. Anil Deshmukh looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 11
Easy Read a secret
Apps read the secret at start-up instead of keeping it in a file.
Get the value of secret sahyadriclinic/prod/db.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Port for SSH
Security group rules need the right port.
Which TCP port must the security group open for SSH?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy Port for MySQL
The DB security group should open only this port, only to the app.
Sahyadri Clinic's app talks to RDS MySQL. Which port should the DB security group allow?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Read a decrypted parameter
Without --with-decryption you only see the encrypted blob.
Read /sahyadriclinic/prod/api-key and decrypt it.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Store an encrypted parameter
SecureString parameters are encrypted with KMS and are free for standard tier.
Store /sahyadriclinic/prod/api-key as a SecureString with the value abc123.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Read one secret only
Each app should read only its own secret.
Allow secretsmanager:GetSecretValue on arn:aws:secretsmanager:ap-south-1:102103063049:secret:sahyadriclinic/prod/db-* only.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Store a database password
Secrets Manager encrypts secrets and can rotate them; never put them in code.
Create secret sahyadriclinic/prod/db with the value from secret.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium CloudFormation: web security group
Opening only 443 keeps the server safer.
Template with an AWS::EC2::SecurityGroup that allows TCP 443 from 0.0.0.0/0. Add a GroupDescription.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Hard Read all parameters under a path
A path groups config, like /app/prod/, so one call loads it all.
Get all parameters under /sahyadriclinic/prod, including sub-paths, decrypted.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Rotate a secret now
Rotation changes the password on a schedule or on demand.
Start rotation of secret sahyadriclinic/prod/db now.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Deny access from outside the office
An explicit Deny with aws:SourceIp blocks keys used from anywhere else.
Write one Deny statement: deny all actions (*) on all resources (*) when the source IP is NOT in 203.0.113.0/24.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Revision, comparison and interview cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.




More cards: Ravi Sir's Cards.