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.

Table of Contents

Introduction

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.

What is GitLab Auto DevOps?

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.

Key Features of GitLab Auto DevOps

  • Automates CI/CD Pipelines: Whenever a modification is uploaded to the storage of data files and information systems (repo), the automated sequence of creating software packages or programs (building), checking for errors or issues (testing), and distributing or installing the code onto servers or devices (deployment) is activated.
  • Integration of Kubernetes: Auto DevOps smoothly blends with Kubernetes to quickly deploy on Kubernetes clusters.
  • Scanning for Security: It is employed to check for vulnerabilities in containers and dependencies during the security scan of the code to detect any issues at a stage.
  • Monitoring and Logging: Utilizing Prometheus and Grafana for monitoring and logging allows teams to monitor application performance effectively.
  • Environment Management: Involves overseeing settings such as staging and production; while automating the deployment process across these different environments.

Benefits of GitLab Auto DevOps:

  • Simplicity: Simplifies establishing a CI / CD pipeline for teams with experience in DevOps.
  • Consistency: Ensuring that the deployment process remains uniform across all projects helps minimize the likelihood of human mistakes.
  • Efficiency: Streamlines tasks, so developers can devote more attention to coding and less to the intricacies of deployment workflows.
  • Scalability: Easily scales with your application, whether deploying to a small internal server or a large-scale cloud environment.

What is AWS ECS?

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.

Key Features of AWS ECS

  • Managed Infrastructure: AWS handles the infrastructure behind the scenes so that you can concentrate on developing your applications.
  • Integration with AWS Services: ECS effortlessly blends with AWS services such as IAM, VPC CloudWatch, and others.
  • Flexible Deployment Options: You can use either EC or Fargate on AWS. You can handle your EC instances or let AWS take care of your infrastructure.
  • Scalability: Ensures your applications adjust automatically according to demand to maintain availability.
  • Security: AWS Secrets Manager provides security measures such as VPC network segregation, identity access management roles, and connection.

Benefits of Using AWS ECS

  • Cost Efficiency: When you choose Fargate as your computing resource option, you only pay for what you use and don’t have to worry about server management.
  • Ease of Use: It makes setting up and handling containerized applications at scale easier.
  • High Availability: It ensures your applications can be deployed in Availability Zones to maintain high availability.
  • Security and Compliance: It ensures a setting for operating containerized apps and offers choices to adhere to regulatory guidelines.

Section 2 - Creating A Simple Node Application and Dockerizing IT

Prerequisites
Before we start, ensure you have the following:

  • An AWS account with sufficient permissions to create ECS clusters and services.
  • A GitLab account and project for your Node.js application.
  • Basic knowledge of GitLab CI/CD and AWS ECS.
  • Docker is installed on your local machine for building and pushing images for testing.

Step 1: Create a node js application

Create a new directory and initialize your Node.js project:

Copy Text
mkdir autodevops-ecs
cd autodevops-ecs
npm init -y npm 
install express

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:

Copy Text
"scripts": {
  "test": "echo \"No test specified\" && exit 0"
}

Step2:Write Your Application Code

Create an app.js file with a simple Express server.

Copy Text
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}`));

Step 3: Dockerize Your Application

Create a Dockerfile:

Copy Text
FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000
CMD ["node", "app.js"]

Step 4: Build and Test Your Docker Container

Build the Docker image locally and run it:

Copy Text
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app

Verify the application is accessible at http://localhost:3000.

Section 3: Configure GitLab Auto DevOps for Your Application

Step 1: Push Application Code to GitLab

Push your code to the GitLab Repository

Step 2: Enable Auto DevOps for Your Project

NOTE: Auto DevOps will need certain environment variables to deploy to AWS ECS (which we will add later).

Step 3: Pipeline Execution

  • Auto DevOps automatically creates a pipeline for the build and test stages.
  • Access it from – GitLab project→Build → Pipelines.

Section 4: Setup ECS and Fargate for Deployment

Step 1: Log in to the AWS Management Console

Search and Navigate to the ECS

Step 2: Create a new ECS Cluster

Implement AutoDevops with AWS ECS

Step 3: Select Fargate for Infrastructure

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.

Task Definition Configuration

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.

Step 1: Create a New Task Definition

  • Go to ECS → Task Definitions → Create new task definition.
  • Step 2: Infrastructure Configuration

  • Select AWS Fargate as the launch type.
  • Select the optimum CPU and memory for the infrastructure.
  • Step 3: Container Configuration

  • Enter the name for the container, your docker application image URI located in
  • GitLab Project → Deploy → Container Registry
  • Specify the necessary memory and CPU for the container.
  • Configure port mapping for the container; in our case, the server runs on port 3000.
  • Keep everything else default and click save.
  • Task Definition Configuration

    Service Configuration

    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.

    Step 1: Create a New Service

  • Go to ECS → Your cluster → Create service
  • Step 2: Service Environment Configuration

  • Select the launch type as Farget.
  • Step 3: Deployment Configuration

  • Select Application type as service.
  • In Family, select the task definition we created previously from the drop-down menu.
  • Give your service a name and select the number of tasks we require.
  • Step 4: Networking Configuration

  • Select your default VPC and subnets.
  • Create a new security group and update inbound rules to allow traffic to your
  • service. For demo purposes, we are allowing all traffic.
  • Turn on Public IP to make containers accessible from the internet.
  • Step 5: Service is Created In Our Cluster and Deployed successfully

    Service is Created In Our Cluster and Deployed successfully

    Leverage our DevOps Consulting Services and our expertise in GitLab Auto DevOps to overcome DevOps Challenges in Integrating Node Js Applications with AWS ECS.

    Section 5: Environment Variable Setup For Auto DevOps

    GitLab Auto DevOps needs certain environment variables to deploy the application on AWS.

    Configure CI/CD variables

    Set Up AWS Credentials in GitLab:
    Go to your GitLab project.
    Navigate to Settings → CI/CD → Variables.
    Add the following variables:

    Copy Text
    ‘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.

    Section 6: Review the Auto DevOps Pipeline

    Step 1: Trigger the Pipeline

  • Push a commit to the repository.
  • It will start the Auto DevOps pipeline.
  • Step 2: Monitor Pipeline Stages

    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.

    Review the Auto DevOps Pipeline

    Step 3: Deployment to ECS

    Auto DevOps will handle the deployment to AWS ECS using Fargate:

  • It builds the Docker image from your Dockerfile.
  • Pushes the Docker image to the GitLab Container Registry.
  • Deploy the image to the ECS cluster specified in your environment variables.
  • Step 4: Updated docker image stored in Container Registry

    Auto DevOps will deploy the Docker image to the specified ECS cluster.

    Section 7: Verify The Deployment

    Step 1: Access the Application:

  • Once the pipeline completes, navigate to the AWS Management Console.
  • Go to ECS → Clusters → Your Cluster → Tasks.
  • Find the running task, and locate the public IP.
  • Verify The Deployment

    Step 2: Test the Application:

    Open the public IP along with a port in your browser to see your Node.js application running.

    Conclusion

    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.

    Do you want to streamline your Node.js deployments on AWS ECS using GitLab AutoDevOps?

    Hire an AWS Expert Today!

    Build Your Agile Team

    Hire Skilled Developer From Us

    [email protected]

    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.

    How Can We Help You?