AWS CDK Aspects Compliance Enforcement Prompt
Write AWS CDK Aspects that visit the construct tree to enforce tagging, encryption, and security rules across all stacks at synth time.
- Target user
- Platform engineers enforcing guardrails in CDK pipelines
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure-as-code engineer who enforces org-wide guardrails in AWS CDK using Aspects that fail the synth before anything reaches CloudFormation. I will provide: - The CDK language (TypeScript or Python) and app structure - The policies to enforce (required tags, encryption at rest, no public S3, allowed instance types) - Whether violations should warn or hard-fail Your job: 1. **Implement the Aspect** — provide a class implementing `IAspect` with a `visit(node)` that inspects each construct in the tree. 2. **Match nodes precisely** — use the L1 Cfn resource type or `instanceof` on L2 constructs to target the right resources, and skip irrelevant nodes. 3. **Annotate, don't throw** — attach findings with `Annotations.of(node).addError()` / `addWarning()` so all violations surface in one synth, not just the first. 4. **Apply at the right scope** — show `Aspects.of(app).add(new MyAspect())` and explain app-level vs stack-level application and ordering. 5. **Handle mutation vs validation** — if the Aspect also fixes resources (e.g. adds tags), explain Aspect invocation order and the risk of mutating after synth-dependent logic. 6. **Test** — provide an assertion test that synthesizes a stack with a violation and asserts the error annotation. Output as: the Aspect class, the wiring in `app.ts`/`app.py`, and a test proving a violation fails synth. Note that addError blocks deploy while addWarning does not — choose per policy and state which you used.
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
-
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.
-
AWS CDK Construct Design Prompt
Design layered AWS CDK constructs (L2/L3) with sane defaults, escape hatches, and unit + snapshot tests — so app teams consume a paved-road construct instead of hand-rolling CloudFormation.
-
CloudFormation Template Review Prompt
Review a CloudFormation (or CDK-synthesized) template for correctness, drift resistance, and safe updates — intrinsic functions, change-set surprises, deletion policies, and the resources that recreate when you least expect it.
-
IaC Compliance Control Mapping & Evidence Prompt
Map a compliance framework's controls to enforceable infrastructure-as-code guardrails and generate the audit evidence trail — so 'we're compliant' is provable from code and CI, not asserted in a spreadsheet, across any IaC tool.
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.