简体   繁体   English

如何解决 AWS Cloudformation 中的循环依赖

[英]How to resolve a circular dependency in AWS Cloudformation

I created an AWS Cloudformation template and I'm having trouble getting over a circular dependency.我创建了一个 AWS Cloudformation 模板,但无法克服循环依赖。 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.我正在创建一个 EC2 实例和一个负载均衡器,负载均衡器依赖于 EC2 实例,因为它在其 Instances 属性中引用了它。 Everything was working fine until I had to reference the load balancer's DNSName property inside the EC2 instance Init section.一切正常,直到我不得不在 EC2 实例初始化部分中引用负载均衡器的 DNSName 属性。

"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" ] }, "\"" ] ] }
          },
         }

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.所以这里的问题是,EC2 实例无法引用负载均衡器,因为它尚未创建,并且无法首先创建负载均衡器,因为它需要 EC2 实例 ID 以便它可以与其关联。

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.我查看了 WaitConditions 和 CreationPolicy,但除非我误解了它们的工作方式,否则我认为它们不会帮助我。 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.基本上我需要的是 a) 创建 EC2 实例但在创建 LoadBalancer 之前不要执行 UserData 或 b) 首先创建 LoadBalancer 并且不要将其与 EC2 实例关联,一旦创建了 EC2 实例就返回并更新 LoadBalancer 以将其与 EC2 实例相关联。 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.理想情况下,我希望这发生在堆栈的单个创建中。

You could look into using the cfn-get-metadata helper script from within the EC2 instance to retrieve Stack::Resource information.您可以考虑使用 EC2 实例中的 cfn-get-metadata 帮助程序脚本来检索 Stack::Resource 信息。

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-get-metadata.html http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-get-metadata.html

You may need to add Ref with each resource logical name.您可能需要为每个资源逻辑名称添加Ref

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>

Adding Ref resolves it as following:添加Ref将其解析如下:

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

Note: I am using YAML since JSON is horrible with Cloudformation.注意:我正在使用 YAML,因为 Cloudformation 的 JSON 很糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM