In another thread I have asked how to keep ECS task definitions active in AWS. As a result I am planning to update a task definition like this:
resource "null_resource" "update_task_definition" {
triggers {
keys = "${uuid()}"
}
# Workaround to prevent older task definitions being deactivated
provisioner "local-exec" {
command = <<EOF
aws ecs register-task-definition \
--family my-task-definition \
--container-definitions ${data.template_file.task_definition.rendered} \
--network-mode bridge \
EOF
}
}
data.template_file.task_definition
is a template data source which provides templated JSON from a file. However, this does not work, since the JSON contains new lines and whitespaces.
I figured out already that I can use the replace
interpolation function to get rid of new lines and whitespaces, however I still require to escape double quotes so that the AWS API accepts the request.
How can I safely prepare the string resulting from data.template_file.task_definition.rendered
? I am looking for something like this:
Raw string:
{
"key": "value",
"another_key": "another_value"
}
Prepared string:
{\"key\":\"value\",\"another_key\":\"another_value\"}