Deploy a Node.js App with CI/CD and AWS
By: Gugatharsan Sivalingam
Tags: Continuous Integration, Web Development, Nodejs, AWS, Programming

Photo from ITNEXT

I know how difficult is to build and deploy your applications manually. Deployment is really a developer’s nightmare. In this tutorial, I’ll show you how to deploy the NodeJS application with a single command. All of the processes will be handled by AWS CodePipeline and AWS Elastic Beanstalk. Before diving deep into the application you need to have the following:

· AWS Account

· NPM installed

· GitHub account

First, let’s create a simple NodeJS application that’ll return simple JSON data. Open your VS Code terminal and type the following command:

$ npm init

npm init

After typing that command it’ll ask for the above fields to fill up. After successfully filling these values package.json will be created inside that folder. Then we need to install express and cors dependencies for this application. To install these dependencies use the following command:

$ npm i express cors

Express is a back end web application framework for NodeJS. It’s a standard server framework for NodeJS. CORS is really useful when you’re offering a public API and would like to control access to certain resources and how people use them. Also, if you want to use your own API or files on a different web page you can simply configure CORS to allow that, while still blocking others out.

package.json

After installing these packages you can check inside the package.json whether they are installed correctly or not. Inside the “scripts” add “start”: “node server” to initialize the node server. Then inside this folder create a file called server.js to write server configurations. Add the following code segment inside the server.js

In the above code segment, I have imported express framework and cors. Here I’m sending “API Running” JSON message to the base URL that is http://localhost:5000. Below code, segment defines the PORT and app.listen will use that port to initialize the server. Now start the server using the following command:

$ npm start server

server running

Now, this application will run on port 5000 and you can see that JSON message if you visit that URL. Currently, everything runs locally because of this only you can access this URL. If you want everyone to access this URL, you need to deploy this application.

To deploy this application you need to push the code to the GitHub repository. Before pushing your code to the repository, don’t forget to ignore the node_modules using “.gitignore”.

.gitignore file

It’s time to push the code to your repository. Create an empty repository in the GitHub.

Sample GitHub Repo

Then use the following commands in VS Code terminal to deploy your code in the repository you have created.

$ git init -> to initialize your git repository $ git add . -> adds a change in the working directory to the staging area $ git commit –m “your message” -> used to save your changes to the local repository $ git branch –m main -> rename local branch $ git remote add origin "Your_Repo_URL"-> to add remote repository $ git push -u origin main -> Push changes to remote repository.

after pushing your code

Now you can check inside your repo that everything is there. After successfully pushing your code to GitHub, it’s time to configure the AWS Elastic Beanstalk to create a node environment to deploy your application. Through the AWS management console go to the EBS service.

AWS Elastic Beanstalk

Click on the create application.

EBS Environment creation

First, give your application a name. Since we are deploying the NodeJS application, set the platform as Node.js and change other settings based on your node application version. Here I keep everything as default and create an application with AWS sample code. It’ll take some time to create the environment until then let’s go to CodePipeline service to create a pipeline for this application.

CodePipeline

Click on the create pipeline to create a new pipeline.

pipeline settings

Give a name to your pipeline and click next.

Add source stage

From the source provider select GitHub and connect it to this console. Then select the repository you want to deploy and the branch of it. After configuring these click the next button. Then it’ll ask for add build stage. Since we don’t use any of the builds we can skip this step by clicking on the skip build stage.

Add build stage

Now you need to add where you are going to deploy this code. Before this, you need to confirm that your EBS environment is ready to deploy your application.

Your EBS Environment

If it creates your environment successfully you can see the health in a green tick. If you click that URL in the upper right corner it’ll redirect to AWSsample nodejs application. now go to your CodePipeline console to deploy your application.

Add deploy stage

Note — Remember in which region you are creating your applications.

After filling up these fields select the next button.

Review Page

Now it’ll redirect to the review page where you can see all your configurations. Now click on the Create pipeline button. It’ll take some time to deploy your application inside the EBS.

Your CodePipeline

Now your application was deployed successfully to your EBS.

Your EBS Environment with Your Application

You can test this application by click on the link above or using postman.

Postman Test

Conclusion

See how easily we can deploy our applications in the AWS. Now you don’t need to worry about the deployment of your applications. Every time you made a change in your code and push it to your repository, this pipeline automatically detects the change and deploys your application again. I hope everyone enjoyed this article. I hope to see you guys again with another good topic.

Thank You.

The code repository I used in this demo: