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
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
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"
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" {
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"
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" })
Parameters
-
allocation_id
required - string -
id
optional computed - string -
network_interface_id
optional computed - string -
private_ip
optional computed - string -
public_ip
optional computed - string -
subnet_id
required - string -
tags
optional - map from string to string
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.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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).
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.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EIP1.AllocationId
SubnetId: !Select
- 0
- !Split [",", !ImportValue kv-public-subnets]
Type: AWS::EC2::NatGateway
Properties:
AllocationId:
Fn::ImportValue: !Sub "${Environment}-nat-eip::EipAAllocationId"
SubnetId:
Fn::Select:
Type: AWS::EC2::NatGateway
Properties:
AllocationId:
Fn::ImportValue: !Sub "${Environment}-nat-eip::EipAAllocationId"
SubnetId:
Fn::Select:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EIP1.AllocationId
SubnetId: !Select [0, !Ref PublicSubnetIds]
Tags:
- Key: Name
Type: 'AWS::EC2::NatGateway'
Properties:
AllocationId: !GetAtt NAT1EIP.AllocationId
SubnetId:
Fn::ImportValue:
!Sub "${VPCStackName}-PublicSubnet1ID"
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {
"Ref": "NatGatewayEipAllocationIdSubnetA"
},
"SubnetId": {
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {
"Fn::GetAtt": [
"ControlledANatEIP",
"AllocationId"
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
"SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
"Tags": [
{
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
"SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
"Tags": [
{
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {"Fn::GetAtt": ["EIPA", "AllocationId"]},
"SubnetId": {"Fn::ImportValue": "SubnetPubAId"},
"Tags": [
{
Parameters
-
AllocationId
optional - String -
ConnectivityType
optional - String -
SubnetId
required - String -
Tags
optional - List of Tag
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'sNatGatewayId
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, ansible-city/aws_foundation and NHS-digital-website/jubilant-doodle source code examples are useful. See the CloudFormation Example section for further details.