Mastering AWS DevOps with AWS Fargate Architecture: Leveraging ECR, ECS, and Load Balancer
AWS Fargate stands out as a powerful solution for running containerized workloads without the need to manage underlying infrastructure. By leveraging Fargate in conjunction with AWS services like Elastic Container Registry (ECR), Elastic Container Service (ECS), and Load Balancer, teams can achieve a streamlined DevOps workflow that accelerates development cycles and enhances application resilience.
Introduction
Welcome to the exciting world of AWS DevOps management! If you’re a beginner AWS software engineer eager to take your first steps in the DevOps world you’re in the right place.
In this step-by-step guide, we’ll navigate the AWS services to containerize a node.js server in AWS ECR and deploy the containerized image in AWS ECS with serverless Fargate architecture auto scaled using a Load Balancer.
Why These Services?
Before we dive into deployment, let’s understand why I’ve chosen these particular AWS services.
- AWS ECR (Elastic Container Registry): Securely store, manage, and deploy Docker container images.
- AWS ECS (Elastic Container Service): Orchestrate and manage Docker containers at scale.
- AWS LOAD BALANCER that will manage and route traffic to our ECS Service Tasks.
- AWS Fargate: Run containerized applications without managing servers.
Assumptions
- A node.js server project hosted in a git repository on a service such as Bitbucket or GitHub.
- AWS account, docker and node.js with npm installed in your system.
Containerize your application in AWS ECR
- Go to AWS ECR console and click “Create repository”.
- Select private repository.
- Enter the repository name of your choice and click create.
- Dockerise your application: In order to make a docker image of your application we need to have a dockerfile inside it. Create one according to your application environment.
- From console click “Push commands”.
- Run all the push commands you will get your server’s dockerised image pushed.
Create your ECS cluster
Now lets jump into setting up the ECS. ECS has four major components as shown in the picture below.
- Container Definition: In this, we specify the image to be used. The one we pushed into the ECR.
- Task Definition: Task is nothing but a running instance of your container. In its definition, we specify the memory and number of CPUs to allocate to the task.
- Service: It serves as the environment for your tasks e.g vpc, subnets, security groups.
- Cluster: It’s a container for your ECS services, one cluster can have multiple services.
- From AWS ECS cluster console click “Create”.
- Provide your cluster name and select infrastructure as AWS Fargate.
Create your Task Definition
- Go to the Task Definitions tab and click on Create.
- Provide your task definition family name and infrastructure as AWS Fargate.
- Select launch type as AWS Fargate.
- Choose your desired operating system and system requirements.
- You can provide permissions via Task role.
- Provide your containerized image deployed in AWS ECR, the name and image uri.
- Provide your server port mappings and system requirements
This is optional step for providing your health check information.
Create your cluster service
Go to the clusters tab and select your newly created cluster and under the Services sub-tab, click on Create.
Select environment as Capacity provider strategy and rest as default.
- Select application type as Service.
- Select the task definition that you created earlier from the dropdown.
- Provide a name to your service and the number of tasks you want to be running at all times. In our case, I have given the value 2.
Specify the VPC and subnets keeping the below point in mind is that the public subnets of load balancer and private subnets of ECS tasks should be in same availability zones. Otherwise your load balancer wont be able to access the ECS tasks and your tasks would be stopping/starting continuously.
- Select Application Load Balancer from the dropdown.
- Provide the container and target group name with your health check route. Remember to put the listener to port 80 and rest keep default.
- Optionally you can also set auto scaling.
- Provide your minimum and maximum task number you want to run.
- Select your policy type and scaling threshold and click on Create.
We are all set your app should be running fine now. Hit the DNS name of your load balancer in the browser and your app should be working fine.