Setting up NAT / WAF in Amazon AWS - amazon-web-services

I have the following issue:
I want to setup a NAT or WAF in the following scenario:
Internet->VPC->IGW->Rotuer->Subnet1->EB1
->Subnet2->EB2
->Subnet3->EB3
in short words, my scenario is: VPC, Internet Gateway, Route table (Default from VPC) and subnets with EB into.
I tried some steps where I have to create another route table, set NAT instanc as target to 0.0.0.0/0 in this second one and attach the EB subnets to this one; attach the NAT subnet to the main route table and this seems not work.
Tried to Amazon AWS scneario 1 example that is my current configuration without results.
What can be missing to setup correctly it?
Thanks in advance.

I'm not entirely clear about what you are wanting to achieve, but I think you want your subnets to access the Internet, and prevent the Internet from having a direct access to the instances the reside in your private subnet(s).
It's hard to visualize your workflow that way. But i think I follow:
(AWS VPC Internet Gateway) --> Internet
AWS VPC CIDR range contains:
a) public/private subnet #1
b) private subnet #2. private subnet #3.. etc.
A typical set up of achieving requires the following Amazon entities:
1) IGW - internet Gateway
2) VPC with CIDR range of how big or small you want.
for the VMs that you want to protect, while still having internet access, you need them to be assign subnets. The subnets have to be WITHIN the VPC CIDR range. But do not make the subnet to be the same CIDR of the VPC, if you do this, then it essentially makes your private subnet a public accessible one since you have consumed all of the IP range and in which case you must assign it to the IGW which is not what you want.
3) A bastion server - what is a bastion server? This is a server that you purposely put to take internet incoming requests, which protect your private subnets. This can be in the form of a server that acts as a router or NAT. This can be either
3a) a homebrew install of Ubuntu (my fave distro) with iptables and ipv4 packets forwarded, or you can do
3b) a Nat gateway in your amazon VPC, or
3c) a vpc-nat AMI that you can find in the AMI Community. So just chose what method you wish.
4)Create Security group #1 - create a security group for the Bastion Server. I usually call it "NAT SG" and have inbound 0.0.0.0/0 and outbound to TCP 80, 443, 22 and etc, according to your outbound port needs determined by whatever you are running.
5) Security group #2 - create a security group for the private subnet. This is for traffic that will allow all within the subnet(s) and to funnel all outbound non private data to 0.0.0.0/0 to the bastion server.
To implement this is a simplified conceptual order of operations you need to do:
1) Create a VPC with the CIDR range (let's say 10.0.0.0/16)
1.5) create an Internet Gateway and assign it to the VPC (10.0.0.0/16)
2) Create private subnets with CIDR range that's in the VPC CIDR (10.0.1.0/24, 10.0.2.0/24, 10.0.2.0/24, etc).
3) Create a public subnet that will contain the bastion server, or whatever you want the Internet to see. (10.0.254.0/24 (this is huge, allowing 256 servers in the subnet. You will most likely only have the bastion server consuming only 1 server, so it will probably auto assign 10.0.254.2)
4) create 2 routing tables - one called private and one called public. For the private routing table, edit, then add a new Route for 0.0.0.0/0 goes to the bastion server ID. For the public routing table, edit , then add a new route for 0.0.0.0/0 to the Internet Gateway ID (see step 1.5)
5) Either launch your own server and set it up to become a NAT router (if you know how to do that), or start up the vpc-nat AMI (amzn-ami-vpc-nat-hvm-2016.03.0.x86_64-ebs - ami-311a1a5b) is the latest one, so use that. When you create the instance, select the public subnet, the VPC, and assign it a public IP (don't wait and give it an Elastic IP, you will want the public IP),select the "NAT SG" security group, and then Launch it. After it is launched you will select it and select Network --> src/dest. check and DISABLE it. This allows all traffic to funnel in. You only need 1 bastion server, I'm not stating to do all 3.
6) Launch your AMI or custom VM that you want private, and be within the private subnets (10.0.1.0/24, 10.0.2.0/24, 10.0.3.0/24... etc). And assign it a private security group, and the same VPC. If it is linux or windows you may want to generate a new private key (pem) for future login. Convert the pem to a ppk using puttygen.
Testing, in order to ensure you have internet connectivity:
the bastion server must be able to reach the world
the VM instance in the private subnet (if the routing was done accurately), should reach the internet by flowing packets to the 0.0.0.0/0 rule to pass through the bastion server, and the bastion server will allow resolution and flow to the internet for outbound requests.
I usually do a ping to google since they do not block ICMP requests and are quite reliable. So ssh into the bastion server, ping google.com. If you get the IP but it cannot get a reply then it means your Internet gateway or the Security group did not all all outbound traffic 0.0.0.0/0, so double-check that.
Now SSH into the private server (assuming linux) and ping the bastion server (10.0.254.X) and you should be able to get a response. That's a good sign. now ping google.com and see if you get a reply. I hope you do. If you get DNS resolution but it is stuck at get any responses, then you forgot to disable Src/Dest checking on the bastion server. Then go to your ec2 console, locate the bastion server and select, then actions, networking, and select src/dest check and confirm DISABLE and save.
That should make your private network resolve and hit the world, while still being protected from the internet.
I hope this helps.
Richard.

Related

AWS EC2 Internet access from behind Load Balancer

Using Terraform to setup a VPC with two EC2s in private subnets. The setup needs to SSH to the EC2s to install package updates from the Internet and install the application software. To do this there is an IGW and a NAT-GW in a public subnet. Both EC2s can access the Internet at this point as both private subnets are routing to the NAT-GW. Terraform and SSH to the private subnets is done via Client VPN.
One of the EC2s is going to host a web service so a Classic mode Load Balancer is added and configured to target the web server EC2. Using Classic mode because I can't find a way to make Terraform build Application mode LBs. The Load Balancer requires the instance to be using a subnet that routes to the IGW, so it is changed from routing to the NAT-GW, to the IGW. At this point, the Load Balancer comes online with the EC2 responding and public Internet can access the web service using the DNS supplied End Point for the LB.
But now the web server EC2 can no longer access the Internet itself. I can't curl google.com or get package updates.
I would like to find a way to let the EC2 access the Internet from behind the LB and not use CloudFront at this time.
I would like to keep the EC2 in a private subnet because a public subnet causes the EC2 to have a public IP address, and I don't want that.
Looking for a way to make LB work without switching subnets, as that would make the EC web service unavailable when doing updates.
Not wanting any iptables or firewalld tricks. I would really like an AWS solution that is disto agnostic.
A few points/clarifications about the problems you're facing:
Instances on a public subnet do not need a NAT Gateway. They can initiate outbound requests to the internet via IGW. NGW is for allowing outbound IPv4 connections from instances in private subnets.
The load balancer itself needs to be on a public subnet. The instances that the LB will route to do not. They can be in the same subnet or different subnets, public or private, as long as traffic is allowed through security groups.
You can create instances without a public IP, on a public subnet. However, they won't be able to receive or send traffic to the internet.
Terraform supports ALBs. The resource is aws_lb with load_balancer_type set to "application" (this is the default option).
That said, the public-private configuration you want is entirely possible.
Your ALB and NAT Gateway need to be on the public subnet, and EC2 instances on the private subnet.
The private subnet's route table needs to have a route to the NGW, to facilitate outbound connections.
EC2 instances' security group needs to allow traffic from the ALB's security group.
It sounds like you got steps 1 and 2 working, so the connection from ALB to EC2 is what you have to work on. See the documentation page here as well - https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html

How can a lambda inside a private subnet access EC2 in a public subnet?

I've created an EC2 instance inside a public subnet (so that I can access it from my home network) and I have created some Lambda's inside the private subnets of my VPC.
My 1st lambda can freely access the internet (through a NAT Gateway) and do its job. Which is to fetch a file from the internet and upload it to S3, once per day.
My 2nd lambda is supposed to retrieve this file from S3 (which it does without issue) read the file & then upload the data to MySQL running on the EC2 instance. It is unable to connect to the Database (using either the EC2's public or private IP's) and the Cloudwatch logs show that the session times out, making me think this is a networking issue.
I have a 3rd lambda that will also need to interact with the EC2/DB instance.
My security group allows for all incoming traffic from my home network IP, the NAT gateway & the VPC IP range. All outbound traffic is allowed.
I appreciate its not usual to have an EC2/DB set up this way, in a public subnet, but its my preference to interact it with it this way from home using Sequel Pro.
However, is my set up even possible? Eg can my private subnet lambdas interact with a public subnet ec2 instance? if so, does anybody have any ideas how I can make this happen?
It appears that your situation is:
An Amazon EC2 instance running in a public subnet, with MySQL
The EC2 instance has a Security Group allowing all incoming traffic from your home network IP, the NAT gateway and the VPC IP range
An AWS Lambda function connected to a private subnet of the same VPC
A NAT Gateway allowing private subnets to connect to the Internet
The Lambda function is unable to connect with the MySQL database running on the EC2 instance
The normal security configuration for this scenario would be:
A Security Group on the Lambda function (Lambda-SG) that allows all Outbound access (no Inbound required)
A Security Group on the EC2 instance (EC2-SG) that allows inbound access from Lambda-SG on port 3306, plus whatever inbound permissions you want for accessing your instance via SSH, etc.
Given that your Security Group includes "the VPC IP range", this should be sufficient to permit inbound access.
The Lambda function should reference the EC2 instance via its private IP address to keep traffic within the VPC. By default, all subnets within a VPC can communicate with each other unless the Network ACLs have been modified (and they should generally be left at default values).
This means that the only remaining explanation would be that the MySQL database is not accepting traffic from the VPC IP range. (I'm not a MySQL person, but I know that PostgreSQL requires incoming IP ranges to be defined, so this might be true for MySQL too.)
To diagnose what might be happening in your network, I recommend:
Launch another Amazon EC2 instance in the public subnet
Connect to the new instance and try to connect to the MySQL database via the private IP address
If that works, repeat the process but from an EC2 instance in the private subnet. To use this you will need to connect to the 'public' EC2 instance, and from there connect to the 'private' EC2 instance. Then, try and connect to MySQL from that private instance.
These steps will progressively let you identify where the network problem might lie. Let us know what you find!

AWS EC2 not accessible from public IP

My application is working fine on the Amazon EC2 Windows Server. But when I am trying to access the same through the elastic public IP, it is not accessible. I have seen the inbound configurations and the ports(HTTP,HTTPS,SSH) are enabled. Infact, I added a rule to All Traffic from Anywhere. But no success.
Firewall ports are open in host server. The URL is accessible from private IP from other EC2 machines in same network
http://3.45.54.89:8080/My-App/ ----> Public IP- Not Working
http://10.0.2.23:8080/My-App/ ----> Private IP - Working
Please guide. Stuck Badly in this
The fact that you can access the port from within the network points out that you indeed have a rule somewhere in the VPC that is blocking the connection from outside the network.
Here are the components you need to access a resource inside a VPC from outside the VPC:
Internet Gateway: This is the entry point into the VPC. Seems like this is already working as expected as you can access the instance in the first place.
(optionally) Bastion host: If the resource you are trying to connect to is in a private subnet you need a bastion host in the public subnet. Read more about it here.
NACL: If you have not changed the defaults, this should allow the requests to flow through. Note that NACL are stateless i.e. you need to allow both inbound and outbound.
Security Group: As the final layer, the security group should allow the traffic on the ports, to the appropriate cidr block.
Based on the scenario you have described, my guess is that your instance is in a private subnet. If it is, then you need to route the requests through something in the public subnet.
Edit the firewall ports for an IIS website in Windows Server. if the iis apps are not in the inbound list please add them and try again.
https://www.youtube.com/watch?v=YQcklFuWrcw

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.

Nginx Instances which are Launching in Public subnet with public IP in VPC but unable to connect to Network

Our AWS instances are created in a public subnet availability zone and are not able to connect to the internet and SSH. So all the resources are created on the public subnet and one AZ.
I have developed CF nginx template with single VPC and two public subnets, butsecond public subnet instances are unable to connect network and SSH even though I'm giving public IP of the instance in the browser it is not working.
The main issue is instances which are launching in the second public subnet are unable to connect internet system logs are:
Contact the upstream for the repository and get them to fix the
problem
Reconfigure the base URL/etc.
Disable the repository, so yum won't use it by default
Looking at the scenario generally in order to enable access to or from the Internet for instances in a VPC subnet, you must do the following:
Attach an Internet gateway to your VPC.
Ensure that your subnet's route table points to the Internet gateway.
Ensure that instances in your subnet have a globally unique IP
address (public IPv4 address, Elastic IP address, or IPv6 address).
Ensure that your network access control and security group rules
allow the relevant traffic to flow to and from your instance.
To use an Internet gateway, your subnet's route table must contain a route that directs Internet-bound traffic to the Internet gateway. You can scope the route to all destinations not explicitly known to the route table (0.0.0.0/0 for IPv4 or ::/0 for IPv6).
Kindly Refer this AWS Documentation and see what you are missing , as you must have skipped one of the above mentioned things.