AWS CloudFormation:如何在 cloudformation 模板中引用默认/主路由表(在创建 VPC 时创建)?

N.C*_*het 7 amazon-web-services aws-cloudformation

我有一个创建自定义 VPC 的 CloudFormation 模板。该模板创建以下资源 - VPC、Internet 网关、将 IGW 附加到 VPC 并创建公共子网。我想向作为 VPC 一部分创建的路由表添加一条路由(目的地 0.0.0.0/0,目标 IGW)。

我已经通读了关于路由、路由表的 cloudformation 文档来弄清楚如何做到这一点,但无济于事。

我可以使用 Fn::Ref 函数来引用作为模板一部分显式创建的资源或参数,但如何引用使用 VPC 固有地创建的资源?

非常感谢有关如何重用现有路由表、NACL 和安全组的任何见解。

谢谢,

Ben*_*ley 11

到目前为止,干得不错 - 您拥有互联网网关、路由表和公共子网。现在,您需要创建路由并将路由表附加到子网(如果您尚未这样做)。如果您使用 YAML,它可能看起来像这样:

 InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Subnet (AZ1)

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Routes

  DefaultPublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1
Run Code Online (Sandbox Code Playgroud)


小智 5

  1. 不要使用默认路由表(请参阅https://serverfault.com/questions/588904/aws-vpc-default-route-table-in-cloudformation
  2. 您可以根据https://serverfault.com/questions/544439/aws-cloudformation-vpc-default-security-group获取默认安全组
  3. 最后,您还可以获得与上面的 DefaultSecurityGroup 相同的 DefaultNetworkAcl。另请参阅https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html