如何在cloudformation中指定子网和VPC ID?

red*_*888 0 amazon-ec2 amazon-web-services aws-cloudformation amazon-vpc

我不想创建新的子网和VPC,已经创建了它们,并且希望我的cloudformation模板使用它们。

我在哪个参数中指定此参数,或者我对此工作方式感到困惑?

当我查看“ AWS :: EC2 :: VPC”和“ AWS :: EC2 :: Subnet”的文档时,似乎这些资源仅用于创建新的 VPC,并且子网正确吗?

我是否应该仅将实例资源直接指向我要使用的现有VPC和子网?

编辑:

例如,如果我的模板中有一个实例资源,并且将其直接指向这样的现有子网:

{
  "Resources": {
    "MyServer": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": {
          "Ref": "InstanceType"
        },
"SubnetId": {
  "Ref": "subnet-abc123"
},
...
Run Code Online (Sandbox Code Playgroud)

验证模板时出现此错误:

Template contains errors.: Template format error: Unresolved resource dependencies [subnet-abc123] in the Resources block of the template
Run Code Online (Sandbox Code Playgroud)

我尝试通过映射执行此操作,但仍然出现错误。我在映射中有这个:

  "Mappings": {
    "SubnetID": {
      "TopKey": {
        "Default": "subnet-abc123"
      }
    }
Run Code Online (Sandbox Code Playgroud)

而这在实例资源中:

"SubnetId": {
  "Fn::FindInMap": [
    "SubnetID",
    {
      "Ref": "TopKey"
    },
    "Default"
  ]
}
Run Code Online (Sandbox Code Playgroud)

尝试验证时出现此错误:

Template contains errors.: Template format error: Unresolved resource dependencies [TopKey] in the Resources block of the template
Run Code Online (Sandbox Code Playgroud)

Joh*_*ein 6

如果您希望使用特定的 VPC 和 subnet,只需插入它们的值:

{
  "Resources": {
    "MyServer": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "SubnetId": "subnet-abc123",
        "ImageId": "ami-abcd1234"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

子网始终属于 VPC,因此指定子网将自动选择匹配的 VPC。


hel*_*loV 5

Parameters部分中指定它们,然后在Resources部分中参考它们。CF将允许您先选择VPC,然后再选择Subnet

  "Parameters" : {

    "VpcId" : {
      "Type" : "AWS::EC2::VPC::Id",
      "Description" : "VPCId of Virtual Private Cloud (VPC).",
      "Default" : ""
    },

    "VpcSubnet": {
      "Description" : "SubnetId in VPC",
      "Type" : "AWS::EC2::Subnet::Id",
      "Default" : ""
    },


  "Resources" : {
    ...
    "Ec2Instance" : {
      "Properties" : {
        "SubnetId" : { "Ref" : "VpcSubnet" },
Run Code Online (Sandbox Code Playgroud)