How to do it...

In this recipe, you will create a new CloudFormation that creates a stack with a NAT gateway. Let's get started:

  1. Start with the usual CloudFormation template version and description:
AWSTemplateFormatVersion: "2010-09-09" 
Description: Create NAT Gateway and associated route.
  1. The template must take the following required parameters:
Parameters: 
PublicSubnetId:
Description: Public Subnet ID to add the NAT Gateway to
Type: AWS::EC2::Subnet::Id
RouteTableId:
Description: The private subnet route table to add the NAT
Gateway route to
Type: String
  1. In the Resources section, define an Elastic IP (EIP) that will be assigned to the NAT gateway:
Resources: 
EIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
  1. Create the NAT gateway resource, assigning it the EIP you just defined in the public subnet:
  NatGateway: 
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EIP.AllocationId
SubnetId: !Ref PublicSubnetId

  1. Finally, define the route to the NAT gateway and associate it with the private subnet's route table:
  Route: 
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref RouteTableId
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway
  1. Save the template with a known filename, for example, 07-02-NATGateway.yaml.
  2. Launch the template with the following CLI command:
      aws cloudformation create-stack 
--stack-name nat-gateway
--template-body file://07-02-NATGateway.yaml
--parameters
ParameterKey=RouteTableId,ParameterValue=<route-table-id>
ParameterKey=PublicSubnetId,ParameterValue=<public-subnet-id>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset