amazon-web-servicesamazon-ec2amazon-vpcaws-route-table

How to transfer the packets through NAT gateway instead of public IP?


Using AWS services - I have used VPC and divided it into two subnets(private and public), then created two route tables(private and public), created one Internet gateway and attached to the public route table and one NAT Gateway connected to the private route table also both the route tables are mapped to their respective subnets but the problem is I need to deploy some code in the ec2 instance whose IP should not go without touching NAT Gateway.

I had many articles and also followed youtube but still I am very much confused like how to achieve this and whatever I did is right or wrong.

Also NAT gateway should be attached in private or public?


Solution

  • Also NAT gateway should be attached in private or public?

    NAT should be deployed in public subnet. Then for your private subnet, you setup a route to the NAT.

    Once this is done, you place your EC2 instance in the private subnet. This way, any internet traffic from the instance will be routed through the NAT.