AWS Cloudformation - 无法将多个子网关联添加到公共路由表

Mar*_*cus 7 amazon-web-services amazon-cloudformation

我们有一个带有 Internet 网关的 VPC。我们有 3 个子网(每个 AZ 一个),并且希望对所有三个子网使用一个路由表。此 RT 包含将 0.0.0.0/0 路由到 igw 的规则,但是当我们尝试将多个子网与此 RT 关联时,堆栈创建在创建路由规则时失败,并给出以下错误消息:

route table rtb-xxxxxxx and network gateway igw-xxxxx belong to different networks.
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为 igw 没有连接到子网,它连接到 VPC 本身。

为了让模板正常工作,我必须做的是只与 RT 有 1 个子网关联,然后再用其他两个更新堆栈。

我已经尝试添加 2 个等待条件,一个与 RT 的创建相关,另一个与路由规则的创建相关,但是它们没有解决问题 - 我仍然在同一个该死的规则上遇到相同的错误:(

任何人都可以阐明我需要做些什么来解决这个问题?

小智 5

您确定您已将 InternetGatway 附加到 VPC(或与路由表相同的 VPC)。在云的形成中,这看起来像……

    "AttachInternetGateway" : {
       "Type" : "AWS::EC2::VPCGatewayAttachment",
       "Properties" : {
          "VpcId" : { "Ref" : "YourVpc" },
          "InternetGatewayId" : { "Ref" : "InternetGateway" }
       }
    },
Run Code Online (Sandbox Code Playgroud)


lan*_*rix 5

正如@Marcus 在回答他自己的问题时所解释的那样;创建AWS::EC2::Route条目并在其中指定Gateway时,缺少DependsOn属性。

对于指定网关的路由条目,您必须指定对网关附件资源的依赖关系。

当 IGW 连接到 VPC 时,收到相同的错误并让我摸不着头脑,这是AWS::EC2::Route声明中的一个简单更改。

CFN 失败:

"VPC" : {
    "Type" : "AWS::EC2::VPC",
    "Properties" : {"CidrBlock" : "10.1.0.0/16"}
},
"InternetGateway" : {
    "Type" : "AWS::EC2::InternetGateway"
},
"InternetGatewayAttachment" : {
    "Type" : "AWS::EC2::VPCGatewayAttachment",
    "Properties" : {
        "VpcId" : {"Ref" : "VPC"},
        "InternetGatewayId" : {"Ref" : "InternetGateway"}
    }
},
"ManagementRouteTable" : {
    "Type" : "AWS::EC2::RouteTable",
    "Properties" : {
        "VpcId" : {"Ref" : "VPC"}
    }
},
"NATDefaultRoute" : {
    "Type" : "AWS::EC2::Route",
    "Properties" : {
        "RouteTableId" : {"Ref" : "ManagementRouteTable"},
        "DestinationCidrBlock" : "0.0.0.0/0",
        "GatewayId" : {"Ref" : "InternetGateway"}
    }
}
Run Code Online (Sandbox Code Playgroud)

工作CFN:

"VPC" : {
    "Type" : "AWS::EC2::VPC",
    "Properties" : {"CidrBlock" : "10.1.0.0/16"}
},
"InternetGateway" : {
    "Type" : "AWS::EC2::InternetGateway"
},
"InternetGatewayAttachment" : {
    "Type" : "AWS::EC2::VPCGatewayAttachment",
    "Properties" : {
        "VpcId" : {"Ref" : "VPC"},
        "InternetGatewayId" : {"Ref" : "InternetGateway"}
    }
},
"ManagementRouteTable" : {
    "Type" : "AWS::EC2::RouteTable",
    "Properties" : {
        "VpcId" : {"Ref" : "VPC"}
    }
},
"NATDefaultRoute" : {
    "DependsOn" : "InternetGatewayAttachment",
    "Type" : "AWS::EC2::Route",
    "Properties" : {
        "RouteTableId" : {"Ref" : "ManagementRouteTable"},
        "DestinationCidrBlock" : "0.0.0.0/0",
        "GatewayId" : {"Ref" : "InternetGateway"}
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 0

这就是我们将多个子网与同一个路由表关联起来的方式:

...
"PublicSubnetA": {
    "Type": "AWS::EC2::Subnet",
    "Properties": { "AvailabilityZone": "..",
                    "CidrBlock": "...",
                    "VpcId": { "Ref": "VPC" },
                    "Tags": [ .. ] }},
"PublicSubnetB": {
    "Type": "AWS::EC2::Subnet",
    "Properties": { "AvailabilityZone": "..",
                    "CidrBlock": "...",
                    "VpcId": { "Ref": "VPC" },
                    "Tags": [ .. ] }},
"RouteTableIGW": {
    "Type": "AWS::EC2::RouteTable",
    "Properties": { "VpcId": { "Ref": "VPC" },
                    "Tags": [ .. ]}},
"...": {
    "Type": "AWS::EC2::SubnetRouteTableAssociation",
    "Properties": { "RouteTableId": { "Ref": "RouteTableIGW" },
                    "SubnetId":     { "Ref": "PublicSubnetA" }}
},
"...": {
    "Type": "AWS::EC2::SubnetRouteTableAssociation",
    "Properties": { "RouteTableId": { "Ref": "RouteTableIGW" },
                     "SubnetId":    { "Ref": "PublicSubnetB" }}
},
... 
Run Code Online (Sandbox Code Playgroud)

而且效果很好。我以这种方式将一个 IGW 定向路由表与 2 个公共子网关联起来,将另一个 NAT 定向路由表与 4 个私有子网关联起来。