AWS Amazon EC2 NAT Gateway

This page shows how to write Terraform and CloudFormation for Amazon EC2 NAT Gateway and write them securely.

aws_nat_gateway (Terraform)

The NAT Gateway in Amazon EC2 can be configured in Terraform with the resource name aws_nat_gateway. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

nat-gateways.tf#L5
resource "aws_nat_gateway" "gw1" {
  # The Allocation ID of the Elastic IP address for the gateway.
  allocation_id = aws_eip.nat1.id

  # The Subnet ID of the subnet in which to place the gateway.
  subnet_id = aws_subnet.public_1.id
nat.tf#L19
resource "aws_nat_gateway" "nat-dev-nat-eu-central-1c" {//0532118502fcf6dd7
    allocation_id     = aws_eip.eipalloc-dev-nat-eu-central-1c.id //0ce2e574181a9bb6b
    subnet_id         = aws_subnet.subnet-dev-nat-eu-central-1c.id //"subnet-08149acc2018354ba"
    depends_on        = [aws_internet_gateway.dev-igw]
    tags = {
        Name = "dev-nat-eu-central-1c-gw"
nat.tf#L11
resource "aws_nat_gateway" "nat-dev-nat-eu-central-1c" {//0532118502fcf6dd7
    allocation_id = aws_eip.eipalloc-dev-nat-eu-central-1c.id //0ce2e574181a9bb6b
    subnet_id = aws_subnet.subnet-dev-nat-eu-central-1c.id //"subnet-08149acc2018354ba"
}

//resource "aws_nat_gateway" "nat-0cdd6df2f58edc1fb" {
ngw.tf#L7
resource "aws_nat_gateway" "nat_1a" {
  subnet_id     = aws_subnet.public_1a.id # NAT Gatewayを配置するSubnetを指定
  allocation_id = aws_eip.nat_1a.id       # 紐付けるElasti IP

  tags = {
    Name = "handson-1a"
natgateway.tf#L15
resource "aws_nat_gateway" "ngw_a" {
  allocation_id = aws_eip.nat_a.id
  subnet_id     = aws_subnet.public_a.id

  tags = merge(local.common_tags, {
  Name = "nat-gw-a" })

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Provides a resource to create a VPC NAT Gateway.

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

aws_ebs_volume

Ensure to use a customer-managed key for EBS volume encryption

It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).

risk-label

aws_instance

Ensure to avoid storing AWS access keys in user data

It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::EC2::NatGateway (CloudFormation)

The NatGateway in EC2 can be configured in CloudFormation with the resource name AWS::EC2::NatGateway. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

nat_gw.yml#L8
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt EIP1.AllocationId
      SubnetId: !Select
        - 0
        - !Split [",", !ImportValue kv-public-subnets]
nat.yml#L37
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId:
        Fn::ImportValue: !Sub "${Environment}-nat-eip::EipAAllocationId"
      SubnetId:
        Fn::Select:
nat.yml#L37
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId:
        Fn::ImportValue: !Sub "${Environment}-nat-eip::EipAAllocationId"
      SubnetId:
        Fn::Select:
natgw.yml#L23
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt EIP1.AllocationId
      SubnetId: !Select [0, !Ref PublicSubnetIds]
      Tags:
        - Key: Name
aws-cfn-nat.yml#L216
    Type: 'AWS::EC2::NatGateway'
    Properties:
      AllocationId: !GetAtt NAT1EIP.AllocationId
      SubnetId:
        Fn::ImportValue:
          !Sub "${VPCStackName}-PublicSubnet1ID"
network-nat-gateways.json#L32
            "Type": "AWS::EC2::NatGateway",
            "Properties": {
                "AllocationId": {
                    "Ref": "NatGatewayEipAllocationIdSubnetA"
                },
                "SubnetId": {
natgateways.template.json#L30
      "Type": "AWS::EC2::NatGateway",
      "Properties": {
        "AllocationId": {
          "Fn::GetAtt": [
            "ControlledANatEIP",
            "AllocationId"
nat.json#L16
        "Type": "AWS::EC2::NatGateway",
        "Properties": {
            "AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
            "SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
            "Tags": [
          {
nat.json#L16
        "Type": "AWS::EC2::NatGateway",
        "Properties": {
            "AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
            "SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
            "Tags": [
          {
nat.json#L16
        "Type": "AWS::EC2::NatGateway",
        "Properties": {
            "AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
            "SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
            "Tags": [
          {

Parameters

Explanation in CloudFormation Registry

Specifies a network address translation (NAT) gateway in the specified subnet. You can create either a public NAT gateway or a private NAT gateway. The default is a public NAT gateway. If you create a public NAT gateway, you must specify an elastic IP address.

With a NAT gateway, instances in a private subnet can connect to the internet, other AWS services, or an on-premises network using the IP address of the NAT gateway.

If you add a default route (AWS::EC2::Route resource) that points to a NAT gateway, specify the NAT gateway ID for the route's NatGatewayId property.

For more information, see NAT Gateways in the Amazon VPC User Guide.

Frequently asked questions

What is AWS Amazon EC2 NAT Gateway?

AWS Amazon EC2 NAT Gateway is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon EC2 NAT Gateway?

For Terraform, the narendra311777/myeksterraform, mdigbazova/terraforming-add-a-new-customer and mdigbazova/terraforming-4ha-live-all-resources source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the thbishop/kv, NHS-digital-website/jubilant-doodle and ansible-city/aws_foundation source code examples are useful. See the CloudFormation Example section for further details.