NAT instance and NACL configurations - amazon-web-services

I have
two public subnet which has one NAT EC2 instance and one bastion host
one private subnet which has one EC2 instance
I am able to ping google.com from my bastion host but I am unable to do so from my private EC2 instnace.I have checked all steps mentioned here:-
https://aws.amazon.com/premiumsupport/knowledge-center/ec2-internet-connectivity/
I think the problem is with my NACL.
INBOUND RULES OF THE NACL ATTACHED TO THE PRIVATE SUBNET
OUTBOUND RULES OF NACL ATTACHED TO THE PRIVATE SUBNET
Pinging google.com works in case I detach the NACLS from private subnet. Please verify these NACLS
NOTE: - 10.100.3.0/24 and 10.100.0.0/24 are the CIDRs of my public subnet

Your outbound NACL rules are blocking all traffic except those destined to have a final destination of 2 private IP ranges.
Whilst the NAT might be in these subnets, the target is not, you should allow any IP ranges you want to be able to speak out to.

The NACL is stateless. That means you have to open the respective protocol and ports for outgoing requests and you also need inbound rules for the return traffic of outgoing requests. To allow a response to outgoing ping requests, it must allow inbound ICMP traffic. If you want to send HTTP/S requests, you must allow inbound traffic on the ephemeral ports on TCP.
You can learn more about Network ACLs and ephemeral ports here.

Related

aws NLB leads to time-out

i have NLB which has target group to which 3 instance in different AZ are registered.
as i went through documentation NLB doesn't have security group. so i added 3 inbound rules to ec2 instance security group which allow private IP of NLB's network interface from all the three AZ to send tcp traffic to ec2 instance.
Note:- health checks to all 3 ec2 instances are healthy.
when i tried the same thing by allowing traffic my private IP there wasn't time-out issue.
Based on the comments.
Since NLB does not have security groups (SGs), the target instances need to allow either private IP addresses of NLB nodes or IP addresses of clients. This depends whether client IP preservation is enabled or not.
By default:
When you specify targets by instance ID, the client IP of all incoming traffic is preserved and provided to your applications.
Subsequently, SGs of the instances should allow all client IP addresses. Alternatively, you can disable client IP preservation, in which case, the SGs can use private IP addresses of NLB nodes.

Can we make EC2 instances in the web tier as Private?

We have Typical 3 tier architecture having Web, App and DB.
Can we make EC2 instances in the web tier as Private? and allow incoming traffic only through ALB?
AFAIK we can apply an SG only allowing connections from the SG of the ALB. But What if our Private EC2 instance has to return response back to the client? How it'll be routed through ALB as ALB is mostly used for managing incoming traffic.
Also for outgoing traffic can we configure something like Private EC2 instance -> ALB -> Internet? If yes then how? So, is there any way for private EC2 instances to communicate to internet without assigning them public IP?
But What if our Private EC2 instance has to return response back to the client? How it'll be routed through ALB as ALB is mostly used for managing incoming traffic.
You don't have to do anything special, assuming your ALB and instances are correctly set. Any request coming to ALB, will be able to return to the client event if instances are in private subnet without any internet connectivity.
However, your instance won't be able to initiate internet connections by themselves. So if the instances don't need internet to operate, you don't need NAT. Otherwise, it is required.
Keeping your instances in a private subnet is best practice, even for the web layer.
For inbound traffic you would add a load balancer into your public subnets then allow HTTP/HTTPS ingress on the webs security group only from the load balancer. You can either do this through adding the subnet ranges into the web servers security group, or reference the security group the load balancer has assigned to it instead.
For outbound internet traffic in a private subnet you will need to create either a NAT Gateway or NAT instance within a public subnet, and then add a route for 0.0.0.0/0 for the private subnet to route traffic to the NAT. Additionally if you want IPv6 traffic you would create an egress only internet gateway with a route of ::/0.

AWS EC2 instance in private subnet unable to connect to internet via NAT gateway

I have two EC2 instances, one on public subnet(10.1.1.0/24) and one on private subnet (10.1.2.0/24)
EC2 instance in public subnet works fine with internet connectivity and configured services.
There is a NAT Gateway configured in the public subnet 10.1.1.0/24 with Elastic IP.
Default route on private subnet is [0.0.0.0/0 --> NAT Gateway in public subnet].
I have a custom network ACL on private subnet, what I observed is when source and destination for inbound/outbound rules is set to 10.1.1.0/24 (public subnet) external connectivity breaks, but ssh works.
Only when source/destination are set to 'any' does internet connectivity restore.
I am trying to understand what exact source/destination should be used in network ACLs on private subnet for internet connectivity to work.
To allow instance from private subnet access internet using NAT gateway, your NACLs or SG must allow internet traffic. The NAT does not change the internet traffic into private VPC traffic. So if you remove 0.0.0.0/0 the internet connectivity stops.
This is explained in AWS docs on Instances cannot access the internet:
Ensure that the network ACLs that are associated with the private subnet and public subnets do not have rules that block inbound or outbound internet traffic.
Therefore, you must allow 0.0.0.0/0 (or some IP range if you want only part of internet traffic) in your NACL and SG for the instances in private subnet.
As a side note, generally NACLs are not modified, and default settings are usually used. Instead security groups are the first choice of controlling access to and from the instances.

Providing internet access to private Ec2 instance using NAT instance

I was able to connect my private Ec2 instance to the internet through NAT instance.
wget google.com was successful.
But when I attached a NACL to my private subnet.I wasn't able to connect to the internet.Can anyone please tell me what is wrong with my NACL.
10.0.1.0/24 and 10.0.2.0/24 are CIDRs of public subnet(which has the NAT instance)
private NACL inbound rule
private NACL outbound rule
Also, when I add rule to accept all traffic from internet.It works. Is it correct practice to add this rule ?
Based on the comments.
The issue was due to blocking inbound internet traffic (0.0.0.0/0). From the Instances cannot access the internet in AWS docs, to enable internet access of the instance using NAT, the following must be ensured:
Ensure that the network ACLs that are associated with the private subnet and public subnets do not have rules that block inbound or outbound internet traffic. For the ping command to work, the rules must also allow inbound and outbound ICMP traffic.
Question from comments:
But just one question how will whitelisting work in this case. If I only want to download certain libraries using the internet and not allow any api requests ?
If you know IP ranges of the public servers that these libraries are going to be downloaded from, then you could limit the internet traffic to only these ranges. But if the range is not fixed or unknown you have to allow all (0.0.0.0/0) internet traffic in the rules.
Generally, security groups are used as primary way of controlling network traffic. NACLs are good due to their their denying ability. Security groups can't explicitly deny traffic, thus NACLs are helpful in cases where you want to explicitly deny some IPs. But in your case, you are allow only traffic. Thus I think the use of default NACLs would be sufficient and could be considered.

AWS network ACL: why do I need to allow outbound port 80 to be able to browse Internet?

I am very new to AWS and networking. I have been playing with network ACL. I realized that if I don't allow outbound port 443 (HTTPS) on the network ACL, I wouldn't be able to use a browser to go to https://www.google.com from within the EC2 instance in the subnet associated with this ACL.
Likewise, if I don't allow outbound port 80, I wouldn't be able to go to http://www.cnn.com.
This confused me. When I allow port 80 outbound on the ACL, am I allowing the EC2 to talk to port 80 on the CNN server, from an ephemeral port on my EC2, or am I allowing the EC2 to initiate a connection from port 80 on the EC2?
use the security groups at instance level to have security at instance level.And unlike security group, ACL is stateless and works at subnet level i.e if you want your instances to communicate over port 80(http) then you have add an inbound and outbound rule allowing port 80.
You don't have to add any rules. The default network ACL is configured to allow all traffic to flow in and out of the subnets to which it is associated. Each network ACL also includes a rule whose rule number is an asterisk. This rule ensures that if a packet doesn't match any of the other numbered rules, it's denied. You can't modify or remove this rule.
Rules allow all IPv6 traffic to flow in and out of your subnet. We also add rules whose rule numbers are an asterisk (Catch All) that ensures that a packet is denied if it doesn't match any of the other defined numbered rules.
A network ACL has separate inbound and outbound rules (Stateless), and each rule can either allow or deny traffic.
Out Bound Rules allow outbound traffic from the subnet to the Internet. In other words it is matching the traffic flow with the defined rules in the ACLs list and apply it (ALLOW/DENY).
If you have private instances that should not be accessible by public users in the internet it is best practice from security point of view to place these instances in a private subnet and use NAT instance in a public subnet and make all traffic flow goes through this NAT instance to make patch updates and get the public access.
For more information, Check Amazon Docs