AWS - How could I check if EC2 connected with RDS? - amazon-web-services

I created EC2 instance and security group for the RDS ( its type: PostgreSQL and source: launch-wizard-1 (EC2 has) ). And also I created the RDS instance.
Then, Is there any way how could I check if EC2 connected with RDS correctly?
The EC2 instance has no any data, it is just created.

There are two ways to try and connect to your RDS for testing:
Install db client on the ec2 instance and check connectivity.This link will help you :
https://github.com/snowplow/snowplow/wiki/Setting-up-PostgreSQL
You can also connect from your localhost to the rds endpoint using putty and ssh tunneling.
Once connected you can use your local db client like dbeaver for connecting.

install a psql client on the ec2 and connect
for instance
yum install postgres
echo '\dS' |psql -h postgres-db.ca9yqwjklxox.eu-west-2.rds.amazonaws.com
and it should give a list of tables or an error message

Related

Error when using EC2 Instance Connect in the instance was created from Cluster ECS

I want to connect to EC2 instance that I have created in ECS Cluster with EC2 Instance Connect but it always show same error.
There was a problem connecting to your instance
Log in failed. If this instance has just started up, wait a few minutes and try again. Otherwise, ensure the instance is running on an AMI that supports EC2 Instance Connect.
My EC2 instance meet all the requirements to use EC2 Instance Connect (amzn2-ami-ecs-hvm-2.0.20210520-x86_64-ebs) and I have tried with or without SSH key pair assign to instance but all of them show the same error as above.
Anyone know why? Thanks for advance.
I don't think EBS-optmized AMIs come with EC2 instance connect. You have to install it yourself after you establish traditional SSH connection to it, or through a user data:
sudo yum install ec2-instance-connect

AWS RDS Aurora - How to connect using PgAdmin?

Yesterday AWS launched Aurora serverless for PostgreSQL, but it doesn't seem to have the same configuration options as other RDS databases, I can't set it to public facing for example, it forces me to have a VPC.
Now, I have no clue how to apply these VPC things to PgAdmin, I've tried setting the inbound for the security group to all ports and ips but it still won't connect (no server response).
How can I connect to a RDS Database inside a VPC using PgAdmin?
Opening the security group didn't work.
I realize this question is old, but I kept coming back to it as I worked this out.
This solution is similar to #genkilabs solution but simpler.
Steps:
Spin up an ec2 micro instance in the same vpc as the database. You will tunnel through this.
Add the security group for your ec2 to the inbound rules of the database's security group.
ssh into the ec2 instance and install psql (and postgress...) with:
sudo amazon-linux-extras install postgresql10
Verify that you can connect to your database with psql:
psql -h {server} -p 5432 -U {database username} -d {database name} -p
In PGAdmin create a new server connection
Enter the database host, username, and password as usual.
Go to the SSH Tunnel tab
turn on ssh tunneling
enter your ec2 hostname for the tunnel host
enter your ssh username
select the identity file and find the .pem or .cer file for your ec2 instance.
Save and done. You should now be able to connect to the serverless Aurora database from your local PGAdmin.
If you have trouble connecting to the database form the ec2, this guide may be helpful. The same steps apply connecting from ec2 as from cloud9.
EDIT Sept '22: With Serverless V2 you can now select "public access" during the initial create, and connect directly (provided your VPC and security groups allow it). However, it is still recommended for production / "enterprise" use to still connect only though a "bastion" or "jump-box".
Officially, you can't...
Per the docs:
You can't give an Aurora Serverless DB cluster a public IP address. You can access an Aurora Serverless DB cluster only from within a virtual private cloud (VPC) based on the Amazon VPC service.
However, connecting to a serverless DB from a non-Amazon product is just officially discouraged, it is not impossible.
The best solution I have found so far is to create an autoscaling cluster of bastion boxes within the same VPC. Then use them to tunnel through. The great part about this strategy is that it exposes a standard postgre format URL, so it can be used with pgAdmin, Navicat, ActiveRecord or any other ORM that uses typical connection urls.
...The bad part is that (so far) it seems to enforce a 30 sec timeout on connections. So you better get all your transactions wrapped up quick like.
If anyone can do better, I'd love to hear how as well.

problems connecting to AWS DocumentDB

I created a Cluster and an Instance of DocumentDB in amazon. When I try to connect to my Local SSH (MacOS) it displays the following message:
When I try for the MongoDB Compass Community:
mongodb://Mobify:<My-Password>#docdb-2019-04-07-23-28-45.cluster-cmffegva7sne.us-east-2.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0
It loads many minutes and in the end it has this result:
After solving this problem, I would like to know if it is possible to connect a cluster of documentDB to an instance in another zone of availability ... I have my DocumentDB in Ohio and I have an EC2 in São Paulo ... is it possible?
Amazon DocumentDB clusters are deployed in a VPC to provide strong network isolation from the Internet. To connect to your cluster from outside of the VPC, please see the following: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html
AWS document DB is hosted on a VPC (virtual private cloud) which has its own specific subnets and security groups; basically, anything that resides in a VPC is not publicly accessible.
Document DB is deployed in a VPC. In order to access it, you need to create an EC2 instance or AWS Could9.
Let's access it from the EC2 instance and access AWS document DB using SSH tunneling.
Create an EC2 instance (preferably ubuntu) of any configuration and select the same VPC in which your document DB cluster is hosted.
After the EC2 is completely initialized, start an SSH tunnel and bind the local port # 27017 with document DB cluster host # 27017.
ssh -i "<ec2-private-key>" -L 27017:docdb-2019-04-07-23-28-45.cluster-cmffegva7sne.us-east-2.docdb.amazonaws.com:27017 ubuntu#<ec2-host> -N
Now your localhost is tunneled to ec2 on port 27017. Connect from mongosh or mongo, enter your cluster password and you will be logged in and execute any queries.
mongosh --sslAllowInvalidHostnames --ssl --sslCAFile rds-combined-ca-bundle.pem --username Mobify --password
Note: SSL will be deprecated. Use tls, just replace SSL with tls in the above command.

Not able to connect to AWS DocumentDB from my ubuntu EC2 machine

I can't connect to my Amazon DocumentDB from my amazon EC2 ubuntu machine? I've checked the security of the Amazon DocumentDB and it's currently assigned to the default which has "all traffic".
I've tried the following command, straight out of the AWS instances page although I receive the error message included below.
I've followed this aws guide https://docs.aws.amazon.com/documentdb/latest/developerguide/getting-started.connect.html
Mongo shell command from the EC2 ubuntu machine
mongo --ssl --host mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username webuser --password mypassword
The error message I receive from within the ubuntu command prompt is below
Error message
MongoDB shell version v3.6.11
connecting to: mongodb://mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017/?gssapiServiceName=mongodb
2019-03-11T21:39:37.587+0000 W NETWOK [thread1] Failed to connect to 172.31.45.184:27017 after 5000ms milliseconds, giving up.
2019-03-11T21:39:37.595+0000 E QUERY [thread1] Error: couldn't connect to server mydatabasename.23scnncsd3.eu-west-1.docdb.amazonaws.com:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:263:13
#(connect):1:6
exception: connect failed
Am I doing something wrong? Any help appreciated!
Many thanks,
Update
Amazon DocumentDB deploys clusters within a VPC, which act as a strong network boundary to other VPCs and the Internet. When you are connecting to your cluster, ensure that the client machine is in the same region and the same VPC as the cluster.
Alternatively, if your development environment is in a different Amazon VPC, you can also use VPC Peering and connect to your Amazon DocumentDB cluster from another Amazon VPC in the same region or a different region.
For more information on troubleshooting: https://docs.aws.amazon.com/documentdb/latest/developerguide/troubleshooting.html
Connecting to an Amazon DocumentDB cluster from outside a VPC: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html
Had the same problem.
Availability Zone/VPC/Security Groups are the same for
EC2 instance and DocumentDB instance, but still failed to connect.
For some reason, the US documentation is missing one step that is present in CN documentation.
https://docs.amazonaws.cn/en_us/documentdb/latest/developerguide/connect-ec2.html
All you need to do is to add another inbound rule to the Secutity Group for TCP and 27017 port. This worked for me.
https://i.stack.imgur.com/lOqov.png

How to create table in RDS database on amazon web service

I am using AWS Amazon Web Service RDS for database. I have set up a database instance but how should I add the tables in that database instance or create any new tables in the database? Any idea how to add table?
Which RDS are you using? I have been using RDS for MySQL for a long time. I create an RDS instance of MySQL and then connect to it from my laptop where I have install MySQL client program. Once I am connected, I can run all the MySQL commands just as if I ma connected to a remote database. I can create DB, Tables...blah blah..
You should provide information on the RDS instance that you are using and how your connecting to it.
Is your database in private or public subnet? That matters as it will affect how we can connect to it. If your RDS database is in a private subnet, then you cannot directly connect to it with a SQL client like MySQL Workbench from your PC.
I would advise you to put the database in private subnet for security concerns.
Now let's assume your database is in private subnet. What you can do is to rent a very cheap EC2 instance as a bastion instance in a public subnet. You can use SSH over TCP/IP or SSH tunneling to connect to this RDS instance even from outside your VPC as mentioned here https://www.youtube.com/watch?v=gM7JvNMOUQM.
Here is a blog that has some details of how to do it. https://bobbyhadz.com/blog/aws-cdk-rds-example
You can create a lambda function in the VPC, use an ORM to create migrations and call the function from the command line using aws command line tool.
If you manage to ssh to your EC2 instance you can connect to your database using MySQL command line tool.
mysql -h [DatabaseConncetivityEndpoint] -u [usernameOfDatabaseInstance] - p [password]
Make sure that you have installed MySQL client using > mysql -v. If not you can install MySQL client inside EC2 instance:
> yum update
> yum install mysql
Then, you can flow the normal MySQL operation on the database.