Amazon AWS EC2, Allow inbound http Request - amazon-web-services

I have deployed a nodejs application in AWS EC2 instance. It has API gateway with '/api/companies' endpoint. After starting my server I am able to cURL from the instance terminal and it returns the result correctly. But I am not able to make the request from my browser to the EC2 instance. I am not sure exactly how to modify the inbound rules to make this work

You might want to check out the Security Group Rules documentation, but the gist is:
Create a new Security Group in your VPC for your EC2 instance
In the Inbound rules, create a new rule
Set the Source to your IP address. (Optional, if you want to allow access from any IP address, enter 0.0.0.0/0 in the Source)
Set the Protocol to tcp, and set the Port to 80 or 443 depending on what your application uses

Edit your existing security group or add a new security group
Select the VPC the instance is running in. The attached image shows No VPC, but you have to select your VPC
For Source, you can choose My IP if you want to allow inbound traffic from your IP only or from Anywhere in the world

Related

Unable to access AWS EC2 instance

I am unable to access an AWS AMI instance even after setting the inbound rules to allow all traffic:
I get this error:
This site can’t be reached
X.XX.XXX.XX refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
How can I fix this?
I would:
Make sure your inbound rules are as you shown and that your outbound rules do allow all traffic to exit.
In the EC2 Dashboard click on the Instances (running) and then click on the Instance ID. Click on the VPC ID for that instance and then on Main network ACL. Click now on the Network ACL ID and confirm your Inbound rules, Outbound rules and Subnet associations. Make sure nothing here is blocking access. By default the Inbound and Outbound rules will allow all traffic and all subnets will be there.
You do not say so, but I imagine you have SSH access to the instance. Make sure HTTP and HTTPS services are running and listening for connections on the interface IP address and not on 127.0.0.1; something like this:
Make sure IPtables is not blocking access. If you have existing rules you may want to clear them so that they look like:
Run tcpdump and look for traffic on ports 80 or 443
If still not working... make sure you are accessing the right IP address; If you're not using an elastic IP and your restarted the instance it will have a new public IP address.
If this is a NAT instance, you must stop source / destination checking. A NAT instance must be able to send and receive traffic when the source or destination is not itself.
Is your EC2 on a VPC that permits public IP addresses? This can commonly happen when you have accidentally attached the EC2 to a private VPC.
If this is the case make an AMI of the EC2 and re-create it on the public VPC.
Edit:... I had perhaps assumed the issue was simpler than it might be, Dan M explains how to ensure that the HTTP and HTTPS daemon are running, but you could also confirm that it's working "correctly" by running curl http://localhost from the EC2 itself... if this returns the HTML you're expecting then I would recommend going to AWS VPC Network Reachability Analyzer - https://eu-west-2.console.aws.amazon.com/vpc/home?region=eu-west-2#ReachabilityAnalyzer (but you'll need to select the correct region obvs) and create a "path" to test, when this fails (assuming it fails) the report should tell you everything you need to know, and if you're unsure about how to interpret this, post it in here.
NB: perhaps create a path from the internet gateway to the network interface on your EC2 webserver, and define the Destination port - optional as 80.

AWS Network Load Balancer failed to connect with EC2 instance if EC2 instance is not open to public

I have been struggling with this problem for 2 days but couldn't get it working.
I have this flow:
external world --> AWS API Gateway ---> VPC Link ---> Network Load Balancer ---> my single EC2 instance
Before introducing the API Gateway, I want to first make sure the Network Load Balancer --> my single EC2 instance part works.
I have set up the EC2 instance correctly. There is a Typescript / ExpressJS api service running on port 3001
I have also set up a Network Load Balancer and a Target Group, the NLB is listening and forwarding requests to port 3001 of the target group (which contains the EC2 instance).
Here is the NLB:
Note that the NLB has a VPC! This raise the question below and I find it so confusing.
listener:
You can see it is forwarding requests to docloud-backend-service, which is described as follows:
You can see that the health check has passed.
I have configured the security group of my EC2 instance with this rule:
1. Allow All protocol traffic on All ports from my VPC
(specified using CIDR notation `171.23.0.0/16`);
Now, when I do curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user, the command fails by timeout.
Then, after I add this rule:
2. Allow All protocol traffic on All ports from ANY source (`0.0.0.0/0`);
Now, when I do curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user,
the api service gets the request and I can see logs generated in the EC2 instance.
Question:
The second rule opens up the EC2 instance to public, which is dangerous.
I want to limit access to my EC2 instance port 3001 such that only the AWS API Gateway, or the NLB can access it.
The NLB has no security group to be configured. It has a VPC though. If I limit the EC2 instance such that only its own VPC can access it, it should be fine, right?
The first rule does exactly that. Why does it fail?
The NLB has a VPC. Requests go from API Gateway to NLB, then from NLB to EC2 instance. So from the EC2 instance's perspective, the requests come from an entity in the VPC. So the first rule should work, right?
Otherwise why would AWS assign a VPC to the NLB anyways?
Why would I see the VPC on the NLB's description console anyways?
I want to limit access to my EC2 instance port 3001 such that only the AWS API Gateway, or the NLB can access it.
For instance based target groups and for IP based target groups as well we can enable/disable if want to preserve the requester's IP address:
This setting can be found if go to our target group -> Actions -> Edit Target attributes.
What does this mean from the perspective of the Security Group of our application?
If we enable it (which is the default for instance type target groups), the application will see traffic as it is coming directly from the end-client. This means, you we have to enable inbound traffic for 0.0.0.0:3001.
If we disable it, the application will see the source traffic as it was coming from the private IP address of the Network Load Balancer. In this case, we can limit the inbound traffic to the private IP address of the NLB or to the CIDR range of the subnet in which the NLB is placed.

2 ec2 instance interacting with each other

I am new to aws and ec2 interaction with traffic flow.
I have one ec2 instance which I am using as a web server and other as an application server.
how can my two ec2 interact with each other maintaining all the security required?
Both the ec2 machines are on the ubuntu image.
I tried adding All ICMP - IPv4 with source 0.0.0.0/0. I feel it's not the correct way I want only my other instance to access it.
I also tried adding source as other instance security group but didn't work. I was not able to ping from one machine to other
The recommended security configuration would be:
Create a Security Group for the web server (Web-SG) that permits Inbound traffic for HTTP and HTTPS (ports 80, 443). Leave the Outbound configuration as the default "Allow All".
Create a Security Group for the app server (App-SG) that permits Inbound traffic from Web-SG on the desired ports. Leave the Outbound configuration as the default "Allow All".
That is, App-SG should specifically refer to Web-SG in the Inbound rules. This will permit traffic from Web-SG to enter App-SG.
You might want to add additional access so that you can manage the instances (eg SSH), or you can use AWS Systems Manager Session Manager to connect.
Do not use Ping to test access since that requires additional settings and only proves that Ping works. Instead, test the actual access on the desired ports (eg port 80).

Cannot access EC2 via HTTP/HTTPS

I recently had to completely rebuild my AWS EC2 environment, because I accidentally deleted the SSH key, thinking it was something else. Unfortunately, I cannot access my Tomcat instance which I have confirmed is running on that EC2 instance.
I have added the following security groups for inbound HTTP traffic:
Type. Protocol Port Range Source
HTTP TCP 80 0.0.0.0/0
HTTP TCP 80 ::/0
Custom TCP Rule TCP 8080 0.0.0.0/0
Custom TCP Rule TCP 8080 ::/0
SSH TCP 22 (my IP address)
I have three security groups, and the above rules were added to the group named default:
rds-launch-wizard
launch-wizard-1
**default
I purchased a domain via AWS which I have pointed to this EC2 instance using Route 53. Previously, the DNS was available, but now it is not. However, there is a bigger fundamental problem here because I can't even ping the public IP of my EC2 instance.
I am in fact able to access my EC2 instance via SSH on port 22, which is why I was able to setup Java and Tomcat (both of which I have confirmed are running).
I suspect that some state from my previous configuration is responsible for this problem, but I don't even know where to begin looking for something.
Any help would be appreciated.
To have inbound/outbound internet access to your EC2, you need to look for three things:
Are you able to SSH from outside or inside AWS via an internet gateway (IGW)? If you can SSH from outside then you already have IGW setup properly. Otherwise, make sure your subnet's route table points to IGW by having a route like below:
0.0.0.0/0 igw-efxxxxxxx Active No
Network ACL: Go to your EC2's subnet and find its associated NACL
Create both inbound and outbound rules to ALLOW traffic to the above NACL by adding a rule like below:
100 ALL Traffic ALL ALL 0.0.0.0/0 ALLOW
Security Groups: Your security groups look good. No changes required there. However, based on the comments you made below it appears that, while you did define a security group with the proper inbound rules, for whatever reason you did not associate that security group with your EC2 instance. As a result, the inbound rules you defined were not being applied. To fix this, from the EC2 instance tag access the following:
Actions -> Networking -> Change Security Groups
Then, associate your security group with your instance by checking the appropriate box. After making these changes, your inbound rules should take effect, and you should be able to hit your Tomcat instance running on EC2.

Unable to connect to amazon EC2 instance via PuTTY

I created a new instance of Amazon EC2 in Amazon Web Services (AWS) by referring to the documentation. I even added a SSH rule like this:
Port: 22
Type: SSH
Source: <My IP address>/32
I downloaded the .pem file, converted it into .ppk file by using PuTTYGEN. Then I added host name in PuTTY like this:
ec2-user#<public_DNS>
I selected default settings, added that .ppk file to PuTTY, logged in and I got this error:
Even trouble shooting link didn't help me.
I'm also getting this error in system logs:
How can I connect to my Amazon EC2 instance via PuTTY?
Things to check when trying to connect to an Amazon EC2 instance:
Security Group: Make sure the security group allows inbound access on the desired ports (eg 80, 22) for the appropriate IP address range (eg 0.0.0.0/0). This solves the majority of problems.
Public IP Address: Check that you're using the correct Public IP address for the instance. If the instance is stopped and started, it might receive a new Public IP address (depending on how it has been configured).
VPC Configuration: Accessing an EC2 instance that is launched inside a Virtual Private Cloud (VPC) requires:
An Internet Gateway
A routing table connecting the subnet to the Internet Gateway
NACLs (Network ACLS) that permit through-traffic
If you are able to launch and connect to another instance in the same subnet, then the VPC configuration would appear to be correct.
The other thing to check would be the actual configuration of the operating system on the instance itself. Some software may be affecting the configuration so that the web server / ssh daemon is not working correctly. Of course, that is hard to determine without connecting to the instance.
If you are launching from a standard Amazon Linux AMI, ssh would work correctly anytime. The web server (port 80) would require installation and configuration of software on the instance, which is your responsibility to maintain.
Ajay,
Try this. Go to your VPC dashboard. Click on Network ACLs - on the associated acl, update your Inbound Rules to allow SSH access on port22.
Go to vpc attached to instance and then add entry to route table with
0.0.0.0/0 - Destination
Internet Gateway of your VPC - As Target
Save It and try to connect it.
Go to VPC --> Security Group --> Edit inbound rules --> make the ssh source ip (anywhere) then save it and try to login with your putty-client. finally go back to your security group inbound rules and change the source IP from (anywhere) to (my ip) or any custom IP do you want then save it.
note: I assume that you have successfully stored and converted your private key
Security Group - This must accept traffic from your IP address
ex:
Protocol - SSH, PORT-22, IPAddress - SOME IP ALLOW
All Traffic On Any Port From 0.0.0.0/0 means from any IP Address ALLOW
Route Table - Make Sure you have outgoing traffic route enabled
ex:
Destination - 0.0.0.0
target- internet gateway
Use or generate private key
I struggled with this problem for ages after my EC2 instance suddenly started refusing a connection. I tried every answer on SO and Google but nothing helped!
The fix was to make sure that the Network ACL inbound rules were updated to match the rules on the security group.
I have no clue why it worked yesterday and stopped today, but this fixed it.