Terraform-provider-aws: Terraform ECS task environment variable with json

Created on 1 Nov 2017  ยท  10Comments  ยท  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @sunilkumarmohanty as hashicorp/terraform#16519. It was migrated here as a result of the provider split. The original body of the issue is below._


Hi there,

Terraform Version

0.10.0

Terraform Configuration Files

```hcl

Copy-paste your Terraform configurations here.

#

I am creating an ecs task definition with terraform. My variable looks like the below
some_json_env = "{\"foo\":\"bar\"}"

In ecs task definition I am setting the environment variable

{
"name": "SOME_JSON",
"value": "${var.some_json_env}"
}

I get the

Expected Behavior

I want the environment variable in the container to be as below
SOME_JSON = {"foo":"bar"}

Actual Behavior

I get an error saying error decoding JSON: invalid character 'f' after object key:value pair

bug

All 10 comments

In the task definition, I can set the environment variable directly

{
          "name": "SOME_JSON",
          "value": "{\"foo\":\"bar\"}"
      }

This works without any issue

We ran into a similar "issue" in our Terraform configuration when we needed variables with quotes in container definition environment variables.

The trick here is that there are two levels of string escaping happening, one by Terraform's string interpolation and one by ECS itself when it passes the container definition JSON. I believe if you use \\" in the variable, you should see ECS throws an invalid JSON error, so the correct answer is to escape the quotes again with additional slash: \\\"

\\\" -Terraform-> \" -ECS-> "

Looks real ugly when you need to do it but it should work.

Hi @bflad,

If I understood correctly, should I use
"value": "{\\\"foo\\\":\\\"bar\\\"}"

thanks @bflad
it worked as per your suggestion

@Ninir / @radeksimko this should be good to close unless you think we need some additional documentation, e.g. #2162

I think the note is good enough.

So this means you effectively cannot parameterize environment variables as a map to accept an arbitrary number of them, and must instead specifically spell out the accepted variables, so that you can properly escape the quotes for each one, right?

@pizzaops that's the conclusion I've come to. These guys have done some heroic work to allow constructing the container definition JSON within Terraform, including properly composing a map of environment variables, but I couldn't get it to work: https://github.com/Eiara/terraform_container_definitions.

Damn. It was unnessessary difficult to parametrize environment variables that are passed down to ECS task definition. But thanks to https://github.com/Eiara/terraform_container_definitions I managed to do it.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings