@hansarh wrote:
I’m trying to create a tag policy of using “aws_organizations_policy” resource. I’m trying to create this structure:
{ "tags": { "tag1": { "tag_key": { "@@assign": "tag1", "@@operators_allowed_for_child_policies": ["@@none"] } }, "tag2": { "tag_key": { "@@assign": "tag2", "@@operators_allowed_for_child_policies": ["@@none"] } } } }
I want to create this tag policy structure based on a list of tags as input, and I’ve tried using the templatefile function with a for directive to create new “tag-blocks” for each tag in the list.
this is my tags.tmpl file:{ "tags": { %{ for tag in tags ~} "${tag}": { "tag_key": { "@@assign": "${tag}", "@@operators_allowed_for_child_policies": ["@@none"] } }, %{ endfor ~} } }
In the configuration I’m calling the function like this:
standard_tags_policy = templatefile("${path.module}/templates/tags.tmpl", { tags = var.standard_tags })
standard_tags is a list of tag keys. the “standard_tags_policy” is a local variable which I place in the resource configuration like this:
content = <<CONTENT ${local.standard_tags_policy} CONTENT
I get this error:
Error: “content” contains an invalid JSON: invalid character ‘}’ looking for beginning of object key stringOne error I’ve found in the .tmpl I’ve written is that the last “tag-black” will end with a comma which is incorrect json. I’ve read jsonencode in the .tmpl file might be easier to use for this scenario, but I don’t understand how I can use the “for expression” as the example shows rather than the “for directive” which I’ve made use of? Hope this makes sense, and I really hope someone can help me out:)
Posts: 1
Participants: 1