This post is part of a series of introductory articles related to building AWS services using AWS CloudFormation. You can read about CloudFormation Conditions and about CloudFormation Parameters in earlier posts
By default, Amazon CloudFormation deploys resources defined in a template in parallel. When you think about it, this makes a lot of sense; deploying in parallel CloudFormation can reduce the amount of time it takes to create the services you’ve defined in your template.
In most cases, parallel deployment is acceptable, but what if you find yourself in a situation where you need a specific resource to be available before creating another? This is where the DependsOn attribute comes into play - it allows you to control the order of deployment within your template.
Here’s a simple scenario
In the diagram above, we have an RDS DB Instance and EC2 instance we need to deploy. In our scenario, we need the DB Instance to exist before the EC2 Instance.
Here’s how we can do that.
In our CloudFormation template, we define both resources and any attributes we need to be configured.
|
|
While the snippet above is technically correct, CloudFormation will deploy the EC2, Subnet Group and RDS resources simultaneously.
To control the order, we just add the DependsOn attribute - in our situation, we’d add it to the RDS resource definition. This ensures that both the EC2 Instance and the Subnet Group are deployed before the RDS DB Instance.
|
|
The DependsOn attribute is a simple but powerful tool to help you control how CloudFormation deploys and configures resources in your AWS account. We use it frequently when building templates for our Infrastructure as Code service and for our customers.
Like what you read? Why not subscribe to the weekly Orbit newsletter and get content before everyone else?