I'm trying to create an ECS service using Terraform. I have some modules defined to create some necessary resources (like the alb, vpc, subnets, etc). All of those have been created successfully, but the aws_ecs_service
is not being created.
This is the Terraform code I'm using:
terraform {
required_version = ">= 0.13"
}
resource "aws_ecs_task_definition" "main" {
family = "task-definition"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.fargate_cpu
memory = var.fargate_memory
container_definitions = jsonencode([
{
name = "container-definition"
image = var.container_image
cpu = var.fargate_cpu
memory = var.fargate_memory
command = ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
port_mappings = [
{
container_port = var.app_port
host_port = var.app_port
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "/ecs/task-definition"
awslogs-region = var.aws_region
awslogs-stream-prefix = "ecs"
}
}
}
])
}
module "load_balancer" {
source = "../alb"
vpc_id = var.vpc_id
app_port = var.app_port
public_subnets_ids = var.public_subnets_ids
health_check_path = "/"
}
resource "aws_ecs_service" "main" {
name = "testing-service"
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.main.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
security_groups = [module.load_balancer.sg_id]
subnets = var.private_subnet_ids
assign_public_ip = true
}
load_balancer {
target_group_arn = module.load_balancer.alb_tg_arn
container_name = "container-definition"
container_port = var.app_port
}
depends_on = [
module.load_balancer
]
}
I'm fully aware that fragment of code is not enough to reproduce the problem, but I have not been able to make a smaller example reproducing the problem. If you need the rest of the files, I can create a public repo or something like with the rest of the code.
The error I'm getting is:
╷
│ Error: error creating testing-service service: error waiting for ECS service (testing-service) creation: InvalidParameterException: The container container-definition did not have a container port 8000 defined.
│
│ with module.service.aws_ecs_service.main,
│ on service/main.tf line 47, in resource "aws_ecs_service" "main":
│ 47: resource "aws_ecs_service" "main"
Update
Taking a look at the generated resources, I have seen that the port mapping has not been generated! Even though I have it specified in the terraform code:
That's a screenshot from the task definition created by that code.
8000
in your python command, and usedvar.app_port
elsewhere. Do they match ?