繁体   English   中英

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

[英]AWS CloudFormation: how do I refer to the default/main route table (that is created when a VPC is created) in a cloudformation template?

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

我已通读路线的cloudformation文档,路由表以了解如何执行此操作,但无济于事。

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

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

谢谢,

  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

到目前为止,表现不错-您拥有Internet网关,路由表和公共子网。 现在,您需要创建路由并将路由表附加到子网(如果尚未这样做)。 如果您使用的是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM