簡體   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