在创建子网时引用CloudFormation脚本中的现有AWS VPC Id

时间:2014-11-03 05:24:57

标签: amazon-web-services subnet vpc

如何在CloudFormation脚本中引用现有VPC的VPC ID(之前已在单独的CloudFormation脚本中创建)以便在VPC中创建子网?

3 个答案:

答案 0 :(得分:8)

在定义VPC的模板中,在输出部分包含VPC ID:

"Outputs" : {
    "VPC" : {
        "Value" : {"Ref":"VPC"},
        "Description" : "VPC ID"
    },
    ...
}

在使用VPC的堆栈模板中,为VPC ID定义参数:

"Parameters" : {
    "VPC" : {
        "Type" : "String",
    },
    ...
}

创建此堆栈时,请在VPC定义堆栈上调用describe-stack以从输出中获取ID,并将其作为VPC参数传递给create-stack

答案 1 :(得分:4)

或者从输入中获取vpc id,例如

 "VpcId" : {
      "Type" : "AWS::EC2::VPC::Id",
      "Description" : "VpcId of your existing Virtual Private Cloud (VPC)",
      "ConstraintDescription" : "must be the VPC Id of an existing Virtual Private Cloud."
    },

答案 2 :(得分:-2)

按名称引用它 即。 "VpcId" : { "Ref" : "myVPC" }, 在类似的事情中:

    {
   "Type" : "AWS::EC2::Subnet",
   "Properties" : {
      "AvailabilityZone" : String,
      "CidrBlock" : String,
      "Tags" : [ Resource Tag, ... ],
      "VpcId" : { "Ref" : String }
      }
    }  

此处的文档: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html