Sub topic 7
Retrieving parameters and secrets from the CLI and SDK
This chapter assumes you can create parameters and secrets, and that your role has the IAM and KMS permissions from the earlier chapters. Here you read values back with the AWS CLI and with boto3.
Reading from the CLI
Parameter Store has three read calls. get-parameter reads one name. get-parameters reads up to 10 names. get-parameters-by-path reads everything under a path.
aws ssm get-parameter \
--name /myapp/prod/db/password \
--with-decryption \
--query Parameter.Value \
--output textWithout --with-decryption, a SecureString comes back as ciphertext. The call still succeeds, so the mistake is easy to miss.
Secrets Manager has one read call, get-secret-value. The SecretString field holds the raw text, which is a JSON document for multi-field secrets.
aws secretsmanager get-secret-value \
--secret-id myapp/prod/db \
--query SecretString \
--output textReading from boto3
The SDK calls match the CLI. Two details matter in practice. First, get_parameters_by_path returns at most 10 parameters per call, so use a paginator. Second, create clients once and reuse them.
import json
import boto3
ssm = boto3.client("ssm")
secrets = boto3.client("secretsmanager")
def load_environment(path):
config = {}
paginator = ssm.get_paginator("get_parameters_by_path")
for page in paginator.paginate(Path=path, Recursive=True, WithDecryption=True):
for p in page["Parameters"]:
config[p["Name"]] = p["Value"]
return config
def load_db_credentials(secret_id):
response = secrets.get_secret_value(SecretId=secret_id)
return json.loads(response["SecretString"])
config = load_environment("/myapp/prod")
creds = load_db_credentials("myapp/prod/db")
print(len(config), creds["username"])The script prints the parameter count and the username, never the password.
Common errors
| Error or symptom | Usual cause |
|---|---|
| `ParameterNotFound` | Wrong name, missing leading `/`, or wrong Region |
| `ResourceNotFoundException` | Wrong secret name or Region |
| `AccessDeniedException` on a SecureString | Role lacks `kms:Decrypt` on the key, even with `ssm:GetParameter` allowed |
| Value looks like random text | `WithDecryption` was not set |
| Only 10 parameters returned | Pagination was not handled |
Exercise
A role has ssm:GetParameter on /myapp/prod/* and calls get_parameter for a SecureString with WithDecryption=True. The call fails with AccessDeniedException. What is the most likely missing permission, and what happens if the caller omits WithDecryption?
Show answer
The role most likely lacks kms:Decrypt on the KMS key that encrypts the parameter. If the caller omits WithDecryption, the call can succeed without KMS access, but the Value field holds ciphertext instead of the plaintext.