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.
嗨我是围我是一位软件开发人员,是AWS CloudFormation的忠实拥护者。 在本文中,我将向您展示如何解决使用AWS CloudFormation服务(循环依赖)时遇到的问题之一。
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.
让我们想象一下您有多个CloudFormation模板,分别称为core.yaml和resources.yaml。 core.yaml模板将从resources.yaml模板导入资源,而core.yaml模板取决于resources.yaml模板。 我们将需要在core.yaml之前部署resources.yaml。
The following is the dummy resources.yaml template.
以下是虚拟的resources.yaml模板。
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.
请密切注意模板中的DBSecurityGroup。 如您所见,它正在使用/引用core.yaml模板中的WebServerSecurityGroup。
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.
core.yaml包含Web服务器的安全组(WebServerSecurityGroup)和ElasticBanstalk。 同样,它是从resources.yaml模板导入资源。 以下是虚拟的resources.yaml模板。
As you can see in the template, WebDatabase is referencing/ using the DBSecurityGroup importing it from the resources.yaml.
正如您在模板中所看到的,WebDatabase正在引用/使用DBSecurityGroup从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.
您可能已经发现,这里有一个问题,就是循环依赖。 resources.yaml使用的是core.yaml中的资源,core.yaml使用的是resources.yaml中的资源。 我们不能首先部署任何模板。 现在,我们在部署模板时遇到问题。
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.
作为解决方案,我们可以创建第三个模板,并从resources.yaml导入或使用的core.yaml中声明资源。 我将第三个模板命名为base.yaml。 以下是伪代码。
We basically just moved the web security group from the core.yaml into the third template.
我们基本上只是将Web安全组从core.yaml移到了第三个模板中。
Now, we can deploy our templates in the following order.
现在,我们可以按以下顺序部署模板。
- base.yaml base.yaml
- resources.yaml resources.yaml
- core.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/.
如果您有任何疑问或找到解决问题的更好方法,可以在https://www.linkedin.com/company/linkedin/处告诉我。