The Simplest Way to Make Cloudformation and Pytest Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair cloudformation and pytest to check cloud stacks before deployment. Cloudformation builds the cloud resources, while pytest runs automated checks against them. This pairing lets engineers test templates in a safe sandbox account. Teams can spot broken permissions, bad network rules, and missing outputs early. Instead of fixing live incidents, developers catch mistakes during code review. A good test suite runs fast and cleans up all test resources afterward. How cloudformation and p

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Teams pair cloudformation and pytest to check cloud stacks before deployment. Cloudformation builds the cloud resources, while pytest runs automated checks against them.

This pairing lets engineers test templates in a safe sandbox account. Teams can spot broken permissions, bad network rules, and missing outputs early. Instead of fixing live incidents, developers catch mistakes during code review. A good test suite runs fast and cleans up all test resources afterward.

How cloudformation and pytest share roles and data

A test workflow starts when a developer pushes a template change. The build runner logs in to the cloud account with a test role. It sends the template to the cloud service to build a temporary stack.

Once the stack finishes building, the test runner starts the Python checks. Pytest reads the stack outputs, resource names, and live settings from the cloud API. It compares those live values against the rules written by the engineering team.

Static checks can also run before any cloud resources exist. In this mode, Python scripts read the raw template file from the disk. They check that every storage bucket has encryption and every role follows team rules. Combining static file checks with live stack tests gives teams full coverage.

Data flows one way during these test runs. The cloud service sends stack state and metadata back to the test runner. The test runner then records pass or fail results in the build log. Finally, the runner deletes the temporary stack so the account stays clean.

Common failure modes and how to scope permissions

Many teams fail by giving the test runner full admin rights. A shared service account with broad keys creates a huge security risk. If an attacker steals that key, they can delete live production systems. Broad permissions also hide bugs where a stack lacks its own needed roles.

CloudFormation Guard + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Another common mistake is leaving test stacks running after a test crashes. Failed runs can leave behind costly databases and open network paths. Unlogged API calls from local developer laptops also make security audits very hard. Teams struggle to tell which engineer changed a test stack or read data.

To fix this, give the build runner a strict, scoped cloud role. That role should only create and delete resources needed for the specific test. Run all live stack tests inside an isolated sandbox account, never in production. Use short-lived session tokens from your identity provider instead of static keys.

You should also wrap stack creation in a setup and teardown block. Even if a check fails, the teardown step must delete the test stack. Tag every test resource with a build number and a time stamp. A nightly cleanup job can then delete any leftover stacks from broken runs. Keep network paths closed so the test runner only talks to required endpoints.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Should pytest build a real stack for every single test run?

No, run fast static checks on the template file for every code commit. Only build live cloud stacks when merging code or testing complex network changes.

How do we stop failed tests from leaving costly cloud resources behind?

Put your stack deletion step inside a fixture teardown block that always runs. Also run a scheduled cleanup script that removes old tagged test stacks daily.

How should developers run these cloud tests from their local laptops?

Developers should log in with short-lived roles tied to an isolated sandbox account. Never store long-lived admin keys in local files or shared team chat rooms.

The query runs. The emails stay hidden.