I am setting up an ECS task using Terraform and am encountering an error. The error is “Error: failed creating ECS Task Definition (web-2048-task): ClientException: Container.name contains invalid characters.”
Here is my task code:
resource "aws_ecs_task_definition" "aws-ecs-task" {
family = "${var.app_name}-task"
container_definitions = file("templates/task.json")
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
memory = "2048" #2 GB
cpu = "1024" #1 vCPU
execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
task_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
tags = {
Name = "${var.app_name}-ecs-td"
Environment = var.app_environment
}
}
Here is the json task definition code:
[{
"container_name": "${var.app_name}",
"name": "${var.app_name}",
"image": "${data.aws_ecr_repository.aws-ecr.repository_url}:latest",
"essential": true
},
{
"portMappings": [{
"containerPort": 80,
"hostPort": 80
}],
"cpu": 1024,
"memory": 2048,
"networkMode": "awsvpc"
}
]
I tried replacing the ${var.app_name} in the json to be the name of the app, which is ‘web-2048`, but the error changes to “Container.name should not be null or empty.” When I change the app name back to a variable, then I get the above error again.
I checked the Terraform Registry for aws_ecs_task_definition but didn’t see any info regarding container names. Same for the Aws developer guide section on task definitions but didn't find anything that helped.
Can I get some guidance on this?
CodePudding user response:
There is no "container_name"
attribute on ECS container definitions. There is only a "name"
attribute. According to the documentation:
The name of a container. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed.
I would delete the container_name
attribute, and try setting name
to something you know should be valid.
CodePudding user response:
My guess is the issue lies within the substitution for
"image": "${data.aws_ecr_repository.aws-ecr.repository_url}:latest"
You might want to echo / validate (docker pull the image with the echoed string) if the substitution works correctly such that the image is substituted with the right values.