ECS Fargate and its architecture

Matías Salinas
4 min readMar 12, 2023

--

Amazon Elastic Container Service (ECS) Fargate is a fully-managed container service that allows developers to run Docker containers without the need to manage the underlying infrastructure. It provides a highly scalable and secure platform for deploying containerized applications.

ECS Fargate Architecture:

The ECS Fargate architecture consists of several components that work together to create a highly available and scalable container platform:

  1. Amazon Elastic Container Registry (ECR): ECR is a fully-managed Docker container registry that makes it easy to store, manage, and deploy Docker container images. It provides a secure and scalable platform for storing and managing Docker images used by ECS Fargate.
  2. Amazon Elastic Container Service (ECS): ECS is a highly scalable, fully-managed container orchestration service that allows developers to run, stop, and manage Docker containers on a cluster of Amazon EC2 instances or Fargate tasks. It provides a highly available and scalable platform for deploying containerized applications.
  3. Fargate: Fargate is a serverless compute engine that allows developers to run containers without managing the underlying infrastructure. It provides a highly available and scalable platform for deploying containerized applications. Fargate tasks are similar to EC2 instances but do not require any management of underlying infrastructure.
  4. Amazon Virtual Private Cloud (VPC): VPC is a logically isolated section of the AWS cloud that allows developers to launch Amazon Web Services (AWS) resources into a virtual network that they define. ECS Fargate tasks can be launched into a VPC, which provides additional security and isolation.
  5. Elastic Load Balancing (ELB): ELB is a fully-managed load balancing service that distributes incoming application traffic across multiple targets, such as EC2 instances or Fargate tasks. ELB provides automatic scaling, high availability, and traffic management for containerized applications.
  6. AWS Identity and Access Management (IAM): IAM is a service that helps developers securely control access to AWS resources. It provides user management, access control, and auditing features that enable developers to securely manage access to their ECS Fargate resources.
  7. AWS CloudFormation: CloudFormation is a service that enables developers to define and manage AWS infrastructure as code. It provides a simple and consistent way to create, manage, and delete AWS resources in a repeatable and automated way.

Example Deployment with Terraform:

Terraform is an infrastructure as code tool that allows developers to define and manage infrastructure using a high-level configuration language. It provides a simple and consistent way to deploy AWS resources using templates that can be version controlled and automated.

Here’s an example Terraform deployment for ECS Fargate:

  1. - Define the AWS provider:
provider "aws" {
region = "us-west-2"
}

2.- Define the ECS Fargate task definition:

resource "aws_ecs_task_definition" "example" {
family = "example-task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]

container_definitions = jsonencode([{
name = "example-container"
image = "example/image:latest"
memory = 512
cpu = 256
portMappings = [{
containerPort = 80
protocol = "tcp"
}]
}])
}

3.- Define the ECS Fargate service:

resource "aws_ecs_service" "example" {
name = "example-service"
task_definition = aws_ecs_task_definition.example.arn
desired_count = 1
launch_type = "FARGATE"

network_configuration {
security_groups = [aws_security_group.example.id]
subnets = [aws_subnet.example.id]
}

load_balancer {
target_group_arn = aws_lb_target_group.example.arn
container_name = "example-container"
container_port = 80
}
}

4.- Define the security group:

resource "aws_security_group" "example" {
name_prefix = "example-sg"
}

resource "aws_security_group_rule" "example" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.example.id
}

5.- Define the subnet:

resource "aws_subnet" "example" {
cidr_block = "10.0.0.0/24"
}

resource "aws_subnet_ids" "example" {
ids = [aws_subnet.example.id]
}

6.- Define the load balancer target group:

resource "aws_lb_target_group" "example" {
name_prefix = "example-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.example.id
health_check {
interval = 30
path = "/"
port = 80
protocol = "HTTP"
timeout = 10
healthy_threshold = 2
unhealthy_threshold = 2
}
}

7.- Define the VPC:

resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}

This Terraform configuration creates an ECS Fargate task definition, an ECS Fargate service, a security group, a subnet, a load balancer target group, and a VPC.

To deploy this configuration, save it in a file named example.tf, initialize Terraform by running terraform init, and apply the configuration by running terraform apply. This will create the necessary resources in your AWS account.

In conclusion, ECS Fargate is a powerful service that simplifies the deployment of containerized applications in the cloud. By using Terraform, developers can easily automate the deployment of ECS Fargate resources, making it easier to manage and scale containerized applications.

--

--

Matías Salinas
Matías Salinas

No responses yet