Connect a Node Application to an RDS Instance and Containerize with Docker
Container technology can be very useful for creating complex applications because it compartmentalizes tasks and prevents compatibility and security issues. However, developing and deploying containers across teams can require a significant amount of extra effort and slow-down the CI/CD pipeline. To help with this, Amazon provides two services: Elastic Container Registry (ECR) and Elastic Container Services (ECS).
Part 1 of this tutorial will demonstrate how to build a basic Dockerized web-application that displays JSON data retrieved from an AWS Relational Database Service (RDS). Part 2 will demonstrate how to upload the docker image to AWS Elastic Container Services and deploy that image using an AWS Elastic Container Services Cluster with Fargate.
Step 0: Install AWS CLI and Docker
To accomplish the tasks detailed in this walkthrough, your environment must have nodejs, npm and express-generator installed. In addition, it must also have the AWS CLI installed and logged into an IAM role with the appropriate permissions.
This tutorial will use a fresh Ubuntu 18.04 EC2 instances with the AWS CLI pre-installed. If you do the same, you can set up your environment with the following script. If your using a different OS, check out the following installation instructions @ node/npm, express-generator, docker and Awscli.
Run This Script to Install Software on Ubuntu 18.04
#!/bin/bashsudo apt-get update
sudo apt-get install node -y
sudo apt-get install npm -y
sudo npm install -g express-generator
sudo apt-get updatesudo apt install apt-transport-https ca-certificates curl software-properties-common -ycurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository \
“deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable”sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io -ysudo apt install mysql-client-core-5.7
Finally, the IAM role must be set with the proper permission. To do this, navigate to the AWS IAM tab and create a new user. Give the user access and save the Access key and Secret Access key. Now, in the terminal, use the command
aws configure
And paste the keys when prompted along with your desired region (I will be using us-east-1). Test the connection by running the command:
aws ec2 describe instances
If your environment is signed in, it will display information about all ec2 instances associated with your account.
Step 1: Create an RDS MySQL instance
To begin, create a free tier MySQL rds instance in AWS with an easy to remember name and password. Next, on your machine connect to your database using the following command:
mysql -h $YOUR_ENDPOINT -P 3306 -u $USERNAME -p
Enter your password. Note: if you are having connectivity issues, it could be your firewall. If you are unable to modify this, I would recommend using an EC2 instance within the same availability zone as your database.
Next, create a Database with a table with the following commands:
CREATE DATABASE walkthrough;USE walkthroughcreate table contacts ( name varchar(100), address varchar(100), email varchar(100), phone varchar(100));INSERT INTO contacts (name, address, email, phone) VALUES (“Alberta”, “123 Alberta Street”, “alberta@alberta.com”, “(999) 999–9999”);INSERT INTO contacts (name, address, email, phone) VALUES (“Brandon”, “123 Brandon Street”, “Brandon@Brandon.com”, “(999) 999–9999”);INSERT INTO contacts (name, address, email, phone) VALUES (“Carla”, “123 Carla Street”, “Carla@Carla.com”, “(999) 999–9999”);INSERT INTO contacts (name, address, email, phone) VALUES (“Derrick”, “123 Derrick Street”, “Derrick@Derrick.com”, “(999) 999–9999”);
Make sure it is working with the following command:
SELECT * from contacts;
Step 2: Create a NODE App and connect it to RDS
With the database set up, the next step is to create the application. To do this, navigate to the home directory of your machine and run the following command:
express ecr-walkthrough
This will generate the skeleton of a node application. Navigate to the newly created ‘ecr-walkthrough’ directory and set up the application by running:
npm install
This will install the plug-ins needed to run that application. Test the application by running:
npm run start
and navigating to localhost:3000 (Or $YOUR_EC2_PUBLICIP:3000) after running the command. If the basic express page is displaying, then everything is working. Next, the application will need to connect to the database, so exit the server and install MySQL with the following command:
npm install --save mysql
Now, the application will need to access the database. To do this open routes/index.js and replace the contents of routes/index.js with the code below. Remember to replace the endpoint, username, password, database, and table.
var express = require(‘express’);
var router = express.Router();
var mysql = require(‘mysql’);
var data;
var my_database = mysql.createConnection({
host: ‘$YOUR_DATABASE ENDPOINT’,
port : ‘3306’,
user : ‘$YOUR_USERNAME’,
password: ‘$YOUR_PASSWORD’,
database: ‘$YOUR_DATABASE’
});my_database.connect(function(err){
if (err) throw err;
console.log(“connected”);
my_database.query(“SELECT * from $YOUR_TABLE”, function(err, result, fields){
if (err) throw err;
data = result;
});
});
router.get(‘/’, function(req, res, next){
res.send(JSON.stringify(data));
});
module.exports = router;
This will call the database and display every item in JSON format. Make sure everything is working by running:
npm start run
It should display something like the following:
Close down the server with CTRL +C. *Note: sometimes node servers hang and hold up the port (particularly if you are starting and stopping it multiple times). If this occurs, use the command ps aux | grep node. If more than one service is running ‘kill $ProcessID’ .
Step 3: Containerize the Application with Docker
Now that the web application has been built, it is time to place it in a container. There are many ways to do this. Ideally, it is best to build a container from scratch using a base alpine linux image. This minimizes security concerns and memory usage. But, for the purposes of this walkthrough, a Dockerfile calling the official NODE.js image is fine.
The Dockerfile must open the image, copy the contents of the application into the container, run npm install, and start the application (so it is running when the container is deployed). To do this, navigate to the app’s directory (ecr-walkthrough) and create a Dockerfile:
touch Dockerfile
And add the following code:
FROM node:latest
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY — chown=node:node . .
EXPOSE 3000
CMD npm start
This will copy your application files into a docker image, give the correct permissions and start the server in a container. Build the image with the following command.
docker build -t ecr-test .
This will download the latest official docker node image (if it is not already on your computer) and create the new container. Check your that your image is there with the following
docker images
Finally, check that the image works by creating a container with the following commands
docker run -dit — name local-test -p 3000:3000 ecr-test
This should create the container called local-test. Check that is running properly with the command:
docker ps -a
And, confirm that the image is working properly by going to localhost:3000 (or $YOUR_EC2_IP:3000) and check that the page is displaying properly.
Note: If you need to make changes to the container directly, access the container with the command:
docker exec -it ecr-test bash
After making modifications, the container image must be recreated with
docker commit $CONTAINER_NAME $IMAGE_NAME
Conclusion
This tutorial covered how to connect a Node Application to an RDS Instance and then place it in a docker container. Check out Part 2 to learn about using AWS Elastic Container Registry to manage container development and AWS Elastic Container Services Clusters to deploy docker containers.