【问题标题】:AWS CloudFormation identify IP of another instance - circular dependencyAWS CloudFormation 识别另一个实例的 IP - 循环依赖
【发布时间】:2015-11-27 22:12:07
【问题描述】:

我正在使用 CloudFormation 模板配置两个实例。 “主”和“从”。

在 userdata 脚本中,我需要将 slave 的私有 IP 传递给 master,并将 master 的 IP 传递给 slave。

这是我的模板:

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "",
  "Parameters" : {
  },
  "Resources" : {
    "InstanceSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Default Ports",
        "SecurityGroupIngress" : [ 
        { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"}
         ]
         }
         },
      "MASTER" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "Tags":[{"Key":"Name", "Value":"MASTER"}],
        "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
        "KeyName" : "mykey",
        "ImageId" : "ami-a25415cb",
        "InstanceType": "m1.large",
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -ex", "\n",
          "wget https://s3.amazonaws.com/mybucket/bootstrap.sh","\n",
          "ROLE=MASTER SLAVEIP=",?????," sh bootstrap.sh","\n"
         ] ] } }
      }
      },
      "SLAVE" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "Tags":[{"Key":"Name", "Value":"SLAVE"}],
        "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
        "KeyName" : "mykey",
        "ImageId" : "ami-a25415cb",
        "InstanceType": "m1.large",
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -ex", "\n",
          "wget https://s3.amazonaws.com/mybucket/bootstrap.sh","\n",
          "ROLE=SLAVE MASTERIP=",?????," sh bootstrap.sh","\n"
         ] ] } }
      }
      },
  },
  "Outputs" : {
  }

}

什么是正确的替代品??????如果有可能,如果没有 - 我可以使用什么替代方案?

UPD:发现这个:{“Fn::GetAtt”:[“MASTER”,“PrivateIp”]},它自己工作正常,但失败并出现“模板验证错误:资源之间的循环依赖:[SLAVE, MASTER]" 如果我​​尝试同时使用主 IP 和从 IP。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    如果您使用 VPC 和子网,您可以通过为每个实例创建一个 AWS::EC2::NetworkInterface 来做到这一点。然后在用户数据中使用{ "Fn::GetAtt": [ "MyNetworkInterface", "PrimaryPrivateIpAddress" ] }来引用网络接口的内部IP地址

    您使用 NetworkInterfaces 属性将网络接口与 EC2 实例相关联

    ...
    "MasterNetInt" : {
      "Type" : "AWS::EC2::NetworkInterface",
      "Properties" : {
        "SubnetId": { "Ref" : "MySubnet" }
      }
    },
    "SlaveNetInt" : {
      "Type" : "AWS::EC2::NetworkInterface",
      "Properties" : {
        "SubnetId": { "Ref" : "MySubnet" }
      }
    },
    
    
    "Master" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
        "KeyName" : "mykey",
        "ImageId" : "ami-a25415cb",
        "InstanceType": "m1.large",
        "SubnetId": { "Ref" : "MySubnet" },
        "NetworkInterfaces": [ { "Ref" : "MasterNetInt" } ],
        "UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -ex", "\n",
          "wget https://s3.amazonaws.com/mybucket/bootstrap.sh","\n",
          "ROLE=MASTER SLAVEIP=", { "Fn::GetAtt": [ "SlaveNetInt", "PrimaryPrivateIpAddress" ] }," sh bootstrap.sh","\n"
         ] ] } }
      }
    },
    "Slave" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
        "KeyName" : "mykey",
        "ImageId" : "ami-a25415cb",
        "InstanceType": "m1.large",
        "SubnetId": { "Ref" : "MySubnet" },
        "NetworkInterfaces": [ { "Ref" : "SlaveNetInt" } ],
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -ex", "\n",
          "wget https://s3.amazonaws.com/mybucket/bootstrap.sh","\n",
          "ROLE=SLAVE MASTERIP=", { "Fn::GetAtt": [ "MasterNetInt", "PrimaryPrivateIpAddress" ] }," sh bootstrap.sh","\n"
         ] ] } }
      }
    }
    ...
    

    如果您不熟悉设置 VPC 和子网,请阅读以下文档:http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Scenario1.html

    并参考这些模板示例:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-services-us-west-2.html#d0e113371

    基本要求是:

    AWS::EC2::VPC
    AWS::EC2::InternetGateway
    AWS::EC2::VPCGatewayAttachment
    AWS::EC2::RouteTable
    AWS::EC2::Route
    AWS::EC2::Subnet
    AWS::EC2::SubnetRouteTableAssociation
    AWS::EC2::NetworkAcl
    AWS::EC2::SubnetNetworkAclAssociation
    AWS::EC2::NetworkAclEntry
    
    AWS::EC2::NetworkInterface
    AWS::EC2::Instance
    

    【讨论】:

    • 谢谢亚历克斯!我实际上想出了如何构建所需的配置,只知道主机上的从机 IP,所以现在一切就绪,但会保留你对下一个项目的建议。
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2021-04-16
    • 2016-10-02
    • 2018-10-13
    • 2022-11-10
    • 2019-01-01
    • 2018-12-16
    相关资源
    最近更新 更多