How can I connect to a private Amazon RDS instance from local node js application which was earlier using non-ssh sequelize connection?

1

After successfully making Amazon RDS instance private (by following this method), I am not able to connect to the same RDS instance from the node.js application running on my local machine which was earlier using non-ssh sequelize connection.

2 Answers
0

Hi, to connect to a private Amazon RDS instance from a local Node.js application, you will need to use an SSH tunnel. This will allow you to securely connect to the RDS instance from your local machine.

Here are the steps to set up an SSH tunnel to connect to an Amazon RDS instance:

  1. Install an SSH client on your local machine. If you are using a Mac or Linux system, you can use the built-in ssh command. Otherwise, you can use a third-party SSH client such as PuTTY.

  2. Generate an SSH key pair on your local machine. This will consist of a private key and a public key. The private key will be used to authenticate your connection to the RDS instance, and the public key will be added to the RDS instance's security group to allow access.

  3. Create a new security group in Amazon RDS, and add the public key to it. This will allow your local machine to access the RDS instance using the private key.

  4. Use the ssh command (or your third-party SSH client) to create an SSH tunnel to the Amazon RDS instance. The tunnel will forward traffic from your local machine to the RDS instance, allowing you to connect to the instance using your local application.

  5. In your Node.js application, use the sequelize library to connect to the RDS instance using the tunnel. The connection details will be the same as if you were connecting to the RDS instance directly, but you will need to specify the localhost port that the tunnel is forwarding traffic to.

Here is an example of how to create an SSH tunnel to an Amazon RDS instance using the ssh command:

ssh -i /path/to/private/key -N -L localhost:3306:rds-instance-endpoint:3306 ec2-user@rds-instance-endpoint

This command will forward traffic from your local machine's port 3306 to the RDS instance's port 3306 over an encrypted SSH connection. You will need to replace /path/to/private/key with the path to your private key file, rds-instance-endpoint with the endpoint of your RDS instance, and ec2-user with the user name for the EC2 instance hosting the RDS instance.

Once the tunnel is established, you can use the sequelize library in your Node.js application to connect to the RDS instance using the localhost port that the tunnel is forwarding traffic to. For example:

const sequelize = require('sequelize');

const db = new sequelize('database', 'username', 'password', {
  host: 'localhost',
  port: 3306,
  dialect: 'mysql',
});

This will connect to the RDS instance through the SSH tunnel, allowing you to access the database from your local Node.js application.

Follow me

profile picture
answered a year ago
  • Step 2 says "Generate an SSH key pair on your local machine". What are the steps (or a link) to explain how to do this?

    Step 3 says "Create a new security group in Amazon RDS, and add the public key to it." I do not see anywhere in the RDS console to create a new security group. Can you please explain how to do this from the RDS Console?

    I assume you mean to add a new security group to the EC2 instance. When editing a security group in the EC2 instance there is no option to add a public key. Can you please explain how to do this?

0
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions