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.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 16 views 0 copies

Python code

33 lines
Python 3.9+
import 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

stdout
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

  1. Use boto3 SSM client with a moto mock for a more realistic integration test.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.