Quantcast
Channel: Terraform - HashiCorp Discuss
Viewing all articles
Browse latest Browse all 11363

Templatefile JSON input for aws tag policy

$
0
0

@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 string

One 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

Read full topic


Viewing all articles
Browse latest Browse all 11363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>