RouteTable在使用CloudFormation创建时出现了两次 – ECS。

huangapple go评论59阅读模式
英文:

RouteTable is created twice with cloudformation - ECS

问题

我正在尝试使用CloudFormation创建VPC,其中包括一个公共子网、路由表和Internet网关。问题是,即使我只包括了一个路由表,仍会创建两个与VPC关联的路由表。

以下是我的代码:

AWSTemplateFormatVersion: "2010-09-09"
Description: "创建一个带有公共子网、Internet网关和公共路由表的VPC"

Parameters:
  VpcCIDR:
    Type: String
    Description: "VPC的CIDR块(例如,10.0.0.0/16)"

  PublicSubnetCIDR:
    Type: String
    Description: "公共子网的CIDR块(例如,10.0.1.0/24)"

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      Tags:
        - Key: Name
          Value: MyVPC

  MyInternetGateway:
    Type: AWS::EC2::InternetGateway

  MyVPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MyVPC
      InternetGatewayId: !Ref MyInternetGateway

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC
      Tags:
        - Key: Name
          Value: PublicRouteTable

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: MyVPCGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: "0.0.0.0/0"
      GatewayId: !Ref MyInternetGateway

  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Ref PublicSubnetCIDR
      AvailabilityZone:
        Fn::Select:
        - 0
        - Fn::GetAZs: {Ref: 'AWS::Region'}
      Tags:
        - Key: Name
          Value: PublicSubnet

  PublicSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet
      RouteTableId: !Ref PublicRouteTable

Outputs:
  VpcId:
    Value: !Ref MyVPC
    Description: "VPC ID"
  PublicRouteTableId:
    Value: !Ref PublicRouteTable
    Description: "公共路由表ID"
  PublicSubnetId:
    Value: !Ref PublicSubnet
    Description: "公共子网ID"

RouteTable在使用CloudFormation创建时出现了两次 – ECS。

英文:

I am trying to create vpc using cloudformation, with a single public subnet, route table and internet gateway. Problem is two route tables are created in association with the vpc even if I have included just 1 route table.

RouteTable在使用CloudFormation创建时出现了两次 – ECS。

Below is my code

Description: "Create a VPC with a public subnet, Internet Gateway, and a public route table"
Parameters:
VpcCIDR:
Type: String
Description: "CIDR block for the VPC (e.g., 10.0.0.0/16)"
PublicSubnetCIDR:
Type: String
Description: "CIDR block for the public subnet (e.g., 10.0.1.0/24)"
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
Tags:
- Key: Name
Value: MyVPC
MyInternetGateway:
Type: AWS::EC2::InternetGateway
MyVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref MyVPC
InternetGatewayId: !Ref MyInternetGateway
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: PublicRouteTable
PublicRoute:
Type: AWS::EC2::Route
DependsOn: MyVPCGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref MyInternetGateway
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: !Ref PublicSubnetCIDR
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
Tags:
- Key: Name
Value: PublicSubnet
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet
RouteTableId: !Ref PublicRouteTable
Outputs:
VpcId:
Value: !Ref MyVPC
Description: "VPC ID"
PublicRouteTableId:
Value: !Ref PublicRouteTable
Description: "Public Route Table ID"
PublicSubnetId:
Value: !Ref PublicSubnet
Description: "Public Subnet ID"

答案1

得分: 2

一个VPC始终会有一个默认路由表

当您创建一个VPC时,它会自动拥有一个主路由表。当子网没有与之关联的显式路由表时,默认情况下会使用主路由表。

英文:

A VPC will always have a default route table:

> When you create a VPC, it automatically has a main route table. When a subnet does not have an explicit routing table associated with it, the main routing table is used by default.

huangapple
  • 本文由 发表于 2023年7月24日 20:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定