Error when checking connection between ec2 insatnce and rds database - amazon-web-services

I want to test if my ec2 instance can connect to my database.
I ssh into my instance ran the command:
echo '\dS' |psql -h rds-endpoint-xxx.amazonaws.com 5432
the result I get is this:
psql: error: could not connect to server: Operation timed out
Is the server running on host "rds-endpoint-xxx.amazonaws.com" (192.168.12.233) and accepting
TCP/IP connections on port 5432?
What does this mean? and what should i do to fix?
My ec2 instance IP is 192.168.0.138 and in the security group of my rds database I have allowed inbound access from 192.168.0.0

Unless your two VPC are peered, you will not be able to access your RDS from the instance due to your security group (SG). Without peering, the connection from the instance to the rds will go over internet, and your SG does not allow internet connections.
To solve this, you either have to peer your VPCs, which you can't do right now as they have same CIDRs. For peering you need non-overlapping CIDRs for the VPC.
Alternative is to allow internet connections to your RDS, which means you need to allow incoming traffic (0.0.0.0/0) unless you have EIP for the instance. Also RDS must be set to be allow public connections.

Related

Connect to RDS instance from Ec2 instance within VPC

Background:
I have a custom VPC with 2 private subnets that contain a Postgres RDS instance within the us-west-2 region and a public subnet that contains an EC2 instance within the us-west-2 region.
Private Subnet ACL:
Allow all inbound IPv4 traffic on port 5432
RDS instance security group:
Allow all inbound IPv4 traffic on port 5432
Public Subnet ACL:
Allow all inbound/outbound traffic on all ports
Public Subnet has an internet gateway within its route table
EC2 instance security group:
Allow inbound SSH traffic from my local IP on port 22
Allow all IPv4 outbound traffic on ports 5432, 443, and 80
After I SSH into the EC2 instance, I export the environment variables associated with the RDS instance's Postgres credentials (e.g. PGDATABASE=testdb, PGUSER=foo_user, PGHOST=identifier.cluster-foo.us-west-2.rds.amazonaws.com, PGPASSWORD=bar) and run the following python script with python version 3.7.10:
import psycopg2
try:
conn = psycopg2.connect(connect_timeout=10)
cur = conn.cursor()
cur.execute("""SELECT now()""")
query_results = cur.fetchall()
print(query_results)
except Exception as e:
print("Database connection failed due to {}".format(e))
I get the following timeout error:
Database connection failed due to connection to server at "foo-endpoint" (10.0.102.128), port 5432 failed: timeout expired
connection to server at "foo-endpoint (10.0.101.194), port 5432 failed: timeout expired
Have you checked if the VPC's DNS Hostnames are enabled? This is more likely the stemming from the failure to resolve the DNS in the same subnet (Knowing that the security group of the RDS welcomes Postgres traffic from anywhere).
Aside from that, I would also recommend whitelisting the security group of the ec2 + the port of Postgres on the security group of the RDS. You can look at this diagram below:
In general, Network Access Control Lists (NACLs) should be left at their default "Allow All Inbound & Outbound" settings unless you have a very specific network requirement (eg creating a DMZ).
NACLs are stateful meaning that they need to permit traffic in both directions. This is different to a Security Group, which is stateless and allows return traffic to go out in response to permitted Inbound traffic. A Security Group could be configured with zero Outbound rules and would still allow users to connect to the resource and receive a response.
The correct security configuration for your scenario should be:
A Security Group on the Amazon EC2 instance (EC2-SG) that permits inbound SSH access (port 22) from your IP address
A Security Group on the Amazon RDS database (DB-SG) that permits inbound PostgreSQL access (port 5432) from EC2-SG
That is, DB-SG should specifically reference the EC2-SG as being permitted for Inbound access. This is the most secure configuration since the database is only reachable from an EC2 instance that is associated with EC2-SG. If the instance is replaced with another EC2 instance, it will still be able to connect if it is associated with EC2-SG.
Please note that EC2-SG does not require any Outbound rules in the Security Group since it can automatically respond to any inbound requests. However, it is normally recommended to leave the default Outbound rules in place so that software running on the instance can access the Internet (eg to install the psycopg2 library). Instances can normally be trusted to have Outbound access to the Internet since since you have installed the software yourself.

Aurora serverless connection timed out

I'm trying to connect to my aurora serverless but every time I try to do it I receive this error:
2021/03/18 17:10:00 error verifying database connection is alive: dial tcp 10.247.15.113:3306: connect: operation timed out exit status 1
I created a VPC, subnets and security groups.
VPC -> 10.247.0.0/20
4 Subnets -> 10.247.0.0/22, 10.247.8.0/22, 10.247.4.0/22 and 10.247.12.0/22
Security group -> Lives inside my VPC and as inbound has port SSH 22 for 0.0.0.0/0 and MYSQL/Aurora 3306 for my EC2 instance IP address. Outbound has all traffic
Using ssh in a database client works but inside my code I receive the error I mentioned, I also tried doing telnet and I receive another operation timed out.
I know this may be something related to the networking but not sure why since I can connect via ssh with an EC2 instance. What can it be?
Your guide is for RDS. It does not apply to Aurora Serverless (AS). Specifically AS can't be accessed from internet. So you can't connect to it directly from home:
You can't give an Aurora Serverless v1 DB cluster a public IP address. You can access an Aurora Serverless v1 DB cluster only from within a VPC.
You have to connect to it from within a VPC, e.g. EC2 instance, ECS container or a lambda function.
The only way to connect to it from home is to use RDS DataAPI, or setup ssh tunnel or VPN between your home network and your VPC.

Aws Connection to EC2 timed out over SSH

I have tried to connect EC2 using SSH but ssh: connect to host XXXXXXXXX port 22: Connection timed out
Note: XXXXXXXX is user#IP
Also I have checked security groups. Inbound rules are allowed for ssh
SSH TCP 22 0.0.0.0/0 -
SSH TCP 22 ::/0 -
For first time, I was able to login using SSH. After that I installed LAMP stack on EC2 instance. I think I forgot to add ssh in ufw rules.
I can't able to connect using Browser Based SSH Connection in AWS and showing erros for Session Manager connection method.
How can I connect using SSH or other, so I can allow SSH in ufw rules.
This indicates that you cannot to the host.
From your question I can see you have validated security group access, however there are some other steps you should take to investigate this:
Is the IP address a public IP? If so ensure that the instances subnet has a route table with a Internet Gateway associated with it.
Is the IP address a private IP? Do you have a VPN or Direct Connect connection to the instance? If not you would either need to set this up or use a bastion host. Otherwise if you do ensure that the route tables reference back to your on premise network range.
If you're not using the default NACLs for your subnet check that both the port ranges from your security group as well as ephemeral port ranges.

How to configure my RDS postgresql instance so that i can interact with my API server EC2 ubuntu machine?

I am able to access my RDS instance through my EC2 instance through
psql -U user -h hostname $DBname
I want to know how the RDS connects to EC2. is it private IP or public IP or Hostname
my developer has configured my private IP in connection http://privateip:5000 in the backend (don't what exactly it)
When I use this command:
curl --location --request GET 'http://localhost:5000/api/Supplier
I am able to access the API response.
but when I use my
curl --location --request GET 'http://privateip:5000/api/Supplier
I get curl: (7) Failed to connect to privateIp port 5000: Connection refused
I am sure my EC2 can connect to RDS .
Maybe my understanding of this is limited.
could any one help me out of this ?
My RDS SG
All TCP TCP 0 - 65535 Custom sg-EC2
PostgreSQL TCP 5432 Custom 0.0.0.0/0
PostgreSQL TCP 5432 custom sg-Ec2
All UDP UDP 0 - 65535 Custom sg-EC2
All ICMP - IPv4 ICMP 0 - 65535 Custom sg-EC2
If you're trying to connect to the private IP address from the instance itself you will need to ensure the following conditions are met:
Inbound access in the security group from the source of your instance to port 5000.
Outbound access allowed in the security group for your instance.
NACL is either the default NACL, or if you have made your own it is allowing inbound and outbound access including to ephemeral ports.
The source inbound rule for the security group should be scoped to one of the following:
Inbound from the private IP address range of the EC2 instance e.g. 10.0.0.1/32
Inbound from either a subnet or VPC range e.g. 10.0.0.0/16.
Inbound from the world e.g. 0.0.0.0/0 (Use with caution as this allows any server that can connect, to be able to connect on this port).
Inbound from a security group (either the same or different). e.g. sd-abcdef
Rule of thumb for checking SG of instance telnet privateip 5000 if the port is not open it clearly indicate that EC2 instance security group not allowing the port.
Then open 5000 from SG of the instance.
If the above fix does not resolve the issue then it might be the case that application only listening on localhost so you need to allow 0.0.0.0 to work it private IP.
I am sure my EC2 can connect to RDS .
The issue is related to connecting with Ec2 instance not the RDS, you request curl --location --request GET 'http://privateip:5000/api/Supplier has nothing to do with RDS except DB connection. you application depend on Ec2 security group, not the RDS security group.

Can not connect to my Redshift cluster in private subnet

I have set up a VPC using a suggested approach as discussed on Linux Bastion Host Quick Start.
I have also created a Redshift cluster in one of private subnets and also created its dedicated security group with no rule restrictions. That is for both inbound and outbound rules for Redshift I am assigning all traffics and ports (0.0.0.0/0). I am even doing the same for the public EC2 instance on public subnet.
I can successfully ssh to my public bastion instances but from there I can not telnet to my Redshift endpoint.
[ec2-user#ip-10-0-141-20 ~]$ telnet ******.redshift.amazonaws.com 5439
Trying 10.0.20.169...
Connected to ******.redshift.amazonaws.com.
Escape character is '^]'.
Connection closed by foreign host.
I am not sure what is wrong with my configurations. In Redshift I have disabled both public access and VPC routing.
I assume that your situation is:
You have an Amazon Redshift cluster in a private subnet
You have a Bastion server in a public subnet of the same VPC
You wish to connect an SQL Client on your computer to the Redshift cluster
A way to do this would be:
Use Port Forwarding to connect to the Redshift cluster via the Bastion host
If you are using a Linux/Mac:
ssh-add keypair.pem
ssh -A ec2-user#BASTION-IP -L 5439:xyz.redshift.amazonaws.com:5439
(This says: Forward local port 5439 to the bastion, where is should send traffic to the Redshift cluster on port 5439)
If you are using Windows, then you can use Pageant and PuTTY
Then, configure your SQL Client to connect to Redshift with server=localhost and port=5439, together with your login credentials
If the above does not work, some things to check:
The Security Group on the Redshift Cluster should allow inbound connections on port 5439 from the Bastion (or from the whole VPC or from 0.0.0.0/0
The outbound rules on the Bastion should remain at their default setting of allowing all outbound traffic
If things are still going wrong, you can test the Redshift connection by installing psql on the Bastion and attempting a connection to Redshift. (Redshift was forked from PostgreSQL, so it behaves similarly).