Quick Summary:
This blog will walk you through a stepwise procedure for automatically deploying a Node.js application to Amazon Web Services (AWS) Elastic Container Service (ECS) using GitLab Auto DevOps, from setting up the initial environment to integrating AWS with Auto DevOps from GitLab. We will also focus on what exactly Auto DevOps in GitLab brings and what features AWS ECS has.
Although it seems basic, deploying applications to the cloud can be cumbersome. However, specific tools such as GitLab Auto DevOps and AWS ECS do much of the heavy lifting by automating the majority of processes. The CI/CD pipelines provided by Auto DevOps automate the process of building, testing, and deploying the applications.
This blog will teach us to deploy a simple Node JS application to AWS ECS utilizing GitLab Auto DevOps. Although most of the processes are automated due to Auto DevOps, it is possible to customize behaviors by defining or adding more environment variables or by adding an a.gitlab-ci.yml file to which custom steps or a set of steps can be added to modify the existing flows further.
GitLab Auto DevOps comprises a set of configured functionalities and connections that collaborate to streamline the software development process efficiently by automatically identifying your programming language and establishing a CI / CD pipeline for constructing and verifying your application. You can easily deploy to staging and production and preview changes per branch. With default settings, you can ship your apps quickly and customize later. GitLab Auto DevOps also offers API management for more advanced control.
Amazon Elastic Container Service (ECS) is a service for managing containers that enables companies to deploy and scale containerized applications effortlessly in cloud environments or on their own premises effectively integrated with AWS, ECS offers a smooth experience for running container workloads with solid security measures It offers multiple deployment choices such, as Amazon EC2 AWS Fargate and Amazon ECS Anywhere to cater to different infrastructure requirements seamlessly and flexibly.
Prerequisites
Before we start, ensure you have the following:
Create a new directory and initialize your Node.js project:
NOTE: Make sure you have updated the exit condition below in your package.json file (it will be required to pass through the test cases job in our pipeline as we haven’t defined explicit test cases for our pipeline).
Update the package.json file to ensure test cases pass in the pipeline:
Create an app.js file with a simple Express server.
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => res.send('Hello, AWS ECS with GitLab Auto DevOps!')); app.listen(port, () => console.log(`App running on port ${port}`));
Create a Dockerfile:
FROM node:14 WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "app.js"]
Build the Docker image locally and run it:
Verify the application is accessible at http://localhost:3000.
Push your code to the GitLab Repository
NOTE: Auto DevOps will need certain environment variables to deploy to AWS ECS (which we will add later).
Search and Navigate to the ECS
Using Fargate over EC2 simplifies our deployment by removing the need to manage EC2 instances. Fargate handles the infrastructure, letting us focus on deploying and running containers without worrying about server upkeep.
Fargate also ensures cost efficiency and scalability by automatically allocating the right resources. Thus, we only pay for what we use, allowing our application to scale quickly with demand.
Creating a task definition in ECS is crucial as it defines the blueprint for your containerized application. It specifies the Docker image, resource allocation, and environment settings to run your containers efficiently.
Task definitions also streamline management by allowing updates to your container configurations without disrupting your service. This flexibility ensures your application runs smoothly as you iterate and deploy new versions.
Configuring a service in ECS ensures your containers run continuously and reliably. The service manages the deployment of your tasks, maintaining the desired number of running instances.
Service configuration also enables automated scaling and load balancing, allowing your application to adapt to traffic fluctuations seamlessly. This setup guarantees high availability and efficient resource usage in your deployment.
Leverage our DevOps Consulting Services and our expertise in GitLab Auto DevOps to overcome DevOps Challenges in Integrating Node Js Applications with AWS ECS.
GitLab Auto DevOps needs certain environment variables to deploy the application on AWS.
Set Up AWS Credentials in GitLab:
Go to your GitLab project.
Navigate to Settings → CI/CD → Variables.
Add the following variables:
‘AWS_ACCESS_KEY_ID’ ‘AWS_SECRET_ACCESS_KEY’ ‘AWS_DEFAULT_REGION’ (e.g., ‘us-west-2’). ‘AUTO_DEVOPS_PLATFORM_TARGET’: `ECS` ‘AUTO_DEVOPS_AWS_ECS_CLUSTER‘ Your ECS cluster name. ‘AUTO_DEVOPS_AWS_ECS_SERVICE‘ Your ECS service name. ‘CI_AWS_ECS_CLUSTER’ Set this to your AWS ECS cluster name. ‘CI_AWS_ECS_SERVICE’ Set this to your AWS ECS service name. ‘CI_AWS_ECS_TASK_DEFINITION’ Set this to your AWS ECS task name.
Go to the GitLab project
Navigate to CI/CD → Pipelines to view the running pipeline.
The pipeline progresses through stages: build, test, code quality, and deploy.
Auto DevOps will handle the deployment to AWS ECS using Fargate:
Auto DevOps will deploy the Docker image to the specified ECS cluster.
Open the public IP along with a port in your browser to see your Node.js application running.
Deploying a Node.js application to AWS ECS using GitLab Auto DevOps simplifies the CI/CD process, leveraging automation to reduce manual effort and increase efficiency. Following these steps, you can achieve a scalable and robust deployment process, harnessing the full power of AWS and GitLab’s automated capabilities.
If you need any help with GitLab Auto DevOps for seamless integration, you can always Hire DevOps Developers from Bacancy.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.