Day 24,25: Deploying Node.js Application on AWS EC2 with Jenkins CI/CD Pipeline

Day 24,25: Deploying Node.js Application on AWS EC2 with Jenkins CI/CD Pipeline

ยท

5 min read

This is #90DaysofDevops challenge under the guidance of Shubham Londhe sir.

  • In Days 24 and 25 of our DevOps journey, we tackle the exciting task of deploying a Node.js application on an AWS EC2 instance while establishing a robust CI/CD pipeline using Jenkins.

Tools Used:

  1. AWS EC2

  2. GitHub

  3. Docker

  4. Jenkins

Understanding CI/CD Pipeline:

  • CI/CD (Continuous Integration/Continuous Deployment) pipeline automates the integration, testing, and deployment processes, ensuring smooth delivery of changes to production.

    Task 01:

Step-by-Step Instructions:

  • Set Up AWS EC2 Instance:

    • Launch an EC2 instance on AWS, ensuring it meets the requirements for your Node.js application.

  1. Install Jenkins on EC2:
  • Follow Official Document for installation instructions for Jenkins on your EC2 instance. Ensure Jenkins is up and running.

    https://www.jenkins.io/doc/book/installing/linux/

  • After installation, Jenkins is accessible on port 8080.

  • Before beginning the Jenkins configurations, we need to add the public key. This enables us to establish a connection between Jenkins and GitHub, allowing access to the source code from the GitHub repository.

    1. Generate SSH Keys:
  • Create SSH keys on your EC2 instance using ssh-keygen.

    • To grant access, add the public key to your GitHub repository.

    • Go to GitHub -> setting -> SSH and GPG key -> Add new key -> Use the command cat id_rsa_pub in the EC2 instance to display the key -> Assign a name to the key -> Paste the copied key.

  1. Configure Jenkins:
  • Set up a new item in Jenkins for your project. Include the GitHub repository URL and credentials to access it.

  • We will now add the credentials, allowing Jenkins to access the code from GitHub.

  • In the Source Core Management section, navigate to 'Add credentials'.

  • Click on Add -> then save it, Afterward, click on build now.

  • Build Successful.

  • Navigate to your instance and verify if the repository has been cloned there.

  • Next, review the README file and install any required dependencies.

  • After executing all these commands, check if you can access the URL. If it's not accessible, it might be because we haven't granted access to port 8000.

Navigate to your instance -> Security -> Edit inbound rules -> Add a new rule and save your changes.

  • Now, take the public IP of the instance along with port 8000, and you will be able to access the application.

5. Containerize Node.js App:

  • We will now dockerize the application, making it accessible to anyone, anywhere.

  • Navigate to your instance and install Docker.

sudo apt install docker.io

  • Create a Dockerfile for your project.
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]
  • Create a Docker image for your Node.js application using the Dockerfile.
sudo docker build . -t todo-node-app

  • Successfully created the image (todo-node-app:latest).

  • Now, run the Docker container to ensure that the application functions as expected.

  •   docker run -d --name node-todo-app -p 8000:8000 todo-node-app
    

  • Try accessing it using the public IP address and port 8000.

  • It is accessible.

    1. Implement CI/CD Pipeline:
  • Integrate Docker commands into Jenkins pipeline scripts for automation.

  • Go to Jenkins job < Build steps< Execute shell.

  • Build is successful. You can access it in your browser.

  • Now, all the processes that were previously done manually will be executed automatically, thanks to our automation setup.
  • Configure Webhooks:

    • Set up GitHub Webhooks: Configure GitHub webhooks to automatically trigger Jenkins builds whenever there are changes in the repository.

    • Terminate the Current Container: Before proceeding, ensure to stop the current container.

    • Navigate to Jenkins > Manage Jenkins > Manage Plugins, then install the GitHub Integration plugin.

    • Go to repository settings -> webhook -> Add webhook -> Payload URL add Jenkins URL here -> (http://3.137.151.160:8080/github-webhook/) -> Content type -> application. Json -> Add webhook.

      • The webhook has been successfully added.

      • Go to Jenkins -> configure -> check the box

  • Simply make an update in the repository, and your job will be triggered automatically.

    • The build was successful.

Task 2:

  • Ensure that you have Docker Compose installed on your system.

  • Let's take a look at how the docker-compose.yaml file should be structured:

version: '3.9'

services:
  web:
    image: "trainwithshubham/node-app-test-new:latest"
    ports:
      - "8000:8000"
  • Now, in Jenkins, navigate to Dashboard > Configure > Build Steps, and include the following commands:

Caution: Before proceeding with the build, it's crucial to delete the previous containers. You can achieve this by using the docker kill command.

docker-compose down
docker-compose up --build -d

  • Save and build. You'll notice that the build was successful.

  • Try accessing the application.

  • Hooray! My application is now up and running.

  • We can confirm that the container has been created by using the 'docker ps' command.

Conclusion:

Deploying a Node.js app with Jenkins CI/CD on AWS enhances development efficiency. Docker and GitHub integration streamline testing and deployment. Task-02's Docker Compose simplifies multi-service setups. Embrace DevOps principles for continuous improvement. Let's innovate, collaborate, and optimize for a smoother development journey ahead.

Feel free to ask any questions in the comments section. I'm here to help!

If you found this post useful, please consider following and giving it a thumbs up ๐Ÿ‘ to show your appreciation ๐Ÿ˜„.

Thank you for reading and your support ๐Ÿ’š

Did you find this article valuable?

Support Akash Dhengale by becoming a sponsor. Any amount is appreciated!

ย