如何解决AWS Cloudformation中的循环依赖关系 [英] How to resolve a circular dependency in AWS Cloudformation

查看:112
本文介绍了如何解决AWS Cloudformation中的循环依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个AWS Cloudformation模板,但无法克服循环依赖关系。我正在创建一个EC2实例和一个负载均衡器,该负载均衡器取决于EC2实例,因为它在其Instances属性中引用了它。一切工作正常,直到我不得不在EC2实例Init部分中引用负载平衡器的DNSName属性。

I created an AWS Cloudformation template and I'm having trouble getting over a circular dependency. I'm creating an EC2 instance and a load balancer, the load balancer depends on the EC2 instance because it references it in its Instances properties. Everything was working fine until I had to reference the load balancer's DNSName property inside the EC2 instance Init section.

"AppServer": {
  "Type": "AWS::EC2::Instance",
  "Metadata": {
    "AWS::CloudFormation::Init" : {
      "configSets" : {
        "Install" : [ "Install" ]
      },
       ...
       ...
       ...
      "Install" : {
        "commands" : {
           "update hostname rmi" : {
            "command" : { "Fn::Join" : [ "", [ "runuser -lm rlt -c \"/home/ec2-user/awscf/update-rmi.sh ", { "Fn::GetAtt" : [ "WebLoadBalancer", "DNSName" ] }, "\"" ] ] }
          },
         }

因此,这里的问题是EC2实例因为未创建而无法引用负载均衡器,并且因为需要负载均衡器而无法首先创建负载均衡器EC2实例ID,以便可以与其关联。

So the problem here is that the EC2 instance can't reference the load balancer because it hasn't been created and the load balancer can't be created first because it needs the EC2 instance ID so it can be associated to it.

我查看了WaitConditions和CreationPolicy,但是除非我误解了它们的工作方式,否则我认为它们不会对我有所帮助。本质上,我需要的是a)创建EC2实例,但在创建LoadBalancer之前不执行UserData或b)创建EC2实例后,首先创建LoadBalancer并不要将其与EC2实例相关联并更新LoadBalancer以将其与EC2实例关联。有人知道这样的事情是否可能吗?除了创建完整堆栈,然后返回并手动更新之外?理想情况下,我希望这种情况在堆栈的单个创建中发生。

I looked at WaitConditions as well as CreationPolicy but unless I'm misunderstanding the way they work I don't think that they will help me. Essentially what I need is to a) Create the EC2 instance but don't execute UserData until the LoadBalancer has been created or b) Create the LoadBalancer first and don't associate it with the EC2 instance, once the EC2 instance is created go back and update the LoadBalancer to associate it with the EC2 instance. Does anyone know if something like that is possible? Outside of creating the full stack then going back and updating it manually? Ideally I'd like this to happen on the single creation of the stack.

推荐答案

您可能需要添加 Ref 以及每个资源的逻辑名称。

You may need to add Ref with each resource logical name.

当我遇到以下情况时,我遇到了相同的错误(循环依赖):

I got the same error (Circular Dependency ) when I had:

  UserData:
    Fn::Base64:
      !Sub  |
      #!/bin/bash -xe
      # ...
      echo "<h1>I amrunning on ASG ${MyAutoScalingGroup}</h1>

添加 Ref 可以解决以下问题:

Adding Ref resolves it as following:

  UserData:
    Fn::Base64:
      !Sub  |
      #!/bin/bash -xe
      # ...
      echo "<h1>I amrunning on ASG ${!Ref MyAutoScalingGroup}</h1>




注意:我正在使用YAML,因为JSON在Cloudformation中很糟糕。 / p>

Note: I am using YAML since JSON is horrible with Cloudformation.

这篇关于如何解决AWS Cloudformation中的循环依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆