AWS CDK
AWS CDK lets you express AWS infrastructure through programming-language constructs that produce CloudFormation templates. Use that abstraction to make repeated infrastructure intent easier to maintain, while continuing to review the generated resources and their deployment behavior.
The program, synthesized template, proposed stack change, and running resources are different forms of evidence. A passing unit test can verify a template property without proving that credentials, quotas, network access, or a stateful migration will work in an account.
TL;DR
- Constructs compose infrastructure configuration; their abstraction level determines how much is expressed for you.
- Synthesis produces deployment artifacts. It does not itself demonstrate that a resource was created or behaves correctly in AWS.
- Assert important generated properties and policies rather than accepting every changed snapshot automatically.
- Preserve resource identity during refactoring and inspect potential replacements before deployment.
- Include account-specific integration tests, recovery decisions, and retained-resource ownership in the release process.
Quick Example
In a disposable Node.js project, install aws-cdk-lib and constructs, record the resolved versions, and save the following as queue.cjs. Run node queue.cjs. This creates local synthesis output in cdk.out; it contains no deployment command or account lookup.
The example tests explicit encryption, transport-policy, timeout, and retention intent in the generated template. The queue construct reference describes these options. The values are illustrative: a real worker's visibility timeout must match its processing and retry design.
Retention is deliberate here, but it is not a backup or a complete recovery plan. If this queue were deployed and later retained, someone would still own its costs, access policy, eventual cleanup, and any message-lifetime constraints. No such resource is created by this exercise.
Core Concepts
Constructs and stacks
CDK constructs represent resources and configuration as composable objects. Low-level L1 constructs closely map to CloudFormation resources. Higher-level constructs add APIs and combinations that can reduce repetitive configuration.
Choose the abstraction that keeps the important decisions visible. A reusable construct is useful when it captures a coherent policy with documented inputs. A large wrapper that conceals access grants, deletion behavior, and network exposure can make review harder even if its call site is short.
Synthesis and deployment
Synthesis turns the application and its inputs into a cloud assembly containing templates and related artifacts. Deployment then uses the relevant AWS mechanisms to apply changes. Some applications also perform context lookups or prepare assets, so do not assume every CDK program is an offline, side-effect-free text conversion.
The example is intentionally limited to local constructs without lookups or deploy actions. For a real project, pin and review dependencies, account/region context, asset-building steps, and configuration inputs so the artifact being approved can be reproduced.
Logical identity and physical resources
CloudFormation tracks resources by logical IDs. The CDK identifier guide explains how construct paths contribute to those identities. Renaming or moving constructs can change the generated IDs and turn a refactor into resource creation and removal.
Inspect before and after templates when extracting a construct or changing its structure. A source-code change that preserves application behavior does not automatically preserve deployed resource identity.
Test at the Right Boundary
AWS's testing guide separates fine-grained assertions from snapshots and notes that CDK upgrades can change synthesized output. Make assertions about the properties that matter to the product. A blanket snapshot approval can conceal precisely the drift the test was intended to expose.
Also prove important assertions can fail. Temporarily change a property in a disposable test variant and verify the expected assertion rejects it. This checks that a matching resource was actually selected and the test is checking the intended requirement.
Review Diffs with Lifecycle Consequences
Use the CDK diff command in the intended environment and inspect how its comparison was produced. Its documented methods differ: a template comparison and a CloudFormation change-set-based comparison provide different replacement information. A diff may contact AWS and create a change set; it is not always an offline local operation.
For each stateful change, identify retained data, affected clients, physical names, replacement constraints, and the transition path. CloudFormation update behavior can involve no interruption, interruption, or replacement depending on the resource property. “The stack rolled back” is not evidence that an application's data or external effects returned to their previous state.
If safe in-place replacement is impossible, design a staged migration: introduce the new resource, move compatible consumers and data through an approved process, verify behavior, then retire the old resource. The precise steps depend on the service; do not automate a generic delete-and-recreate path for every resource type.
Best Practices
Keep identity changes visible
Treat construct IDs and paths as part of deployment review after a resource exists. Compare generated logical IDs before accepting a refactor, and use supported identity-preserving approaches when appropriate.
Make permissions and retention explicit
Review generated grants and resource policies alongside deletion and replacement behavior. A construct's convenient defaults do not establish the application's access or recovery requirements.
Validate through a controlled environment
Separate local template checks from approved account integration. Verify producer/consumer access, retry behavior, monitoring, and recovery using test resources before proposing production changes.
Common Mistakes
Bad: Treat a passing synthesis test as proof that the service was deployed securely.
Correct: State exactly which template properties passed, then separately verify effective permissions and operating behavior in the intended account.
Bad: Rename a stateful construct and assume a code refactor cannot affect data.
Correct: Inspect logical IDs, replacements, retention, and consumer references before deployment.
Bad: Accept every new snapshot after a dependency upgrade.
Correct: Explain the generated differences and retain targeted assertions for policy and lifecycle requirements.
FAQ
Does CDK replace CloudFormation?
For AWS CDK stacks, CloudFormation remains the deployment mechanism represented by the generated templates. Its resource update and replacement constraints continue to matter.
Do I need AWS credentials for this example?
The example uses local synthesis without lookups or deployment. Other CDK applications can require credentials during preparation, so inspect the application rather than assuming all synthesis is offline.
Are higher-level constructs always preferable?
Use them when their behavior and extension points fit. Choose lower-level control when required properties or review clarity justify it, and document any conventions the application must preserve itself.
Does retaining a resource guarantee recovery?
No. Retention affects resource lifecycle when management changes; recovery also depends on the service's data lifetime, access, backups where applicable, and a tested operating procedure.
Related Topics
- Infrastructure as Code — Review plans, state, and change ownership.
- CloudFormation — Understand deployment and update behavior.
- AWS — Place resources in the broader platform.
- CI/CD — Carry verified artifacts into controlled releases.