AWS CDK Unit & Snapshot Testing Prompt
Build a layered test suite for AWS CDK apps — fine-grained assertions, snapshot tests, and aws-cdk-assertions matchers — so synthesized CloudFormation is verified before it ever reaches an account.
- Target user
- Engineers writing AWS CDK constructs and stacks who want regression-proof infrastructure
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a CDK practitioner who treats synthesized templates as testable artifacts and catches IaC regressions in CI, not in prod. I will provide: - Language (TypeScript/Python/Java) and CDK version - The constructs/stacks under test (and which are reused widely) - Critical invariants (encryption on, public access off, required tags, retention) - Current test setup (if any) and CI runner - Pain points (flaky snapshots, slow tests, false confidence) Your job: 1. **Test pyramid for CDK** — explain the three layers and when to use each: fine-grained assertions (`Template.hasResourceProperties`) for invariants, snapshot tests for "did anything change?", and integration tests (`integ-tests`) for real deploy/destroy of high-risk constructs. Most coverage should be fine-grained, not snapshots. 2. **Fine-grained assertions** — write `aws-cdk/assertions` checks for my critical invariants using `Match.objectLike`, `Match.arrayWith`, and `Capture`. Assert on intent (e.g. "bucket has SSE + blockPublicAccess") not on incidental properties. 3. **Snapshot discipline** — show a single snapshot test, but warn about its failure mode: snapshots fail on every intended change and breed rubber-stamp updates. Use them as a tripwire, keep them narrow, and pair with fine-grained tests for the things that matter. 4. **Construct-level tests** — test reusable constructs in isolation with a throwaway test stack so consumers inherit guaranteed defaults (encryption, removal policy, tags). 5. **Policy/aspect tests** — if using Aspects or cdk-nag, assert that suppressions are explicit and that the security baseline holds across the synthesized app. 6. **Determinism** — eliminate snapshot churn from asset hashes, timestamps, and account/region; pin context and use `App` test fixtures so synth is reproducible. 7. **CI wiring** — fast unit/assertion tests on every PR; gated `integ-tests` (real deploy) on a schedule or label, never blocking every commit. Output: (a) a worked test file with fine-grained assertions for my invariants, (b) one narrow snapshot test with churn-proofing notes, (c) a reusable construct test, (d) the CI job split (fast vs integ), (e) guidance on when a failing snapshot should be re-baselined vs investigated. Bias toward: assertion tests over snapshots, intent over incidental properties, and deterministic synth.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
CloudFormation to AWS CDK Migration Prompt
Migrate hand-written CloudFormation templates to AWS CDK incrementally — via cdk migrate, CfnInclude, and stack import — without recreating live resources or losing drift history.
-
AWS CDK Escape Hatches & L1 Override Prompt
Use CDK escape hatches (addPropertyOverride, addOverride, addDeletionOverride, and L2-to-L1 access) to set properties the L2 construct does not expose, without forking constructs or dropping to raw CloudFormation.
-
CloudFormation Dynamic References for SSM & Secrets Manager Prompt
Replace hardcoded secrets and config in CloudFormation templates with resolve dynamic references to SSM Parameter Store and Secrets Manager, including versioning, rotation, and no-echo handling.
-
CloudFormation Macros & Template Transform Authoring Prompt
Design and safely operate CloudFormation macros (Lambda-backed template transforms) to DRY up templates, inject standards, and expand shorthand — with idempotency, error handling, and change-set safety.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.