<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Integration Ninjas</title>
    <description>The latest articles on DEV Community by Integration Ninjas (@integration_ninjas).</description>
    <link>https://dev.to/integration_ninjas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2801264%2F525168c6-2944-4f16-94ca-ebd4be3cb0a8.jpg</url>
      <title>DEV Community: Integration Ninjas</title>
      <link>https://dev.to/integration_ninjas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/integration_ninjas"/>
    <language>en</language>
    <item>
      <title>Deploy Node.js Application to AWS EC2 using GitLab CI</title>
      <dc:creator>Integration Ninjas</dc:creator>
      <pubDate>Fri, 14 Feb 2025 10:22:23 +0000</pubDate>
      <link>https://dev.to/integration_ninjas/deploy-nodejs-application-to-aws-ec2-using-gitlab-ci-54l9</link>
      <guid>https://dev.to/integration_ninjas/deploy-nodejs-application-to-aws-ec2-using-gitlab-ci-54l9</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/hmuitw61-2g"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Automating deployments with GitLab CI/CD is a powerful way to streamline your development workflow. In this guide, we'll walk through the process of deploying a Node.js application to an AWS EC2 instance using GitLab CI/CD and Docker. This setup ensures that every new commit is automatically built, pushed to a container registry, and deployed to your server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js application:&lt;/strong&gt; Ensure you have a Node.js application ready for deployment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitLab repository:&lt;/strong&gt; Create or use an existing repository to host your application code.(Example Repo)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker installed:&lt;/strong&gt; Install Docker locally(if running self-hosted runner) to build and manage container images. If you're running workflow with GitHub hosted runners then Docker is already installed there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS account:&lt;/strong&gt; Sign up for a free AWS account if you haven't already.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EC2 instance:&lt;/strong&gt; Create an EC2 instance in AWS with appropriate specifications for your application. Configure security groups to allow inbound traffic on the port your application uses (e.g., port 3000).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSH Client:&lt;/strong&gt; Install an SSH client like PuTTY or Terminal (on macOS/Linux) to connect to your EC2 instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  AWS EC2 Setup
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use below commands to setup Docker:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;docker.io &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start docker
&lt;span class="nb"&gt;sudo &lt;/span&gt;docker run hello-world
docker ps
&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;666 /var/run/docker.sock
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;docker
docker &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use below reference to setup self-hosted runner on EC2: &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://docs.gitlab.com/runner/install/linux-manually.html" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.gitlab.com%2Fassets%2Fimages%2Fgitlab-logo-header.svg" height="24" class="m-0" width="111"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://docs.gitlab.com/runner/install/linux-manually.html" rel="noopener noreferrer" class="c-link"&gt;
          Install GitLab Runner manually on GNU/Linux | GitLab
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          GitLab product documentation.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.gitlab.com%2Ffavicon.ico%3Fv%3D2" width="800" height="400"&gt;
        docs.gitlab.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Once you create runner, use this reference to register the GitLab runner on that EC2 machine.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Flow Chart
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmb6f5w67bw314493on4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmb6f5w67bw314493on4n.png" alt="CI/CD Workflow" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, follow below steps to create required Docker file and .gitlab-ci.yml file to deploy you Node.js code on AWS EC2:&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Dockerizing the Node.js Application
&lt;/h4&gt;

&lt;p&gt;First, let's create a Dockerfile to containerize our application:&lt;br&gt;
Within your application directory, create a file named &lt;code&gt;Dockerfile&lt;/code&gt; with the following content, replacing &lt;code&gt;&amp;lt;IMAGE_NAME&amp;gt;&lt;/code&gt; with your desired image name and &lt;code&gt;&amp;lt;PORT&amp;gt;&lt;/code&gt; with your application's port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:20-alpine3.18
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","start"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Dockerfile:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses Node.js 20 on Alpine Linux for a lightweight image&lt;/li&gt;
&lt;li&gt;Sets the working directory to &lt;code&gt;/app&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Installs dependencies&lt;/li&gt;
&lt;li&gt;Copies the application files&lt;/li&gt;
&lt;li&gt;Exposes port 5000 (adjust if needed)&lt;/li&gt;
&lt;li&gt;Runs the application using &lt;code&gt;npm run start&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Build and test your Docker image locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t nodejs-app .
docker run -p 5000:5000 nodejs-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything works, move to the next step!&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Setting Up GitLab CI/CD Pipeline
&lt;/h4&gt;

&lt;p&gt;To automate deployment, create a .gitlab-ci.yml file in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default:
  image: docker:24.0.5
  services:
    - docker:24.0.5-dind
  before_script:
    - docker info

stages:
  - build
  - deploy

build_job:
  stage: build
  script: |
    echo "Building the application.."
    docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
    docker build -t integrationninjas/nodejs-app:$CI_COMMIT_SHA .
    docker push integrationninjas/nodejs-app:$CI_COMMIT_SHA

deploy_job:
  stage: deploy
  script: |
    docker pull integrationninjas/nodejs-app:$CI_COMMIT_SHA
    docker rm -f nodejs-app-container
    docker run -d -p 5000:5000 --name nodejs-app-container integrationninjas/nodejs-app:$CI_COMMIT_SHA
  tags:
    - ec2-runner
  only:
    - deploy-to-ec2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Understanding the Pipeline&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Default Section:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses Docker-in-Docker (dind) to enable Docker builds inside the GitLab Runner.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stages Section:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines two stages – &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;deploy&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;build_job&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logs into Docker Hub.&lt;/li&gt;
&lt;li&gt;Builds the Docker image with the commit SHA as the tag.&lt;/li&gt;
&lt;li&gt;Pushes the image to Docker Hub.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;deploy_job&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Pulls the latest image from Docker Hub.&lt;/li&gt;
&lt;li&gt;  Removes the old container (if running).&lt;/li&gt;
&lt;li&gt;  Runs the new container on port 5000.&lt;/li&gt;
&lt;li&gt;  Uses the GitLab Runner tag &lt;code&gt;ec2-runner&lt;/code&gt; to execute on an AWS EC2 instance.&lt;/li&gt;
&lt;li&gt;  Triggers only when pushed to the &lt;code&gt;deploy-to-ec2&lt;/code&gt; branch.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, when you push changes to the deploy-to-ec2 branch, GitLab CI/CD will automatically build and deploy your app! 🚀&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Testing the Deployment
&lt;/h4&gt;

&lt;p&gt;Once the pipeline completes, check if your app is running on EC2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl http://your-ec2-ip:5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see your application response! 🎉&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With this setup, you've successfully automated the deployment of a Node.js application to AWS EC2 using GitLab CI/CD and Docker. Now, every commit to the deploy-to-ec2 branch will trigger an automatic build and deployment process. This approach ensures:&lt;/p&gt;

&lt;p&gt;✅ Faster and reliable deployments&lt;br&gt;
✅ Consistency across environments&lt;br&gt;
✅ Less manual effort, thanks to automation.&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Questions?&lt;/strong&gt; Drop them below! 🚀&lt;/p&gt;

</description>
      <category>aws</category>
      <category>node</category>
      <category>gitlab</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
