How to Mock Pulumi Stack Outputs in Python
Create a dict-like mock of Pulumi stack outputs for local testing and scripts without running pulumi.
Python code
41 linesfrom collections import defaultdict
class StackOutputMock:
def __init__(self, outputs: dict):
self.outputs = dict(outputs)
def export(self):
return self.outputs
def get(self, key: str, default=None):
return self.outputs.get(key, default)
def keys(self):
return self.outputs.keys()
def values(self):
return self.outputs.values()
def items(self):
return self.outputs.items()
def __getitem__(self, key):
return self.outputs[key]
def __contains__(self, key):
return key in self.outputs
if __name__ == "__main__":
stack_output = StackOutputMock({
"bucket_name": "my-app-assets",
"api_url": "https://api.example.com/v1",
"region": "us-east-1",
"lambda_arn": "arn:aws:lambda:us-east-1:123456789012:function:processor"
})
print(stack_output.get("bucket_name"))
print(stack_output["api_url"])
print("region" in stack_output)
print(stack_output.keys())
print(list(stack_output.items()))
Output
my-app-assets
https://api.example.com/v1
True
dict_keys(['bucket_name', 'api_url', 'region', 'lambda_arn'])
[('bucket_name', 'my-app-assets'), ('api_url', 'https://api.example.com/v1'), ('region', 'us-east-1'), ('lambda_arn', 'arn:aws:lambda:us-east-1:123456789012:function:processor')]
How it works
This class wraps a plain dictionary so you can access Pulumi stack outputs with familiar dict-like syntax. The __getitem__ method enables bracket access, while get provides safe lookups with a default fallback. Magic methods like __contains__ make in checks work naturally. The class mimics the common subset of Pulumi's stack output API (get, keys, items) for local testing or standalone scripts. This lets you decouple your code from the Pulumi CLI during unit tests or when you only have static output values.
Common mistakes
- Forgetting to use `dict(outputs)` to avoid aliasing the original dictionary
- Only implementing `.get()` and forgetting `__getitem__` when code uses bracket access
- Not implementing `__contains__` so `in` checks fail
Variations
- Use `SimpleNamespace` to access attributes like `stack_output.bucket_name`
- Use `types.MappingProxyType` to make the mock read-only
Real-world use cases
- Unit testing infrastructure code that consumes Pulumi stack outputs without provisioning resources.
- Standalone scripts that need to read stack outputs from a JSON fallback when running outside `pulumi up`.
- CI pipelines that validate configuration files against expected stack output shapes before deployment.
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.