在 cloudformation json 模板中添加 PublicSubnet/PrivateSubnet 的正确方法?

Proper way to add PublicSubnet/PrivateSubnet in cloudformation json template?

我试图在大量 Cloudformation json 模板中添加 VPC。 这是我在 "Parameters" 下添加的内容:

       "VpcId" : {
            "Description" : "VpcId of your existing Virtual Private Cloud (VPC)",
            "Type" : "String"
        }

这是我在 "Resources" 下添加的:

"VPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "172.0.0.0/16",
                "EnableDnsSupport": true,
                "EnableDnsHostnames": true
            }
        },
        "PublicSubnet" : {
          "Type" : "AWS::EC2::Subnet",
          "Properties" : {
            "VpcId" : { "Ref" : "VpcId" },
            "CidrBlock": { "Fn::FindInMap" : [ "SubnetConfig", "Public", "CIDR" ]},
            "Tags": [
              { "Key": "Application", "Value": { "Ref" : "AWS::StackName" } },
              { "Key": "Network", "Value": "Public" }
            ]
          }
        },
            "PrivateSubnet" : {
          "Type" : "AWS::EC2::Subnet",
          "Properties" : {
            "VpcId" : { "Ref" : "VpcId" },
            "CidrBlock" : { "Fn::FindInMap" : [ "SubnetConfig", "Private", "CIDR" ]},
            "Tags" : [
              { "Key" : "Application", "Value" : { "Ref" : "AWS::StackName" } },
              { "Key" : "Network", "Value" : "Private" }
            ]
          }
        }
      }

我收到错误:

Template validation error: Template error: Mapping named 'SubnetConfig' is not present in the 'Mappings' section of template.

我在 AWS 文档中找到的所有内容,并与 github 中的共享示例进行了比较。 我应该把什么地方 Mapping name 放在哪里? 或者我应该从错误输出中理解什么? 也许我错过了一些额外的资源?

谢谢!

您正在调用函数 "Fn::FindInMap" : [ "SubnetConfig", "Private", "CIDR" ],但在模板的 "Mappings" 部分中,没有 "SubnetConfig"。

看看 documentation。有一个示例在模板的 "Resources" 部分旁边显示了 "Mappings" 部分。在 "Mappings" 中,您应该声明如下内容:

"Mappings" : {
    ...,
    "SubnetConfig" : {
      "Public" : { "CIDR" : "123.456.789.0/24" },
      "Private" : { "CIDR" : "123.456.789.0/24" },
    }
  },