Resolving Circular Dependency between multiple AWS CloudFormation templates

Hi, I am Wai. I am a software developer who is a big fan of AWS CloudFormation. In this article, I will show you how to resolve one of the issues I experienced using AWS CloudFormation service which is Circular Dependency.

First, let’s try to understand what the problem is.

Let’s imaging you have multiple CloudFormation templates, called core.yaml and resources.yaml. The core.yaml template will be importing the resources from resources.yaml template which is the core.yaml template is depending on the resources.yaml template. We will need to deploy the resources.yaml before the core.yaml.

The following is the dummy resources.yaml template.

Pay close attention to the DBSecurityGroup in the template. As you can see it is using/ referencing WebServerSecurityGroup which is in the core.yaml template.

The core.yaml contains security group for the webserver (WebServerSecurityGroup) and ElasticBanstalk. Also, it is importing the resources from the resources.yaml template. Following is the dummy resources.yaml template.

As you can see in the template, WebDatabase is referencing/ using the DBSecurityGroup importing it from the resources.yaml.

As you might have spotted we have a problem here which is Circular Dependency. The resources.yaml is using the resources from core.yaml and core.yaml is also using the resources from resources.yaml. We cannot deploy any of the templates first. We have an issue with deploying the template now.

Solution:

As a solution, we can create the third template and declare the resources from the core.yaml that the resources.yaml is importing or using into the third template. I name the third template as base.yaml. Following is the dummy code.

We basically just moved the web security group from the core.yaml into the third template.

Now, we can deploy our templates in the following order.

  • base.yaml
  • resources.yaml
  • core.yaml

If you have any questions or found a better way to solve the problem, you can let me know here, https://www.linkedin.com/company/linkedin/.

Software Developer based in the UK, used to work in Singapore, but originally from Myanmar.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store