Sub topic 9
Implicit and explicit dependencies
This chapter assumes you can read a CloudFormation YAML template and know Ref, Fn::GetAtt and Fn::Sub. It covers how CloudFormation decides the order in which it creates, updates and deletes resources.
Why order matters
A subnet can't exist before its VPC, and an instance can't launch before its security group. CloudFormation builds a dependency graph from your template. It creates independent resources in parallel and dependent ones in sequence. It deletes them in the reverse order.
Implicit dependencies
An implicit dependency appears when one resource refers to another. These intrinsic functions create one:
Refon a resourceFn::GetAtton a resourceFn::Subwith a${LogicalId}or${LogicalId.Attribute}placeholder
Resources:
AppVpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
AppSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref AppVpc
CidrBlock: 10.0.1.0/24AppSubnet uses !Ref AppVpc, so CloudFormation creates the VPC first. You don't declare the order yourself.
Explicit dependencies with DependsOn
Sometimes one resource needs another but never refers to it. The DependsOn attribute handles this case. It takes one logical ID or a list of them.
A common case is an internet-facing resource that needs a gateway attachment. The route below uses the gateway's ID, but it needs the attachment to be complete too.
Resources:
AppVpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Igw:
Type: AWS::EC2::InternetGateway
IgwAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref AppVpc
InternetGatewayId: !Ref Igw
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref AppVpc
DefaultRoute:
Type: AWS::EC2::Route
DependsOn: IgwAttachment
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IgwWithout DependsOn, CloudFormation could create the route as soon as the gateway exists. That can fail because the gateway isn't attached to the VPC yet.
Rules and common errors
- Prefer implicit dependencies. Use
DependsOnonly when no reference exists. ExtraDependsOnentries slow deployments because they remove parallelism. - Circular dependencies fail. If A depends on B and B depends on A, validation fails with a "Circular dependency" error. Break the cycle, for example by moving a shared rule into a separate resource such as
AWS::EC2::SecurityGroupIngress. - Names must be logical IDs.
DependsOntakes the template's resource names, not physical IDs or ARNs. - Conditions apply. A resource that depends on a conditional resource that wasn't created can cause confusing failures. Give both the same condition.
- Updates and deletes follow the graph. On delete, dependents are removed first.
Exercise
A Lambda function's code reads a DynamoDB table name from a hard-coded string, so the template never uses Ref or GetAtt for the table. The function fails on first invocation because the table doesn't exist yet. How do you make CloudFormation create the table first?
Show answer
Add DependsOn: MyTable (the table's logical ID) to the function resource. A better fix is to pass the name with !Ref MyTable in an environment variable, which creates an implicit dependency.