I have a Node.js app running on an ES2 instance which I can connect successfully to an AWS RDS Postgres database and run a SQL statement to return the results. Now I want to run that same app but on my local machine instead of the EC2 instance. Can it be done?
I tried using the same key pair instance as I am using for the EC2 instance and created an SSH tunnel with this command
(true endpoints replaced with XXX for security):
ssh -i ec2-db-keypair.pem -N -L localhost:8080:database-test-1.XXX.eu-west-2.rds.amazonaws.com:5432 [email protected]
My Node server is listening on 8081 and when the user lands on the route /db
I want it to connect to the database and run some SQL statements.
The connection pool below works from within the EC2 instance, but not from my local machine:
server.listen({ port: 8081 }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
});
server.get("/db", async (request, reply) => {
console.log("connect to db using slonik");
const pem = fs
.readFileSync(path.join(__dirname, "../../eu-west-2-bundle.pem"))
.toString();
const pool = createPool(
// ec2 instance instead of localhost:8080 I use: database-test-1.XXX.eu-west-2.rds.amazonaws.com:5432
`postgresql://postges:mydbpassword@$localhost:8080/postgres`,
{
ssl: {
ca: pem,
rejectUnauthorized: true,
},
}
);
pool.connect(async (connection) => {
try {
const resultNodejsMyTableRows = await connection.query(
sql`SELECT * FROM nodejs_mytable ORDER BY id ASC`
);
console.log("nodejs_mytable=", { resultNodejsMyTableRows });
} catch (err) {
console.log(err);
}
});
return "look at console for responsesn";
});
Currently I get:
Hostname/IP does not match certificate's altnames: Host: localhost. is not in the cert's altnames: DNS:database-test-1.XXX.eu-west-2.rds.amazonaws.com
Is this the correct way that the SSH tunnel works and if not can someone advise on the correct way?
2