ELB connection issue for port 80 from ec2 instance - amazon-web-services

I have a loadbalancer classic2-**.us-east-1.elb.amazonaws.com and its public , i have whitelisted the port 443 and 80 for all connection and was connecting fine from another public ec2 server as expected :
...
ec21~]#telnet classic2-**.us-east-1.elb.amazonaws.com 80
Trying ***...
Connected to ec2-***.compute-1.amazonaws.com.
Escape character is '^]'.
...
Later I changed the incoming security gruop for the loadbalancer for port 80 and allowed only ec2 to access port 80 for the loadbalancer. For that I have edited the security group for the loadbalancer inbound rule and added source as ec2 security grop name (sg-****). After saving that rule I tried telnet to port 80 from ssh to loadbalancer but its not accespting the connection :
....
# telnet classic2-**.us-east-1.elb.amazonaws.com 80
Trying ****...
telnet: connect to address ****: Connection timed out
....
Not sure why its rejecting. Both instance and elb are in public subnet and elb not working with ec2 instance security group as source.
Any advice, thanks

I suspect that the Load Balancer is configured as a Public Load Balancer. As a result, the DNS Name will resolve to a Public IP address. Therefore, the telnet connection will be connecting to the Public IP address of the load balancer. (You can test this by resolving the DNS Name to an IP address, such as using nslookup or even ping.)
However, when one security group refers to another security group, it permits the connection via a Private IP address because it expects the connections to happen totally within the VPC.
There are two ways to resolve this:
Change the Load Balancer to be an Internal Load Balancer, OR
Change the security group to permit inbound connections from the Public IP address of the instance, rather than the Security Group identifier

Related

security group aws specific ip http/https blocks everything

I've follow the documentation of I've read https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html
and I want to create a security group in AWS which allows only one IP access to ports 80 or 443, but AWS is blocking everything even the IP which should have access when I apply this group.
We are using nginx in the ec2 server and the certificate was created with certbot
What do you mean by "blocking everything"?
From these 2 rules, port 80 and port 443 are only open to the one IP that you had given. If this is a webapp, it is likely that you'll have a loadbalancer setup to receive the traffic.
Check the ELB security group and block traffic there (If there is an ELB setup)
Check the VPC NACL if there are any block for port 80/443 traffic. If that is the case, NACL rule will take precedence here
Make sure you check your outbound rules also. If by "Blocking everything", you meant the outbound traffic
Edit the inbound rule to be only lock out any other port to the instance ip address only, while you open 443 and 80 to everyone.
eg. if ur ec2 instance public ip is 13.255.77.8 and you don't want port 5000 to be accessible to the public, create a custom tcp with your that is only acessible to that port ie mapping port 5000 to this ip - 13.255.77.8/32

EC2 Instance not Reachable on a specific port using its public DNS

I created a new EC2 instance and a new SG and set the inbound rules to accept custom TCP on port 8080 as well as HTTP and SSH and use that one for my EC2 instance. I can ping the Public DNS and get a "connection refused". The problem is when I create a simple node server on the instance and start it to listen to port 8080, ec2-x-x-x-x.compute-1.amazonaws.com:8080 times out.
Now, if I reroute incoming traffic from port 80 to port 8080 using iptables I can just call the Public DNS and of course I get a response. I also can use a load balancer for this purpose but my question is, does the Public DNS resolve to the VPC? why can't I just hit the endpoint ec2-x-x-x-x.compute-1.amazonaws.com:8080 and get a response from the node server that's running on my instance?

Internal networkloadbalancer wont route to instance X when curl from NLB DNS from instance X

I have an internal of a network-load-balancer (NLB) (resolving to private ips)
An NLB listener on port 80 points to a target group. An instance 10.141.80.140 in the target group is the only one.
Problem:
When I am on the instance 10.141.80.140 and curl the DNS of NLB
I get no response.
I expect the NLB to redirect to 10.141.80.140 but it doesnt happen.
The NLB DNS only doesnt redirect, when I am on the 10.141.80.140 - the redirection works from other instances in the same subnet
Details:
The security group around the EC2 10.141.80.140 is world open, inbound and outbound
When I curl the NLB DNS from another instance 10.141.80.122 in the same subnet with the same security group and other settings - NLB resolves correctly to 10.141.80.140
When I curl the NLB DNS from the instance, to which NLB should resolve 10.141.80.140 - NLB DOESNT resolve to 10.141.80.140
When I curl the instance ip 10.141.80.140 from the instance 10.141.80.140 - I get a response
When I curl the instance ip 10.141.80.140 from the instance 10.141.80.122 - I get a response
Question:
Is there something, what prevents NLB to resolve the request of an instance,
which would route back to the instance, within the NLB listeners target group?
that is a well-know behavior that I am going to be glad to explain. Network Load Balancer introduced the source address preservation feature - the original IP addresses and source ports for the incoming connections remains unmodified. When the target answers a request, the VPC internals capture this packet and forwards it to the NLB, which will forward it to its destination.
This behavior has a side effect: when the OS kernel detects that the egress packet has as the destination address one of the local addresses, it will forward this packet directly to the application.
For example, given the following components:
We have an internal NLB and a backend instance. Both are deployed in the subnet 10.0.0.0/24.
The NLB has the IP 10.0.0.10 and a listener on port 80 that forwards the request to the port 8080.
The backend instance has the address 10.0.0.55 and has a web server listening on port 8080. It has a security group that allows all the incoming local traffic.
If the instance tries to establish a communication with the NLB; the flow of the communication would be the following:
The instance wants to telnet the NLB: it does a request for establish a TCP connection against the NLB DNS name on the port 80.
As it is an outgoing communication, it starts from an ephemeral port; the instance sends a SYN packet (1):
Source: 10.0.0.55:40000
Destination: 10.0.0.10:80
The NLB receives the packet and forwards it to the backend instance (10.0.0.55:80).
Due the address preservation feature, the backend instance receives a SYN packet with the following information:
Source: 10.0.0.55:40000
Destination: 10.0.0.55:80
The Operation system routes the packet internally (as its destination is the own machine), and here is when the issue happen:
The initiating socket is expecting the SYN_ACK from 10.0.0.10:80 (the NLB).
However, it receives the SYN_ACK from 10.0.0.55:40000 (the instance itself).
The OS will send several TCP_RETRANSMISSION until it times out.
This will not happen with a public NLB, as the instance will need to do NAT in the VPC to use its public IP address to send the request to the NLB. The kernel will not internally forward the packet.
Finally, a possible workaround is registering the backends as per their IP address, not by their Instance ID; with this method, the traffic forwarded by the NLB will contain the NLB internal IP as the source IP, disabling the "source address preservation" feature. Unfortunately, if you are launching instances with an AutoScaling Group, it will only be able to register the launched instances by its ID. In case of ECS tasks, configuring the network as "awsvpc" forces the NLB to register each target by its IP.

Route only api traffic to other AWS EC2 instance

I am new to networking. And I am trying to route only traffic from one VM traffic to another VM. Therefore, I have done this.
I have two AWS EC2 instances as:
Application Server
Database Server
And they have their own security groups and I have allowed all traffic is permissible. Now I want to Database_server accepts only Application_server traffic not all public traffic. Database_server is MySQL which is running on 3306 port.
Suppose:
Application_server Public IP: 14.233.245.51
Database_server Public IP: 15.233.245.51
So I have allowed on port 3306 like this 14.233.245.51/32 for only Database_server but it did not work. It was before this 0.0.0.0/0 and ::/0.
How can I solve this?
First, the application server should communicate with the database server via private IP address. This will keep all traffic within the VPC and will enable security groups to work correctly.
Second, configure the security groups:
App-SG should be associated with the application server and permit incoming traffic on the appropriate ports for the application (eg 80, 443)
DB-SG should be associated with the database server and permit incoming traffic on port 3306 from App-SG
That is, DG-SG permits inbound traffic from App-SG by referring to the ID of App-SG. There is no need to specify an IP address. The security groups will automatically recognize the traffic and permit the App server to send traffic to the DB server. Return traffic will also be permitted because security groups are stateful.
You MUST communicate with the database server via private IP address for this to work.

AWS classic load balancer not seeing instances with only private IP addresses

I have setup an internet facing classic load balancer and when I provision an EC2 instance with a public IP address the load balancer can do the health check successfully but if I provision an identical instance without a public IP address the health check always fails. Everything is the same apart from not adding a public IP address. Same subnet, security groups, NACL etc.
The health check is TCP 80 ping. I have a web server on all instances and LB is listening on port 80.
Any ideas why it could be failing?
Solved. The instance without a public IP is failing to download and install the web server (httpd) so that is why the TCP 80 ping is failing. To access the web I need to use a NAT gateway or put a public IP on it.
curl -I 80 will show you if your web server is listening on that port.