ECS
In this section you will create the Elastic Container Service to run your application.
ECS Cluster
First we need to create the cluster
- Create a
resource
block with typeaws_ecs_cluster
namedthis
with following parameters:name
=var.service_name
capacity_providers
=["FARGATE"]
ECS Task Definition
ECS Task Definition is a collection of containers that run in a service. It has all the container configuration parameters like CPU, Memory, Networking etc.
- Create a
resource
block with typeaws_ecs_task_definition
namedthis
with following parameters:family
=var.service_name
required_compatibilities
=["FARGATE"]
network_mode
="awsvpc"
cpu
="256"
memory
="512"
execution_role_arn
=aws_iam_role.this.arn
- Container Definition:
container_definitions = jsonencode([ { name = "frontend" image = "<your application image path>" cpu = 10 memory = 512 essential = true portMappings = [ { containerPort = 80 hostPort = 80 } ] } ])
Help
terraform/ecs.tf
resource "aws_ecs_task_definition" "this" {
family = var.service_name
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.this.arn
container_definitions = jsonencode([
{
name = "frontend"
image = "nginxdemos/hello"
cpu = 10
memory = 512
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
}
ECS Service
ECS Service is what bundles everything together in ECS.
- Create a
resource
block with typeaws_ecs_service
namedthis
with following parametersname
=var.service_name
cluster
=aws_ecs_cluster.this.id
task_definition
=aws_ecs_task_definition.this.id
desired_count
= 1launch_type
="FARGATE"
- Network Configuration
network_configuration { subnets = data.aws_subnet_ids.private.ids security_groups = [aws_security_group.ecs_tasks.id] }
- Load Balancer:
load_balancer { target_group_arn = aws_alb_target_group.frontend.arn container_name = "frontend" container_port = 80 }
- Depends On:
depends_on = [ aws_alb_listener.frontend, ]
Output
For us to know where we can check our deployment we need to tell Terraform to output the ALB DNS address
- Create a
output
block namedalb_address
with the following parametersvalue
=aws_alb.this.dns_name
Push everything to production
Now you are ready to apply everything to ECS.
terraform apply
It will show you the resources that Terraform will create,delete or change.
Check that every resource looks OK, and write yes
to apply the changes.
Answer
terraform/ecs.tf
resource "aws_ecs_cluster" "this" {
name = var.service_name
capacity_providers = ["FARGATE"]
}
resource "aws_ecs_task_definition" "this" {
family = var.service_name
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.this.arn
container_definitions = jsonencode([
{
name = "frontend"
image = "<your application image path>"
cpu = 10
memory = 512
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
}
resource "aws_ecs_service" "this" {
name = var.service_name
cluster = aws_ecs_cluster.this.id
task_definition = aws_ecs_task_definition.this.id
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = data.aws_subnet_ids.private.ids
security_groups = [aws_security_group.ecs_tasks.id]
}
load_balancer {
target_group_arn = aws_alb_target_group.frontend.arn
container_name = "frontend"
container_port = 80
}
depends_on = [
aws_alb_listener.frontend,
]
}
output "alb_address" {
value = aws_alb.this.dns_name
}