// Module included in the following assemblies:
//
// * installing/installing-aws-wavelength-zone.adoc (Installing a cluster on AWS with worker nodes on AWS Wavelength Zones)
// * installing/installing_aws/aws-compute-edge-zone-tasks.adoc

ifeval::["{context}" == "installing-aws-wavelength-zone"]
:wavelength-zone:
endif::[]
ifeval::["{context}" == "aws-compute-edge-zone-tasks"]
:post-aws-zones:
endif::[]

:_mod-docs-content-type: REFERENCE
[id="installation-cloudformation-vpc-carrier-gw_{context}"]

ifdef::wavelength-zone[]
= CloudFormation template for the VPC Carrier Gateway

endif::wavelength-zone[]
ifdef::post-aws-zones[]
= Wavelength Zones only: CloudFormation template for the VPC Carrier Gateway

endif::post-aws-zones[]

[role="_abstract"]
Use the CloudFormation template to deploy the Carrier Gateway on {aws-first} Wavelength infrastructure. The template automates the creation of an {aws-short} Carrier Gateway for {product-title}. The template provisions an `AWS::EC2::CarrierGateway` and associates it with the cluster VPC to enable traffic routing to provide direct internet connectivity for resources in Wavelength Zones that use carrier networks.

.CloudFormation template for VPC Carrier Gateway

[source,yaml,subs="attributes+"]
----
AWSTemplateFormatVersion: 2010-09-09
Description: Template for Creating Wavelength Zone Gateway (Carrier Gateway).

Parameters:
  VpcId:
    Description: VPC ID to associate the Carrier Gateway.
    Type: String
    AllowedPattern: ^(?:(?:vpc)(?:-[a-zA-Z0-9]+)?\b|(?:[0-9]{1,3}\.){3}[0-9]{1,3})$
    ConstraintDescription: VPC ID must be with valid name, starting with vpc-.*.
  ClusterName:
    Description: Cluster Name or Prefix name to prepend the tag Name for each subnet.
    Type: String
    AllowedPattern: ".+"
    ConstraintDescription: ClusterName parameter must be specified.

Resources:
  CarrierGateway:
    Type: "AWS::EC2::CarrierGateway"
    Properties:
      VpcId: !Ref VpcId
      Tags:
      - Key: Name
        Value: !Join ['-', [!Ref ClusterName, "cagw"]]

  PublicRouteTable:
    Type: "AWS::EC2::RouteTable"
    Properties:
      VpcId: !Ref VpcId
      Tags:
      - Key: Name
        Value: !Join ['-', [!Ref ClusterName, "public-carrier"]]

  PublicRoute:
    Type: "AWS::EC2::Route"
    DependsOn: CarrierGateway
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      CarrierGatewayId: !Ref CarrierGateway

  S3Endpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      PolicyDocument:
        Version: 2012-10-17
        Statement:
        - Effect: Allow
          Principal: '*'
          Action:
          - '*'
          Resource:
          - '*'
      RouteTableIds:
      - !Ref PublicRouteTable
      ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .s3
      VpcId: !Ref VpcId

Outputs:
  PublicRouteTableId:
    Description: Public Route table ID
    Value: !Ref PublicRouteTable
----

ifeval::["{context}" == "installing-aws-wavelength-zone"]
:!wavelength-zone:
endif::[]
ifeval::["{context}" == "aws-compute-edge-zone-tasks"]
:!post-aws-zones:
endif::[]
