Terraform v0.12.5
data "template_file" "task_definition__backend" {
template = file("${path.module}/task_definitions/backend.json")
vars = {
image_url = "1111111111111111.dkr.ecr.us-east-1.amazonaws.com/my-repo-here/backend:${var.version_tag}"
container_name = "backend"
log_group_region = data.aws_region.current.name
log_group_name = aws_cloudwatch_log_group.app.name
}
}
data "template_file" "task_definition__frontend" {
template = file("${path.module}/task_definitions/frontend.json")
vars = {
image_url = "1111111111111111.dkr.ecr.us-east-1.amazonaws.com/my-repo-here/frontend:${var.version_tag}"
container_name = "frontend"
log_group_region = data.aws_region.current.name
log_group_name = aws_cloudwatch_log_group.app.name
}
}
resource "aws_ecs_task_definition" "backend" {
family = local.ecs_cluster_name
container_definitions = data.template_file.task_definition__backend.rendered
network_mode = "awsvpc"
}
resource "aws_ecs_task_definition" "frontend" {
family = local.ecs_cluster_name
container_definitions = data.template_file.task_definition__frontend.rendered
network_mode = "awsvpc"
}
resource "aws_ecs_service" "backend" {
name = "${local.ecs_cluster_name}_backend"
cluster = aws_ecs_cluster.ecs_cluster.id
task_definition = aws_ecs_task_definition.backend.arn
desired_count = "1"
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 300
network_configuration {
subnets = aws_subnet.private_subnet.*.id
security_groups = [
aws_security_group.sg_for_ec2_instances.id]
}
load_balancer {
# Register the ECS service within the ALB target group
# This makes the service participate in health checks
# and receive traffic when healthy
target_group_arn = aws_alb_target_group.target_group_backend.arn
container_name = "backend"
container_port = "80"
}
service_registries {
registry_arn = aws_service_discovery_service.service_discovery.arn
container_name = "backend"
container_port = 80
}
depends_on = [
aws_alb_listener.http_traffic,
]
}
resource "aws_ecs_service" "frontend" {
name = "${local.ecs_cluster_name}_frontend"
cluster = aws_ecs_cluster.ecs_cluster.id
task_definition = aws_ecs_task_definition.frontend.arn
desired_count = "2"
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 300
network_configuration {
subnets = aws_subnet.private_subnet.*.id
security_groups = [
aws_security_group.sg_for_ec2_instances.id]
}
load_balancer {
target_group_arn = aws_alb_target_group.target_group_frontend.arn
container_name = "frontend"
container_port = "80"
}
service_registries {
registry_arn = aws_service_discovery_service.service_discovery.arn
container_name = "frontend"
container_port = 80
}
depends_on = [
aws_alb_listener.http_traffic,
aws_ecs_service.backend,
]
}
Running terraform apply again and again should not cause any errors. I expect that AWS task definitions get updated properly.
AWS task definitions don't get updated and an error is thrown approximately 1 out of 5 attempts. If I rerun terraform apply another time, it usually works.
Error: ClientException: Too many concurrent attempts to create a new revision of the specified family.
status code: 400, request id: efce29cc-a021-4d6b-b603-d84c8b7a91fa
terraform applyNothing special. Just two ECS services and the corresponding task definitions for them. It's worth noting that they are both within the same "family". Maybe this has some impact?
I'm running into the same issue. I reduced the Terraform configuration to make it easier to reproduce it (the left out facts are the same as in the initial post):
Terraform v0.12.7
+ provider.aws v2.26.0
resource "aws_ecs_task_definition" "this" {
count = 2
family = "test-family"
container_definitions = jsonencode([{
name = "test"
image = "dummy"
memory = 512
}])
}
The error can be circumvented by running terraform apply -parallelism=1, but this slows down the execution time up to factor 10 compared to the default parallelism.
When you set count = 1 it applies without errors, but of course only generates a single resource.
I had similar issue. I was able to fix it by using different family for each task definition.
Using for example for_each on a map instead of count, then
family = "local.ecs_cluster_name-${each.key}"
You should have two task definitions with different values for _family_. One for the frontend, one for the backend.
https://docs.aws.amazon.com/AmazonECS/latest/userguide/task_definition_parameters.html#family
When you register a task definition, you give it a family, which is similar to a name for multiple versions of the task definition.
Task definition has nothing to do with your cluster, you can use the same in many clusters, or on many services. But if each service runs a different set of containers, that's a different task definition.
Most helpful comment
I'm running into the same issue. I reduced the Terraform configuration to make it easier to reproduce it (the left out facts are the same as in the initial post):
Terraform Version
Terraform v0.12.7
+ provider.aws v2.26.0
Terraform Configuration Files
Important Factoids
The error can be circumvented by running
terraform apply -parallelism=1, but this slows down the execution time up to factor 10 compared to the default parallelism.When you set
count = 1it applies without errors, but of course only generates a single resource.