Cannot connect to SQS from EC2 instance due to time out - amazon-ec2

The error I see is: Unable to execute http request : Connect to sqs.us-east-1.amazonaws.com : 443
org.apache.http.conn.ConnectTimeoutException: Connect to sqs.us-east-1.amazonaws.com:443 [sqs.us-east-1.amazonaws.com/54.239.27.172] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:76)
at com.amazonaws.http.conn.$Proxy54.connect(Unknown Source)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1181)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
... 21 common frames omitted
The outbound rules for my security group are:
All traffic 10.0.0.0/8
I cannot change the outbound rules to allow all internet - that is a limitation.
Any idea how we can do this?

Amazon SQS is an Internet-based service. To connect to the Amazon SQS Endpoint (sqs.us-east-1.amazonaws.com), the Amazon EC2 instance requires access to the Internet.
Your Amazon EC2 instance is in a private subnet, which means it does not have direct access to the Internet. Therefore, you would need:
Either a NAT Instance or a NAT Gateway in the Public Subnet of the VPC
A Route Table associated with the Private Subnet that routes Internet-bound traffic to the NAT Instance or NAT Gateway

As John stated, AWS APIs are public endpoints (Exceptions are VPC Endpoints, which are VPC-private endpoints, currently only available for S3 and DynamoDB). From a private subnet you need a NAT to access those public endpoints.
However, in order to limit access from your instances to only AWS services, you need to configure the security group's egress rules.
Amazon publishes IP-Ranges for its endpoints and also allows you to subscribe to change notifications:
Whenever there is a change to the AWS IP address ranges, we send notifications to subscribers of the AmazonIpSpaceChanged topic. The payload contains information in the following format:
{
"create-time":"yyyy-mm-ddThh:mm:ss+00:00",
"synctoken":"0123456789",
"md5":"6a45316e8bc9463c9e926d5d37836d33",
"url":"https://ip-ranges.amazonaws.com/ip-ranges.json"
}
What you get from https://ip-ranges.amazonaws.com/ip-ranges.json is a json describing AWS services and their IP-ranges.
{
"syncToken": "0123456789",
"createDate": "yyyy-mm-dd-hh-mm-ss",
"prefixes": [
{
"ip_prefix": "cidr",
"region": "region",
"service": "subset"
}
],
"ipv6_prefixes": [
{
"ipv6_prefix": "cidr",
"region": "region",
"service": "subset"
}
]
}
In order to restrict egress traffic, add a rule to your SG following these instructions from the AWS documentation:
To allow an instance to access only AWS services, create a security group with rules that allow outbound traffic to the CIDR blocks in the AMAZON list, minus the CIDR blocks that are also in the EC2 list.
For current details and IP-range json format, see section "Implementing Egress Control" in Amazon's general documentation

Updates here. Now VPC Endpoints supports endpoints for most AWS services

Related

EC2 instance with EIP in Public subnet not connecting to Internet

I have a strange problem wherein I am able to connect to an EC2 instance in a public subnet in AWS VPC using RDP and HTTP. However after successfully connecting to the instance through RDP, i am not able to connect to Internet (from the instance).
a) I created a non-default AWS VPC and associated an IGW to it. The address range of VPC is 10.0.0.0/16. A subnet with the address space 10.0.5.0/24 was created
c) In order to make the subnet public, i decided to retain the Main Route Table as it is and created an additional Route table with the following entries
10.0.0.0/16 local
0.0.0.0/0 igw
d) This route table was associated with the subnet 10.0.5.0/24. This makes it a public subnet
e) The NACL is set to allow:
INBOUND traffic HTTP(80),HTTPS(80),SSH(22), RDP(3389) for IPV4 and NACL
outbound traffic ALL TRAFFIC ALL PROTOCOL ALL PORT Range Destination 0.0.0.0/0
f) The security group settings are:
Inbound All traffic All All 0.0.0.0/0
Outbound All traffic All All 0.0.0.0/0
g) I created a single EC2 instance in the public subnet and assigned a Elastic EIP to it. I am able to connect to this using RDP and able to access the IIS Welcome page by typing the public DNS name of the EC2 in my local browser. However, from the instance when i open IE and try accessing any popular websites i am not able to access the Internet. I always get a 'Can't reach this page'
h) I disabled Windows Firewall and tried accessing Internet. But still it didnt work.
i) The tracert output just shows Request timed Out in all the lines.
Any help would be appreciated.
Thanks and Regards
Govind
First, verify that the following conditions are met:
The route table in the instance’s subnet has a default route to an
internet gateway. The security group attached to the instance’s
elastic network interface must allow outbound traffic on the following
ports: Port 80 for HTTP traffic Port 443 for HTTPs traffic Identify
the network access control lists (ACLs) that are associated with the
subnet that the instance is located on. These network ACLs must have
rules to allow inbound and outbound traffic on ports 80 and 443.
You don't need to open all the ports to enable outgoing web traffic.
https://aws.amazon.com/premiumsupport/knowledge-center/ec2-connect-internet-gateway/
Finally i was able to fix this issue. I added
ALL TCP TCP (6) 0 - 65535 0.0.0.0/0 ALLOW
as an INBOUND rule in NACL. Immediately i was able to access the Internet from the browser in Ec2 instance.
However i am not convinced that all the ports should be opened to use browser on an EC2 instance in the public subnet. Is there a better way to do this ?
Can i consider setting up a Forward Proxy server as a solution for browser based outgoing traffic as in this case ? Please suggest

EipAddress allocation in Alibaba Cloud VPC

According to Alibaba Cloud EipAddress Allocation.
An available EIP is randomly allocated in the specified region after this API is called. EIP supports ICMP, TCP, and UDP protocols, but does not support IGMP and SCTP protocols.
I create an EipAddress on Alibaba Cloud VPC using below mentioned Request code:
https://vpc.aliyuncs.com/?Action=AllocateEipAddress
&RegionId=cn-beijing
&CommonParameters
How I can know which IP is assigned to my vpc.
If you want to know which IP address is created try this API call.
https://vpc.aliyuncs.com/?Action=DescribePublicIpAddress
&RegionId=cn-beijing
&<CommonParameters>
Here is sample response.
{
“RequestId”:” 365F4154-92F6-4AE4-92F8-7FF34B540710”,
“Code”:200,
“Success”:”true/false”,
“PublicIpAddress”:[
“110.11.1.0/24”
],
“RegionId”:”cn-beijing”,
“PageNumber”:1,
“PageSize”:100,
“TotalCount”:1000
}
From documentation https://www.alibabacloud.com/help/doc-detail/65592.htm?spm=a2c63.p38356.b99.76.667b30a6zlzLJZ
You can check which IP address is assigned by calling the following API Request:
Request:
https://vpc.aliyuncs.com/?Action=DescribePublicIpAddress
&RegionId=cn-beijing
Please replace your RegionID if you are using it in a different region.
The Action DescribePublicIpAddress is used to query the IP address range in a specified region.
Response:
{
“RequestId”:” 123425345345252”,
“Code”:200,
“Success”:”true”,
“PublicIpAddress”:[
“111.10.1.0/24”
],
“RegionId”:”cn-beijing”,
}

Public IP address for outgoing traffic AWS

I'm using a AWS EC2 instances on VPC with an Internet Gateway and I need to connect my companies network which only accept connections from specific IP addresses(Firewall). The Outbound Traffic of my IP Address is the Internet gateway's IP Address. I have tried using a NAT Gateway, but the NAT Gateway Still uses an Internet Gateway.Is it possible to have Elastic IP for all outgoing traffic on AWS?
If you have as a destination a set of static IP addresses (the ones of your company), I solved the same by doing the following (based on this post):
Create a new subnet to host the NAT Gateway.
Create the NAT Gateway in the above subnet, and assign a new Elastic IP. This one will be our outgoing IP for hitting external APIs.
Create a route table for the NAT subnet. All outbound traffic (0.0.0.0/0) should be routed through the NAT Gateway. Assign the created subnet to use the new route table.
Modify the main route table (the one that handles all our EC2 instances requests), and add the IP(s) of the external API, setting its target to the NAT Gateway.
This way you can route any request to your company IPs through the NAT Gateway. All other requests are routed through the default Internet Gateway.

Do I need to set up route table for my laptop to ssh to an instance with public IP?

Let me first describe my operational steps:
I set up a VPC with CIDR 10.20.0.0/16 (and I created and attached to it an IGW, igw-14ed6f75).
And then I set up a subnet subnet_A 10.20.1.0/24. This subnet's route table is 10.20.0.0/16 -->local (NOTE: I did not set route of 0.0.0.0/0 --> igw-14ed6f75 in this route table)
I started an instance instance_A in subnet_A, and its automatically created public IP is 52.53.245.253. (BTW, the key pair used for this instance is bastion_box)
I wanted to ssh to instance_A by ssh -i ~/.ssh/bastion_box.pem ec2-user#52.53.245.253
Result/Observation: I can't ssh into instance_A
Now I added one route 0.0.0.0/0 --> igw-14ed6f75 into the route-table associated with subnet_A, and then I do the ssh operation again ssh -i ~/.ssh/bastion_box.pem ec2-user#52.53.245.253
Result/Observation: I can successfully ssh into instance_A now
Here is my question: why do we need the route of 0.0.0.0/0 --> igw-14ed6f75 to be able to successfully ssh into the instance? I thought this rule is for the instance connect out to Internet, but not for the outside machines to connect in the subnet.
TCP connections are two-way connections. The server needs to be able to send traffic to the client (and once the initial handshake is done, the two are indistinguishable). Without an egress route, the server could not send any packets back to the client to establish the connection.
From http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Internet_Gateway.html (bold added):
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 public IP addresses or Elastic IP addresses.
Ensure that your network access control and security group rules allow the
relevant traffic to flow to and from your instance.

How to set up EC2 with public IP for connections from itself?

I have an EC2 instance (running kafka) which needs to access itself via public IPs, but I would like to not open the network ACLs to the whole world.
The rationale is that when a connection is made to a kafka broker, the broker advertises which kafka nodes are available. As kafka will be used inside and outside EC2, the only common option is for the broker to advertise its public IP.
My setup:
an instance, with public IP (not an elastic IP)
a vpc
a security group, allowing access to the kafka ports from my work network
an internet gateway
a route allowing external access via the gateway
The security group is as follow:
Custom TCP Rule, proto=TCP, port=9092, src=<my office network>
Custom TCP Rule, prtot=TCP, port=2181, src=<my office network>
In short, all works fine inside the instance if I use localhost.
All works fine outside the instance if I use the public IP.
What I now want is to use kafka from inside the instance with the public IP.
If I open the kafka ports to the whole world:
Custom TCP Rule, proto=TCP, port=9092, src=0.0.0.0/0
Custom TCP Rule, prtot=TCP, port=2181, src=0.0.0.0/0
It works, as expected, but it does not feel safe.
How could I setup the network ACL to accept inbound traffic from my local instance/subnet/vpv (does not matter which) without opening too much?
Well, this is not clean, but it has the added advantage of not having to pay for external bandwidth.
I did not find a way as I expected (via the security groups), but just by updating the /etc/hosts on my ec2 instance, and actually using a hostname instead of an IP, all works as expected.
For instance, if I give the instance the hostname kafka.example.com, then by having the following line in /etc/hosts:
127.0.0.1 kafka.example.com
I can use the name kafka.example.com everywhere, even if it actually points to a different IP depending on where the call is made.

Resources