AWS NAT - GW And Elastic IP

AWS NAT - GW And Elastic IP

Nat Gateway :: A NAT gateway is a Network Address Translation (NAT) service. You can use a NAT gateway so that instances in a private subnet can connect to services outside your VPC but external services cannot initiate a connection with those instances.

*Below are Contents from AWS DOCS*

When you create a NAT gateway, you specify one of the following connectivity types:

  • Public – (Default) Instances in private subnets can connect to the internet through a public NAT gateway, but cannot receive unsolicited inbound connections from the internet. You create a public NAT gateway in a public subnet and must associate an elastic IP address with the NAT gateway at creation. You route traffic from the NAT gateway to the internet gateway for the VPC. Alternatively, you can use a public NAT gateway to connect to other VPCs or your on-premises network. In this case, you route traffic from the NAT gateway through a transit gateway or a virtual private gateway.

  • Private – Instances in private subnets can connect to other VPCs or your on-premises network through a private NAT gateway. You can route traffic from the NAT gateway through a transit gateway or a virtual private gateway. You cannot associate an elastic IP address with a private NAT gateway. You can attach an internet gateway to a VPC with a private NAT gateway, but if you route traffic from the private NAT gateway to the internet gateway, the internet gateway drops the traffic.

Elastic IP ::

An Elastic IP is a static, publicly routable IPv4 address that is allocated to your AWS account. It allows resources in AWS to have a permanent public IP address that doesn’t change, even if the underlying instance or service is restarted.

Internet Gateway ::
So when we create VPC to enable its communication with Internet we use IGW . This also performs Network Address Translation

Implementation of above Architecture.

Step 1) Go to VPC → Create VPC → VPC and more → Give some name → CIDR (10.0.0.0/16) —> AZ =2 —> Public Subnet =2 —> Private Subnet =2 —> Nat Gateway= In 1 AZ —> VPC Endpoints =None → Craete VPC

Step 2 ) Goto SG → All ALL INBOUD CONNECTION

Step 3 ) Create 2 EC2 Instance one in Public Subnet and Another in Private Subnet

Step 4) SSH in Pubclic EC2 Instance and the try pinging Google, it should work

Step 5) Copy the pem file in Public EC2 instance, and then ssh in Private instance, and tey pinging Google, it should work

Whats Happening behind the scene !!!

So Private traffic is connected to —> NAT GATEWAY, —> Elastic IP and then it connects to Public Subnet where traffic goes to Internet

Role of Elastic IP in the Communication Process

  1. Association with the NAT Gateway:

    • The NAT Gateway is used in a public subnet and serves as an intermediary for outbound internet traffic from instances in a private subnet.

    • The NAT Gateway requires a public IP address to communicate with the internet. This is where the Elastic IP comes into play—it provides that public address.

  2. Traffic Translation:

    • When instances in the private subnet need to access the internet (e.g., for updates, API calls, or external service access), the following happens:

      • Outbound traffic from the private instance is routed to the NAT Gateway.

      • The NAT Gateway translates the source IP of the private instance into the Elastic IP associated with the NAT Gateway.

      • The translated request is sent to the internet using the Elastic IP.

    • The response from the internet returns to the Elastic IP, and the NAT Gateway translates it back to the private IP of the originating instance.