Amazon NAT gateway not working - Instances not accessible - amazon-web-services

I run an Elastic Beanstalk setup, with a dynamic amount of servers, with dynamic IP's, an external provider needs to whitelist our IP, so I need to route all my traffic out via a NAT gateway.
I am using the default public VPC, with 3 default subnets.
I have created a new Subnet, and a new NAT gateway, which I have placed on this new subnet.
I have created a new route table for this new subnet, where 0.0.0.0/0 points to an internet gateway.
I have changed the routes from the other 3 default subnets from 0.0.0.0/0 (Internet Gateway) to 0.0.0.0/0 (NAT Gateway).
My instances become inaccessible after i do this, what seems to be the issue, and where can i find some logs?

Related

what is the usage of CIDR notation route rule in aws

I have created a VPC and internet gate way. And attached internet gateway to the vpc. Create two subnets one is public subnet and another is private subnet. And created a route table to route the traffic. In route table added a route rule 0.0.0.0/0 to IGW(internet gatway). To test this I launch a EC2 instance in public subnet. I have generated a public IP address example : (554.6.8.24). Using that IP address do ssh and I am connecting from local machine to EC2 instance.
My question is I am connecting to the EC2 instance from my local machine using public IP. What is the point of adding route rule in 0.0.0.0/0 to IGW(internet gatway) in route table?. With out route rule can I connect to Ec2?
If an Internet Gateway is not present in an Amazon VPC, then that VPC has no connection to the Internet. This can be very useful for creating private networks.
If you want to connect a VPC to the Internet, then you need to:
Create an Internet Gateway for that VPC
Create a Route Table that sends traffic to the Internet Gateway
Attach the Route Table to a Subnet (thereby making that subnet a Public Subnet since it routes to the Internet Gateway)
Put resources inside the Public Subnet (eg EC2 instances)
In contrast, Private subnets do not have a Route Table entry that points to an Internet Gateway. Therefore, those subnets cannot access the Internet. This is an added layer of security to prevent access to private resources.
The only difference between a Public Subnet and a Private Subnet is having the Route Table entry that points to an Internet Gateway. Without that entry, a subnet will remain private.

Is there a way to assign one static elastic IP to multiple instances?

Is there anyway that many ec2 instances can share the same IP? So that all outbound API requests from the ec2 instances, are coming from the same IP address? If the API provider were to whitelist the single static IP address, it would grant access to all of the instances.
Any ideas would be great!
If this is for outbound only, then it can be achieved using a NAT Gateway.
All the EC2 instances will sit behind the public NAT Gateway, the NAT Gateway is then assigned an Elastic IP Address.
All traffic from the EC2 instances will be routed via the NAT Gateway to the Internet Gateway and the target environment will see all the traffic originating from the Elastic Ip Address assigned to the NAT Gateway.
However, if traffic must go the other way (inbound), then this architecture will not work.
UPDATE: The NAT Gateway must reside in a public subnet, that subnet will have a route 0.0.0.0/0 to the Internet Gateway. The EC2 instances will reside in a private subnet and have a route 0.0.0.0/0 to the NAT Gateway in the public subnet.
Furthermore for High Availability it is recommended that you have at least 4 subnets, 2 in 2 different Availability Zones (AZ). A NAT Gateway does not span subnets and so at least two NAT Gateways should be used one in public subnet A which resides in the first AZ and the other in public subnet B which resides in the second AZ.
The architecture would be as follows:
Where Routing Table A (Associated with both public subnets) has a route to the Internet Gateway i.e.
10.0.0.0/16 - local
0.0.0.0/0 - igw-xxxxxxxxx
Routing Table B is associated with private subnet 10.0.10.0/24 has a route to the NAT Gateway in the public subnet (10.0.0.0/24) i.e.
10.0.0.0/16 - local
0.0.0.0/0 - nat-123456
Routing Table C is associated with private subnet 10.0.11.0/24 has a route to the NAT Gateway in the public subnet (10.0.1.0/24) i.e.
10.0.0.0/16 local
0.0.0.0/0 nat-654321
In this architecture each NAT Gateway will have it's own Elastic IP address which means 2 IP Addresses will be visible to the target, if high availability is not important then you could use 1 NAT Gateway in one public subnet. Note that, a NAT gateway supports 5 Gbps of bandwidth and automatically scales up to 45 Gbps. If you require more bandwidth, you can split your resources into multiple subnets and create a NAT gateway in each subnet.
For more detailed information on configuring a NAT within a VPC see the following page in the docs.

Mailing isn't working in Django AWS Lambda (Zappa) behind a VPC

I have an AWS lambda function (Django zappa) that is running inside a VPC thus I'm not able to send emails because Lambda isn't connected to the internet, I tried NAT Gatway but with no hope.
I created a NAT Gatway using one public subnet of the VPC.
Created another private subnet with a route table that routes 0.0.0.0/0 to the NAT Gateway.
I would appreciate any help.
Go to VPC Dashboard and use the Launch VPC Dashboard
Create (Allocate) an elastic IP address and keep it handy. Select the option VPC with Public and Private Subnets
Configure the subnet CIDRs and associate the elastic IP created in the previous step in the form. This will create a NAT Gateway and set it up automatically with correct route tables.
Things to note:
NAT Gateway should always be in the public subnet.
Route table of Private Subnet should have a route 0.0.0.0/0 to NAT Gateway
Route table of Public Subnet should have a route 0.0.0.0/0 to Internet Gateway
Lambda function should be configured with the Private Subnet and not the Public Subnet
Security Group configured in the lambda function should allow outbound to 0.0.0.0/0 (which is there by default, but you should verify)

Unable to get AWS NAT gateway working for API with IP whitelist

Our aim is to get our Elastic Beanstalk setups to route traffic through a NAT gateway as we require for certain traffic connecting to API's which require IP whitelisting. Rather than make modifications to the current setup, I have created a separate/isolated VPC & EC2 instance to familiarise and test the setup. However I am yet to get the setup working as desired.
Here is the setup
VPC (vpc-77049811) with CIDR of 10.0.0.0/16
Internet gateway (igw-4d4b212a) assigned to mentioned VPC
Subnet (subnet-096d8a53) with CIDR of 10.0.1.0/24
NAT Gateway (nat-00bb49204627de7e6) attached to mentioned subnet and assigned Elastic IP
Route table attached to mentioned VPC and associate with mentioned subnet
1x EC2 Instance assigned to VPC and its own Elastic IP and Disabled Source/Destination Check
Route Table Setup
10.0.0.0/16 local
0.0.0.0/0 igw-4d4b212a
With the above setup, and am able to log into the server and make a curl request to get the servers public IP address (curl icanhazip.com). As soon as I add a rule to the route table for the url's resolved IP's to route through the NAT gateway though, I am unable to ping or request the curl request as it will timeout.
Rules added to route table which do not work
45.63.64.111/32 nat-00bb49204627de7e6
144.202.71.30/32 nat-00bb49204627de7e6
Not sure if I've overlooked something here or maybe I have misunderstood the concept and use cases for the NAT gateway?
This is public IP 45.63.64.111. You need IGW to reach to this traffic.
You either do that by directly redirecting your traffic to IGW
OR
You do that by directing to NAT then further directing that traffic to IGW
Directing to IGW part is missing.
Nat gateway is used for EC2 in private subnets (which does not have IGW attached to it). In scenario above, EC2 is in public subnet so ideally it does not need NAT.
Here is what I would do to use NAT-
1. Place EC2 in private subent. and have a Route table where all outgoing traffic to nat-gateway.
2. Nat-gateway which is in public subnet will forward your traffic to IGW.
It seems like you have misunderstood the purpose of a NAT.
Its purpose is to provide outbound internet access to instances in a private subnet without allowing any inbound connectivity - i.e. a subnet where the routing table does NOT have an entry for:
0.0.0.0/0 igw-4d4b212a
If you want to restrict access from your EC2 instance to specific IP addresses, put your NAT in the public subnet, create a private subnet, and put your instance in the private subnet. Then add the two routes to the route table associated with the private subnet:
45.63.64.111/32 nat-00bb49204627de7e6
144.202.71.30/32 nat-00bb49204627de7e6
If you simply want to restrict access of your EC2 instance to a couple of IP addresses, you can only create routes for those addresses:
45.63.64.111/32 igw-4d4b212a
144.202.71.30/32 igw-4d4b212a
Be aware that with this last option, your instance can be reached from the internet if you have rules open in your security groups.

aws loadbalancer,routeable confusion

I'm putting the instances behind the aws loadbalancer, I have configured the routable and attached the IGW to it, created the loadbalancer and added this instance to the aws loadbalancer every things work well, the endpoint URL of the AWS loadbalancer able to load the HTTP pages
Now i have removed the IGW from the route table and tested it again, the AWS loadbalalncer endpoint URL not able to load the page, but the instace status shows in AWS loadbalancer as inService
Why the IGW is required when loadbalancer is configured over private subnet, it technically Mean it's a public subnet, which is blocking me to create a NAT inatance
A subnet without a default route pointing to the igw-xxxxxxxx Internet Gateway object is, by definition, a private subnet. If you remove the igw from a public subnet, you now have a private subnet.
Placing an Internet-facing load balancer (ELB) in such a private subnet is incorrect.
It sounds as though you are making a commonly-made -- but incorrect -- assumption that the ELB should be configured in the same subnets as the instances behind it. This is also incorrect.
Provision the ELB in public subnets, without regard to the subnets the instances behind it were placed in.
In summary:
Internet-facing ELB requires a public subnet for placement.
NAT instance requires a public subnet for placement.
The instances that use these services (NAT and ELB) belong in different -- private -- subnets, different subnets than the ELB and NAT instances.
ELB and NAT can be placed together in the same subnets, or separately, as long as the subnets are public (have the IGW as their default route) and are in the same availability zones.
I believe you cannot do anything without the IGW attached to the routing table where the subnet is attached to.
Another way to do this is to spin off a NAT instance(can be found in AWS marketplace) in the public subnet, add it to your private route table where your original instance is on (0.0.0.0/0 - instanceid) all the traffic will be routed through the NAT instance.
Here mainly we have to see two things, provided subnet for the elasticloadbalancer is public or private.
Every VPC should have one IGW to connect to the public, every trafic should go through the IGW only. If VPC connected to the IGW, IGW distribute internet to the all instance which are in that VPC, if route table changed to the particular rt, internet traffic can send to only that instance. Here in ELB instance is in service because both are in the same VPC and can communicate each other means checks the status. This IGW will work main role when we are using the NAT.
Always we provide ip range for the IGW or rt as 0.0.0.0/0, it represents as public.
The following link will explain more : http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Scenario2.html
This small explanation might be helpful for someone.
Let me cover your two questions
the AWS loadbalalncer endpoint URL not able to load the page, but the instace status shows in AWS loadbalancer as inService
This is the default behaviour of load balancer , since internally load balancer and your instance may be in the same VPC so they are able to communicate that is why loadbalancer is showing inService status.
Second question Why the IGW is required when loadbalancer is configured over private subnet, it technically Mean it's a public subnet, which is blocking me to create a NAT inatance
You need IGW if you want to access any resources especially EC2 , Loadbalancer from internet. however if you put your load balancer in private subnet it means IGW is not associated with the subnet having loadbalancer in it and hence this load balancer is not accessible outside your VPC that is reason that you were not able to load your page.
NAT instance is usually used when you want your private subnet instances should be able to initiate request over internet and has nothing to do with normal load balancing setup unless and untill you want dont want to install any updates from internet to your instance.
You are trying to access webpage publicly and removing the route entry IGW from the loadbalncer subnet.
The Subnet without IGW will become Private, Hence you can't access it.
First, a subnet with route table that route traffic via the internet gateway (IGW) is a public subnet. An IGW is required because the subnets created in AWS VPC are internal IPs and as internal IPs are not routable via the internet, traffic to and fro EC2 instances that belongs to an internal IP needs a way to complete these request. This is where an IGW comes into play. The IGW allows your EC2 instance to make outbound request to the internet and allows other user/client to make inbound request to your ec2 instance.
A public subnet are group of IPs (called subnets) in your VPC that allows internet traffic to and fro your ec2 instance. A subnet without an Internet gateway is a private subnet. As you already guess no traffic is allowed in or out.
That said, instances in VPC which are in Private subnet still need to initiate an outbound request to the internet to download software or perform update. In this case you have to create and attach a NAT gateway or NAT instance to the private subnet. NAT Gateway and NAT instance only allow outbound traffic to the internet but not the the other way round. In some cases you might want your production EC2 to be in the private subnet and ELB in public subnet for security reason.
ELB usually belong to the public subnet so it reachable from the internet as is your case as well.
TO answer some of your question - when you deleted the IGW from the route table, your ELB automatically becomes a private subnet as such your web page stops loading.
Also, you could still see the ec2 instance behind the ELB as InService even when you deleted the IGW because the ELB and EC2 instance can communicate via the internal IP as they are in same network or VPC.
The ELB needs a route to the internet in order to send you the response over internet.. As simple as that.
Configure your ELB in public subnet, regardless of where your instances are present.
Basically there are two types of load balancers.
1)Internal
2) External
Internal load balancers are those which are launched in a private subnet which will be accessible only internally by the instances on same vpc of the internal elb
External load balancers are which are accessible over the internet which should be launched in a subnet which has internet gateway attached to it and which has route table configured properly to route the requests.
If you attach an internet gateway to a subnet it becomes a public subnet.Also if you create a load-balancer which you need to be accessible from internet it should be a External load balancer and aws will not allow it launch in a private subnet.The instances are showing in service because its communicating internally using private ip-address.