This guide walks you through setting up an AWS NAT Gateway so that EC2 instances in a private subnet can reach the internet (for updates, patches, or outbound API calls) without being directly reachable from the outside. You’ll create a NAT Gateway in a public subnet, update your private subnet’s route table, and then verify that a private instance can connect outbound. Before starting, you’ll need an AWS account with permissions to create VPC resources, and an existing VPC with at least one public and one private subnet.
What You’ll Need
- An AWS account with IAM permissions to create VPCs, subnets, route tables, NAT Gateways, and EC2 instances.
- A VPC with at least two subnets: one public (with an Internet Gateway attached) and one private (no direct internet route).
- An Internet Gateway attached to the VPC (if not already present).
- A key pair for SSH access to test instances (optional but recommended).
Step-by-Step
- Open the VPC console – Sign in to the AWS Management Console, navigate to the VPC dashboard. Confirm your VPC and note the subnet IDs for your public and private subnets.
- Check your Internet Gateway – In the left menu, click “Internet Gateways”. If one is attached to your VPC, note its ID. If not, create one, attach it to your VPC, and ensure your public subnet’s route table has a route
0.0.0.0/0to that Internet Gateway. - Allocate an Elastic IP – In the left menu, go to “Elastic IPs”. Click “Allocate Elastic IP address” and confirm. This IP will be the public address of your NAT Gateway. Note the allocation ID.
- Create the NAT Gateway – In the left menu, click “NAT Gateways”, then “Create NAT Gateway”. Name it (e.g.,
my-nat-gw). Select your public subnet from the dropdown. Under “Elastic IP allocation ID”, choose the Elastic IP you just allocated. Click “Create NAT Gateway”. Wait for the status to becomeAvailable(this can take a minute or two). - Update the private subnet route table – In the left menu, click “Route Tables”. Find the route table associated with your private subnet (the one that does NOT have a route to an Internet Gateway). Select it, click the “Routes” tab, then “Edit routes”. Click “Add route”. For Destination, enter
0.0.0.0/0. For Target, select “NAT Gateway” and choose your NAT Gateway’s ID from the list. Click “Save changes”. - Verify the route table association – Ensure the route table you edited is indeed associated with your private subnet. In the “Subnet associations” tab, confirm your private subnet is listed. If not, click “Edit subnet associations” and add it.
- Test with an EC2 instance – Launch a test EC2 instance in the private subnet (no public IP needed). Use an Amazon Linux or Ubuntu AMI. After it’s running, connect via SSH using a bastion host or EC2 Instance Connect (if your private subnet allows it). From the instance, run
ping -c 4 8.8.8.8(ICMP may be blocked, but try) or more reliablycurl -I https://aws.amazon.com. If you get a response, your NAT Gateway is working. - Check security groups and NACLs – If the test fails, verify that the security group attached to your private instance allows outbound traffic (default allows all outbound) and that your private subnet’s Network ACL allows outbound traffic on ephemeral ports (1024-65535) and inbound for established connections.
Common Mistakes
- Placing the NAT Gateway in a private subnet – The NAT Gateway must be in a public subnet that has a route to an Internet Gateway. If it’s in a private subnet, it won’t have internet access itself, and traffic will fail.
- Forgetting to update the private route table – Creating the NAT Gateway alone does nothing. You must add a route in the private subnet’s route table pointing
0.0.0.0/0to the NAT Gateway. - Using the wrong route table – If your private subnet is implicitly associated with the main route table, editing a different route table won’t affect it. Always check the subnet associations.
- Ignoring Elastic IP costs – NAT Gateways incur hourly charges plus data processing costs. The Elastic IP itself is free while attached, but the NAT Gateway is not. Be aware of costs if you leave it running.
- Not checking security groups – If your private instance’s security group blocks outbound traffic (e.g., only allows specific ports), the NAT Gateway won’t help. The security group must allow the outbound traffic you need.
Tips
- Use a single NAT Gateway for high availability – For production, place NAT Gateways in multiple Availability Zones and route traffic from each private subnet to the NAT Gateway in its own AZ to avoid cross-AZ data transfer charges.
- Name your resources – Add tags like
Nameto your NAT Gateway, route tables, and subnets. It makes troubleshooting much easier when you have many resources. - Prefer NAT Gateway over NAT instance – While you can run your own NAT instance on an EC2, the managed NAT Gateway is more reliable and requires no maintenance. Use it unless you have a specific need for a custom NAT.
- Test with a simple curl – Use
curl -I https://www.google.comto test outbound HTTPS. If that works, your NAT is fine; if not, check routes and security groups. - Monitor with CloudWatch – NAT Gateways publish metrics like
BytesOutToDestinationandPacketsDropCount. Set up a basic alarm for dropped packets to catch issues early.
Troubleshooting
No internet from private instance – First, check the route table: run ip route show on the instance and confirm there is a default route via the NAT Gateway’s private IP (the ENI’s IP). If not, your route table update didn’t take effect. Verify the route table is associated with the subnet.
NAT Gateway status is “Failed” – This usually means the Elastic IP is already associated with another resource, or the subnet is not public. Release the Elastic IP and reallocate a new one, or move the NAT Gateway to a proper public subnet.
ICMP ping fails but TCP works – Many security groups and NACLs block ICMP. Use curl or nc to test TCP connectivity instead. If TCP works, your NAT is fine.
Intermittent connectivity – This can happen if your private subnet is in a different AZ than the NAT Gateway and you’re hitting cross-AZ limits (though it should still work). Check your route table for specific routes that might override the default. Also, verify that the NAT Gateway’s ENI has a healthy status in the VPC console.
Instance has no route to NAT Gateway – If the route table looks correct, reboot the instance or run sudo dhclient to renew DHCP. Sometimes the instance’s route cache is stale.
NAT Gateway is not in “Available” state – Wait a few minutes. If it stays in “Pending” or “Deleting”, check if you accidentally deleted the Elastic IP or the subnet. You may need to delete and recreate.
Frequently Asked Questions
Can I use a NAT Gateway for IPv6?
No, NAT Gateways only support IPv4. For IPv6 outbound-only access, use an egress-only Internet Gateway.
Do I need a NAT Gateway in every Availability Zone?
For high availability, yes. If one AZ fails, instances in other AZs lose internet access. For dev/test, one is fine.
How much does a NAT Gateway cost?
AWS charges hourly for each NAT Gateway plus data processing per GB. Check the official pricing page for current rates.
Can I use a NAT Gateway to allow inbound traffic to a private instance?
No, NAT Gateway only supports outbound-initiated connections. For inbound, use a bastion host or a load balancer.
What’s the difference between a NAT Gateway and a NAT instance?
A NAT Gateway is a managed service, highly available, and requires no maintenance. A NAT instance is an EC2 instance you configure yourself; it’s cheaper but you must manage it.
How do I test if my NAT Gateway is working?
SSH to a private instance and run curl -I https://aws.amazon.com. If you get an HTTP response, it’s working.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Automatically Watermark Images in WordPress
- MCP Server Production Readiness: How to Assess
- Schedule Ride Advance Lyft: How It Works
- Rush Royale Codes: Active & Expired List
- Compress Images on Mac: Built-In & Free Ways
- Qwerty Nokia Phone Spotted? The Facts