Hi Commnity, I wanna to create multiple task definitions from a string list variable, the inside my tf file a try to use variables to replace name, port and other settings, but in my port definitions block my variable substitution is not working.
My variables file:
variable "services" {
description = "Definición de cada uno de los servicios que deben crearse dentro del namespace para cloudmap"
type = list(any)
default = ["seguridad", "personas", "correo-electronico", "documentos", "vehiculo", "opconcierge", "financiacion", "runt", "comparendo", "coactivo", "patio", "workflow"]
}
variable "service_ports" {
description = "Ports definition for each service"
type = list(any)
default = ["9040", "9041", "9042", "9043", "9044", "9045", "9046", "9047", "9048", "9049", "9050", "8080"]
}
variable "container_images" {
type = list(any)
default = ["coactivo-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev", "seguridad-be-dev"]
}
and my task-definition.tf file
resource "aws_ecs_task_definition" "task_definition" {
count = length(var.services)
family = "task-cd-col-${element(var.services, count.index)}"
container_definitions = jsonencode(
[
{
cpu = 0
dependsOn = [
{
condition = "HEALTHY"
containerName = "envoy"
},
]
environment = []
essential = true
image = element(var.container_images, count.index)
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "/ecs/task-dlab-col-coactivo-prod"
awslogs-region = "us-east-1"
awslogs-stream-prefix = "ecs"
}
}
mountPoints = []
name = "cont-cd-col-${element(var.services, count.index)}-prod"
portMappings = [
{
containerPort = element(var.service_ports, count.index)
hostPort = element(var.service_ports, count.index)
protocol = "tcp"
},
]
volumesFrom = []
},
{
cpu = 0
environment = [
{
name = "APPMESH_VIRTUAL_NODE_NAME"
value = "${var.app_mesh_name}/virtualNode/${element(var.services, count.index)}"
},
]
essential = true
healthCheck = {
command = [
"CMD-SHELL",
"curl -s http://localhost:9901/server_info | grep state | grep -q LIVE",
]
interval = 5
retries = 3
startPeriod = 10
timeout = 2
}
image = var.envoy_image
memory = 500
mountPoints = []
name = "envoy"
portMappings = []
user = "1337"
volumesFrom = []
},
]
)
cpu = "256"
execution_role_arn = "arn:aws:iam::073061431630:role/ecsTaskExecutionRole"
memory = "512"
network_mode = "awsvpc"
requires_compatibilities = [
"FARGATE",
]
tags = {
"Ambiente" = "Produccion"
"Proyecto" = "Datalab_Col"
}
tags_all = {
"Ambiente" = "Produccion"
"Proyecto" = "Datalab_Col"
}
task_role_arn = "arn:aws:iam::073061431630:role/ecsTaskExecutionRole"
proxy_configuration {
container_name = "envoy"
properties = {
"AppPorts" = element(var.service_ports, count.index)
"EgressIgnoredIPs" = "169.254.170.2,169.254.169.254"
"EgressIgnoredPorts" = "8899"
"IgnoredGID" = ""
"IgnoredUID" = "1337"
"ProxyEgressPort" = "15001"
"ProxyIngressPort" = "15000"
}
type = "APPMESH"
}
}
When I run a terraform plan command I get this error:
Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field PortMapping.PortMappings.ContainerPort of type int64
But if I replace these lines
containerPort = element(var.service_ports, count.index)
hostPort = element(var.service_ports, count.index)
protocol = "tcp"
with the port number my terraform plan works fine, but all my container has a different ports.
Please, cloud you help me? Anyone knos what is my problem?
Thank you!