Jsonencode with aws tag policy

Hi,

I am retrieveing values for our teams from tool OpsLevel and trying to insert those into AWS Organizations Tag Policy. I keep getting MalformedPolicyDocumentException: The provided policy document does not meet the requirements of the specified policy type. error message no matter what I try.

I am getting a list of teams from Opslevel which looks like this:

opslevel_teams = [
        teamA,
        teamB, 
        teamC,
    ]

And I want to use this in AWS Tag policy in tag values which has this format:

tag_policy = {
  "tags": {
    "team": {
      "tag_key": {
        "@@assign": "team"
      },
    "tag_value": {
        "@@assign": jsonencode(data.opslevel_teams.all.names)
      }
  }
}
}

And then use the policy in the resource:

resource "aws_organizations_policy" "tagging_policy" {
  name = "tags"
  type = "TAG_POLICY"
  content = jsonencode(local.tag_policy)
}

I tried many different ways with for and jsonencode adding this into the @@asign array but still getting the same error. When I manually change the content of that array to something like “test” it works.

Can anyone help me solve this?

If I take the policy above and subsitute the teams list with ["test"] array I can add that in the console.

{
  "tags": {
    "team": {
      "tag_key": {
        "@@assign": "team"
      },
      "tag_value": {
        "@@assign": [
          "test"
        ]
      }
    }
  }
}

I figured it out. The problem is that some of our company teams have & and , in their names so it’s a problem of allowed characters in tag values and not very informative AWS error message.