Topic lessonTopic: AWS CDK: source and synthesis
AWS CDK synth explained: constructs and CloudFormation
AWS CDK describes infrastructure in program code; synthesis produces CloudFormation templates and deployment artifacts. Successful synthesis and successful deployment are different outcomes. Read a short example through containment, references and change review.
Read the relationship between apps, stacks and constructs, and distinguish reviewing synthesized artifacts from applying them to AWS.
Knowing CloudFormation Resources, logical IDs and Ref helps. Detailed TypeScript knowledge is unnecessary. Read the example here; this page does not execute CDK or access AWS.
From code to assembly, then deployment
| Stage | What it handles | What to review |
|---|---|---|
| Describe | Infrastructure declared in program code | Resources, references, conditions and defaults. |
| Synthesize | A cloud assembly: CloudFormation templates, assets and related artifacts | Generated resources and properties, plus required deployment artifacts. |
| Deploy and verify | The target AWS environment and actual functions | Impact, execution results, then traffic and application behavior. |
A cloud assembly can contain more than templates. File or image assets can be part of the deployment. Review generated infrastructure as well as the readability of its source code.
Apps, stacks and constructs organize infrastructure
- App → ReportStackContains
- ReportStack → ReportNetworkContains
- ReportStack → ReportGroupContains
- App
- Application assembling the model
- ReportStack
- The stack in this example
- ReportNetwork
- L1 declaring a VPC
- ReportGroup
- L1 declaring a security group
A construct is a building block, a stack is the unit represented by a CloudFormation stack, and an app assembles them. The diagram does not show a security group sending traffic to a VPC. Read resource references in the properties below.
Fixed example: pass a VPC reference to a security group
import { App, Stack, aws_ec2 as ec2 } from 'aws-cdk-lib';
const app = new App();
const stack = new Stack(app, 'ReportStack');
const network = new ec2.CfnVPC(stack, 'ReportNetwork', {
cidrBlock: '10.20.0.0/16'
});
new ec2.CfnSecurityGroup(stack, 'ReportGroup', {
groupDescription: 'Illustrative group',
vpcId: network.ref
});
app.synth();| Code | Meaning |
|---|---|
| new Stack(app, 'ReportStack') | Place a stack inside the app. |
| new ec2.CfnVPC(stack, 'ReportNetwork', ...) | Place an L1 VPC construct inside the stack. |
| vpcId: network.ref | Pass a VPC Ref for CloudFormation to resolve. |
| app.synth() | Generate artifacts from the app; this is not evidence of successful AWS deployment. |
Separate abstraction levels from resource identities
| Level | What it expresses | Review focus |
|---|---|---|
| L1 | A low-level construct directly representing a CloudFormation resource, commonly named Cfn… | Review required properties. |
| L2 | A higher-level API with helper behavior | Inspect generated defaults and permissions. |
| L3 / pattern | A composition of building blocks | Inspect what the concise source actually creates. |
| Identifier | Role |
|---|---|
| Construct ID | Identifies a construct within its scope. |
| CloudFormation logical ID | Tracks a resource in the template; its construct path contributes to generation. |
| Physical resource identifier | An ID or name of a created resource, not necessarily the code identifier. |
Moving constructs or changing IDs can alter logical IDs and lead to resource replacement. Code cleanup is not automatically infrastructure-neutral. Inspect generated artifacts and differences for identity or replacement changes. Compare identifiers against the actual generated artifacts.
Synthesis success is a limited claim
Synthesis runs application code. Context lookup and asset preparation can involve AWS queries or additional processing. Do not assume synthesis is unconditionally offline or harmless.
- Review the app and its building blocks, including execution and lookup conditions.
- Inspect templates and assets for changes, permissions, replacements and deletions.
- Align environment, permissions and approval, then deploy and verify actual functions.
Check your understanding
Choose an option to see the result and explanation.
Check, then explain in your own words
- Separate apps, stacks and constructs in the containment diagram.
- Read scopes and Ref in the fixed code.
- Use the questions and review to explain checks after synthesis.
Check your understanding: What should a CDK review inspect besides source code?
Inspect generated templates and assets, references and logical IDs, permissions and replacement/deletion differences, the target environment and post-deployment functions.
Readable source, generated artifacts, deployment outcomes and actual functions are separate review targets.
Sources and conditions checked
The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.
Scope and conditionsCloud assembly templates/assets and synthesizer/deployment conditions; synthesis success is not deployment success.
Used in this lessonFrom code to assembly, then deployment Synthesis success is a limited claim
- Deploy AWS CDK applicationsChecked:
Scope and conditionsApp/stack structure, application execution during synthesis, and the separation of artifacts from deployment; documentation review only.
Used in this lessonFrom code to assembly, then deployment Apps, stacks and constructs organize infrastructure Fixed example: pass a VPC reference to a security group Synthesis success is a limited claim Check your understanding
- AWS CDK constructsChecked:
Scope and conditionsConstruct containment and abstraction levels; inspect generated settings and do not interpret containment as traffic.
Used in this lessonApps, stacks and constructs organize infrastructure Separate abstraction levels from resource identities Check your understanding
- AWS CDK CfnVPC referenceChecked:
Scope and conditionsCfnVPC scope/id/cidrBlock/ref for the fixed TypeScript reading example; no compilation, synthesis or creation performed.
Used in this lessonFixed example: pass a VPC reference to a security group Check your understanding
Scope and conditionsCfnSecurityGroup initialization and groupDescription; the example is not a complete production traffic configuration.
Used in this lessonFixed example: pass a VPC reference to a security group Check your understanding
Scope and conditionsRequired groupDescription and the type and meaning of the VPC specified by vpcId.
Used in this lessonFixed example: pass a VPC reference to a security group
- Identifiers and the AWS CDKChecked:
Scope and conditionsScope-local identifiers, paths, logical and physical IDs; review refactoring risks without inventing generated hashes.
Used in this lessonSeparate abstraction levels from resource identities Synthesis success is a limited claim Check your understanding
- Context values and the AWS CDKChecked:
Scope and conditionsEnvironment-derived context and caching conditions; synthesis is not unconditionally offline.
Used in this lessonSynthesis success is a limited claim Check your understanding