Call dependent.node.addDependency(prerequisite) when one resource must be created after another and their properties do not already express that relationship. CDK translates construct dependencies into CloudFormation ordering where applicable.
First check whether a reference already establishes the order. Passing a VPC to an EC2 construct, for example, creates references to network resources. Adding the same ordering manually is usually unnecessary.
A database and client example
The following stack demonstrates the syntax. It creates billable resources and deliberately omits application startup code. Instantiate DependencyStack from a CDK v2 app:
import { Stack, StackProps } from "aws-cdk-lib"; import * as ec2 from "aws-cdk-lib/aws-ec2"; import * as rds from "aws-cdk-lib/aws-rds"; import { Construct } from "constructs"; export class DependencyStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const vpc = new ec2.Vpc(this, "Network", { maxAzs: 2, natGateways: 0, subnetConfiguration: [{ name: "isolated", subnetType: ec2.SubnetType.PRIVATE_ISOLATED, cidrMask: 24, }], }); const database = new rds.DatabaseInstance(this, "Database", { vpc, vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED }, engine: rds.DatabaseInstanceEngine.postgres({ version: rds.PostgresEngineVersion.VER_16 }), instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MICRO), storageEncrypted: true, publiclyAccessible: false, }); const client = new ec2.Instance(this, "Client", { vpc, vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED }, instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MICRO), machineImage: ec2.MachineImage.latestAmazonLinux2023(), }); client.node.addDependency(database); } }
The client variable avoids shadowing the ec2 namespace. The explicit dependency says to complete creation of the database resource before creating the client. It does not configure database ingress, supply credentials, install a database client, or run a migration.
This example targets CDK 2.272.0 and constructs 10.5.0 and has not been deployed to AWS. Use the full RDS and EC2 guide for connection configuration.
What to inspect during synthesis
Run npx tsc --noEmit, then npx aws-cdk synth. Inspect the EC2 resource's DependsOn and references. A dependency from A to B means B is created first; it does not mean A triggers a replacement every time B changes.
For stacks, consumerStack.addDependency(producerStack) controls deployment order. For values shared across stacks, understand outputs and exports; a dependency alone does not transmit a value.
Avoid circular dependencies
If A depends on B and B depends on A, adding more explicit dependencies cannot resolve the cycle. Inspect references, security-group rules, and resource ownership. Move the shared interface or a separately managed rule to a place that breaks the cycle.
AWS documents ordering semantics in CloudFormation DependsOn. Runtime readiness still belongs in application retries, health checks, or a deliberately designed initialization workflow.
Review cdk diff before deployment. On cleanup, the database's default snapshot policy may preserve data and incur storage charges; inspect the proposal and retained resources rather than assuming destroy removes everything.
Quick Recap
How do you add an explicit CDK dependency?
Call dependent.node.addDependency(prerequisite). For stack-level deployment order, use consumerStack.addDependency(producerStack).
Do all resources require addDependency?
No. References between resources usually allow CloudFormation to infer dependencies. Use explicit ordering when the required relationship is not represented by a resource reference.
Does DependsOn guarantee application readiness?
No. It orders CloudFormation resource operations. It does not run database migrations, wait for arbitrary application health checks, or prove a client can connect.
Comments
Share your thoughts and questions below