I'm trying to convert a locals variable to string.
locals {
env_vars = {
STAGE = "${var.environment}"
ENVIRONMENT = "${var.environment}"
REGION = "${var.region}"
AWS_DEFAULT_REGION = "${var.region}"
}
}
container_definitions = templatefile("${path.module}/task-definitions/ecs-web-app.json", {
# This works just fine:
#ENVIRONMENT_VARS = <<EOT
# {"name":"STAGE","value":"${var.environment}"},
# {"name":"ENVIRONMENT","value":"${var.environment}"},
# {"name":"REGION","value":"${var.region}"},
# {"name":"AWS_DEFAULT_REGION","value":"${var.region}"}
# EOT
# But I'm trying to get this to work instead:
ENVIRONMENT_VARS = join(",", [for key, value in local.env_vars : "{{\"name\":\"${key}\",\"value\":\"${value}}}\""])
}
Where foo.json is:
[
{
"environment":[
${ENVIRONMENT_VARS}
]
}
]
The error I get is:
Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character '{' looking for the beginning of object key string
So in a nutshell, I'm trying to convert:
locals {
env_vars = {
STAGE = "${var.environment}"
ENVIRONMENT = "${var.environment}"
REGION = "${var.region}"
AWS_DEFAULT_REGION = "${var.region}"
}
}
To a string that looks like this:
{"name":"STAGE","value":"dev"},
{"name":"ENVIRONMENT","value":"dev"},
{"name":"REGION","value":"us-west-2"},
{"name":"AWS_DEFAULT_REGION","value":"us-west-2"}
I'm almost 100% sure the curly braces are throwing me off, but I can't figure out what I'm doing wrong.
Any help or pointers would be greatly appreciated.
CodePudding user response:
You have one too many curly brackets. It should be:
container_definitions = templatefile("foo.json", {
ENVIRONMENT_VARS = join(",", [for key, value in local.env_vars:
"{\"name\":\"${key}\",\"value\":\"${value}}\""
])
})