Mock SSM Parameter Store Get Parameters by Path in Python
This code implements a simple mock of the AWS SSM Parameter Store get_parameters_by_path API, returning parameters under a given path with recursive and non-recursive options.
Python code
33 linesimport json
class MockSSM:
def __init__(self, parameters):
self.parameters = parameters
def get_parameters_by_path(self, path, recursive=True):
result = []
for key, value in self.parameters.items():
if recursive:
if key.startswith(path):
result.append({"Name": key, "Value": value, "Type": "String"})
else:
if key.startswith(path) and "/" not in key[len(path):].strip("/"):
result.append({"Name": key, "Value": value, "Type": "String"})
return result
if __name__ == "__main__":
mock_params = {
"/app/config/db_host": "localhost",
"/app/config/db_port": "5432",
"/app/config/cache/ttl": "3600",
"/app/secrets/api_key": "secret123",
}
ssm = MockSSM(mock_params)
print("Recursive: /app/config")
print(json.dumps(ssm.get_parameters_by_path("/app/config"), indent=2))
print("\nNon-recursive: /app/config")
print(json.dumps(ssm.get_parameters_by_path("/app/config", recursive=False), indent=2))
Output
Recursive: /app/config
[
{
"Name": "/app/config/db_host",
"Value": "localhost",
"Type": "String"
},
{
"Name": "/app/config/db_port",
"Value": "5432",
"Type": "String"
},
{
"Name": "/app/config/cache/ttl",
"Value": "3600",
"Type": "String"
}
]
Non-recursive: /app/config
[
{
"Name": "/app/config/db_host",
"Value": "localhost",
"Type": "String"
},
{
"Name": "/app/config/db_port",
"Value": "5432",
"Type": "String"
}
]
How it works
The mock class iterates over a dictionary of parameter key-value pairs and filters those whose keys start with the given path. For recursive=True, all matching keys are returned regardless of depth. For recursive=False, the code checks that there is no slash in the remainder after the path prefix, effectively limiting results to immediate children. The output is formatted as JSON list of objects with Name, Value, and Type fields, matching the AWS SDK response shape. This allows testing application logic without hitting the real SSM API.
Common mistakes
- Forgetting to strip trailing slashes from the path argument for consistent matching.
- Misunderstanding recursive=False as filtering by depth level instead of direct child check.
- Not handling parameters that are not strings, such as SecureString or StringList types.
Variations
- Use boto3 SSM client with a moto mock for a more realistic integration test.
- Implement the mock as a pytest fixture that can be injected into functions expecting a get_parameters_by_path callable.
Real-world use cases
- Unit testing Lambda or ECS task code that reads configuration from Parameter Store without AWS credentials.
- Simulating Parameter Store responses in local development environments when running cloud applications offline.
- Writing integration tests for deployment scripts that fetch configuration paths before provisioning resources.
Sponsored
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.