Sub topic 10
Implicit and explicit dependencies
CloudFormation builds resources in an order it works out from your template. You don't write the order down. This chapter explains how it works out that order, when you need to override it, and which mistakes cause failures. It assumes you can read a YAML template and know Ref, Fn::GetAtt and Fn::Sub from the intrinsic functions chapter.
Why ordering matters
Some resources can't exist until another one does. A subnet needs its VPC. An instance profile needs its role. If CloudFormation created everything at once, many of these calls would fail.
CloudFormation builds a dependency graph from the template. It creates resources in parallel when nothing links them, and in sequence when one needs another. On delete, it reverses the order. A resource is deleted only after everything that depends on it is gone.
Implicit dependencies
An implicit dependency appears when one resource's properties refer to another resource. These intrinsic functions create one:
Refon a resource's logical IDFn::GetAtton a resource's attributeFn::Subwith${LogicalId}or${LogicalId.Attribute}
In this example, the instance profile refers to the role, so the role is created first.
AWSTemplateFormatVersion: "2010-09-09"
Description: Role and instance profile with an implicit dependency
Resources:
AppRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
AppProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref AppRoleNo extra configuration is needed. !Ref AppRole tells CloudFormation that AppProfile depends on AppRole.
Not every reference creates a dependency. Ref on a parameter or a pseudo parameter such as AWS::Region does not, because those values exist before any resource is created.
Explicit dependencies with DependsOn
Sometimes a resource needs another one but doesn't refer to it in any property. For these cases, add DependsOn to the resource. It accepts one logical ID or a list.
A common case is a public route that needs an internet gateway attached to the VPC. The route refers to the gateway, but the gateway's attachment is a separate resource. The route never refers to the attachment, so nothing orders them.
Resources:
Vpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Igw:
Type: AWS::EC2::InternetGateway
IgwAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref Vpc
InternetGatewayId: !Ref Igw
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref Vpc
DefaultRoute:
Type: AWS::EC2::Route
DependsOn: IgwAttachment
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IgwWithout DependsOn, CloudFormation could try to create the route before the gateway is attached. That call fails.
DependsOn is a property of the resource itself, written at the same level as Type and Properties. It is not inside Properties.
Reading the graph
Here Vpc and Igw have no dependencies, so CloudFormation creates them together. IgwAttachment and RouteTable follow, also in parallel. DefaultRoute goes last.
Circular dependencies
If A depends on B and B depends on A, CloudFormation can't find a valid order. It rejects the template with a "Circular dependency between resources" error.
A frequent cause is two security groups that each allow traffic from the other, written as inline rules. The fix is to create both groups first, with no rules. Then add the rules as separate AWS::EC2::SecurityGroupIngress resources, which refer to both groups.
Resources:
WebSg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Web tier
VpcId: !Ref Vpc
DbSg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Database tier
VpcId: !Ref Vpc
DbFromWeb:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !GetAtt DbSg.GroupId
SourceSecurityGroupId: !GetAtt WebSg.GroupId
IpProtocol: tcp
FromPort: 5432
ToPort: 5432This snippet assumes a Vpc resource exists elsewhere in the template.
Common mistakes
- Using DependsOn where Ref would do. An unnecessary
DependsOnhides the real relationship and can slow the stack by forcing sequence. Prefer a reference. - Misspelling a logical ID.
DependsOnmust name a resource in the same template. An unknown name fails validation. - Expecting order from the template layout. The order in which you write resources has no effect.
- Expecting readiness. A dependency means the other resource's creation is complete from CloudFormation's point of view. For some services, the resource may still need time before it works. Check the resource type's documentation for such cases.
Dependencies and updates
The graph also governs updates. When a change replaces a resource, CloudFormation creates the new one, updates the dependents to point at it, and then deletes the old one in a cleanup step. A wrong or missing dependency can therefore show up as a failure during an update, not only at creation.
Exercise
A template has an AWS::EC2::Route that uses !Ref on an internet gateway, and a separate AWS::EC2::VPCGatewayAttachment for the same gateway. Stack creation sometimes fails because the route is created before the gateway is attached. What is the smallest change that fixes it, and why does it work?
Show answer
Add DependsOn: IgwAttachment to the route resource. The route refers only to the gateway, so CloudFormation sees no link to the attachment and may create them in parallel. The explicit dependency forces the attachment to finish first.
Exercise
Two security groups each need an inline rule that allows traffic from the other, and CloudFormation reports a circular dependency. How do you resolve it?
Show answer
Remove the inline rules from both groups. Add the rules as separate AWS::EC2::SecurityGroupIngress resources that use GetAtt to refer to both groups. The groups then have no dependency on each other, and each ingress resource depends on both.