Context
I am creating aws_dms_replication_task
s with a for_each
, trying to build the required table_mappings
from a template file. The mappings must be JSON so I did
table_mappings = jsonencode(templatefile("./tableMappings.tftpl", each.value.table_mappings))
Problem
Doing the above results in the following error: InvalidParameterValueException: Invalid Table Mappings document. Invalid json Invalid json object
and only references the resource I am trying to create, no indicator of where the problem is in the JSON.
To ensure that there is not a problem with my template I reduced it to a very simple case and ran it through a JSON validator. I still get the error, so I know the problem is with the way I attempted to convert the template result. Simplified tableMappings.tftpl
for reference:
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "1",
"rule-name": "1",
"rule-action": "add-column",
"rule-target": "column",
"object-locator": {
"schema-name": "example",
"table-name": "%"
},
"value": "orgId",
"expression": "'orgId-001'",
"data-type": {
"type": "string",
"length": 36
}
}
]
}
Question
How can I correctly JSON encode the result of templatefile()
?
The underlying issue
I am aware that the normal practice is to wrap the template with ${jsonencode(...)}
, which does prevent the error. However, I need to create a list of rules in the template where two of them are static and the rest are formed by a loop. I can do this with template syntax, but wrapping with jsonencode
puts me in expression syntax, and in expression syntax results of loops must come back enclosed in a list or object so I would end up with the incorrect result:
"rules": [
{ ...rule 1},
{ ...rule 2},
[ ...rules from loop stuck in here ]
]
I have a couple ideas for workarounds, but I would appreciate a way to do this without cluttering up my template. Thanks!