简体   繁体   中英

How to host a node js / express server with MySQL on Elastic Beanstalk AWS

I am trying to host my Node js / express server on AWS Elastic Beanstalk. My express server is connected to a MySQL database. So far, I have hosted the MySQL database on an RDS instance on AWS. I have also created an elastic beanstalk environment and "deployed" the Node js server using a CodePipeline (which in connected to GitHub). I have found this process very interesting and feel like I am very close. However, I am running into trouble with the MySQL connection. Here are the relevant lines of code from my Node server:

const express = require("express");
const mysql = require("mysql");
const cors = require("cors")
const formData = require("express-form-data");
const { PORT = 5000 } = process.env

const app = express();
app.use(cors());
app.use(formData.parse({maxFieldSize: '10mb'}));

var mysqlConnection = mysql.createConnection({
    host: "my-rds-dbconnection-endpoint",
    user: "my-username",
    password: "****",
    database: "nameOfSchema"
})

mysqlConnection.connect((err) => {
    if(err) {
        console.log('THIS IS NOT CONNECTING #20')
        throw err;
    }
    console.log("Connected to AWS!")
})
console.log('PORT', PORT)
app.get('/', (request, response) => {
    response.send('Hello World!')
    console.log('hello from /')
});
app.listen(PORT, function() {
    console.log(`App listening on port ${PORT}`);
});

When I run the server using Node in my command prompt for testing purposes, I make it all the way to the message "connected to AWS" and it seems to make the connection to the database. However, when I connect to the environment URL, I get the expected output 'Hello World', but when I download the logs to see what happens, the 'THIS IS NOT CONNECTING' statement prints. Obviously, when I access this node server from the elastic beanstalk environment, it is not connecting to my database. I am unsure why. Does anyone have any ideas about what I could be missing? I sure would appreciate your experience/help!!!

Seeing that you can connect from your local environment and not in your Elastic Beanstalk environment, I assume it's a connectivity issue. There are a few things you can try.

1. RDS Instance - Security Group: Temporarily allow all connections

See if the security group for the RDS instance allows connection from your Elastic Beanstalk environment. You may have whitelisted your own local PC but may not have for other IPs/Security Groups. As an easy check you can allow all connection temporarily to see if this is the issue.

2. RDS Instance - Public Accessibility: Set to Yes/True

For this, I believe it's already configured to True, since you can connect from your local environment with the code provided. Mentioning it here just in case.

3. Elastic Beanstalk - VPC: Set it to be the same as RDS

Connecting EC2(by Elastic Beanstalk) & RDS in different VPC is more complex. Also it makes sense to put them together if you own both environments. After configuring the VPC settings in the Elastic Beanstalk Console , you can set up the security group for the DB and EC2 in the same VPC .

4. Elastic Beanstalk - Security Group: Temporarily allow all connections

While your Elastic Beanstalk environment may accept HTTP/S(TCP Ports 80/443) requests, it may not have been configured for RDS connectivity (TCP Port 3306). Also check if it allows connectivity for your RDS instance (More info.


Note on security: The above two might help get the connection working for now, but is not a proper setting for production environment (not for development either). It's best practice to set RDS public accessibility to No/False. If you haven't planned to already, I recommend setting up a more secure DB environment by SSH Tunnelling using a Bastion Host . Be aware that this will add costs for the EC2 instance, but you can use the free tier instance for the Bastion Host for small projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM