I have a relatively “complex” conditional statement all within the locals.
The below works:
<code>locals = {
all_ports = var.all_ports
main_container_definition = {
portMappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
}
}
</code>
<code>locals = {
all_ports = var.all_ports
main_container_definition = {
portMappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
}
}
</code>
locals = {
all_ports = var.all_ports
main_container_definition = {
portMappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
}
}
as does:
<code>locals = {
all_ports = var.all_ports
port_mappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
</code>
<code>locals = {
all_ports = var.all_ports
port_mappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
</code>
locals = {
all_ports = var.all_ports
port_mappings = [for port in local.all_ports: {"containerPort": tonumber(port),
"protocol": "tcp"}]
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
However, I’d like to add a conditional statement based on another variable.
<code>locals = {
all_ports = var.all_ports
host_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "hostPort": tonumber(port), "protocol": "tcp"}]
bridge_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "protocol": "tcp"}]
port_mappings = var.ecs_task_definition_network_mode == "host" ? local.host_ports : local.bridge_ports
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
</code>
<code>locals = {
all_ports = var.all_ports
host_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "hostPort": tonumber(port), "protocol": "tcp"}]
bridge_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "protocol": "tcp"}]
port_mappings = var.ecs_task_definition_network_mode == "host" ? local.host_ports : local.bridge_ports
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
</code>
locals = {
all_ports = var.all_ports
host_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "hostPort": tonumber(port), "protocol": "tcp"}]
bridge_ports = [for port in local.all_ports: {"containerPort": tonumber(port), "protocol": "tcp"}]
port_mappings = var.ecs_task_definition_network_mode == "host" ? local.host_ports : local.bridge_ports
main_container_definition = {
portMappings = tolist(local.port_mappings)
}
}
However, in doing so I get an 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
Is there a way to print the value of tolist(local.port_mappings) to see what the issue is?t