'Terraform and tagging AWS resources from an external json file

If I had a json file like this:

{
    "allMyTags": {
        "owner": "john",
        "department": "HR",
        "city": "New York"
    }
}

and my AWS provider terraform main.tf looks like this:

resource "aws_vpc" "example" {
  # ... other configuration ...

  tags = {
    owner = "john"
  }
}

How do I go about replacing everything that is in the tags section of main.tf with the external json file. The json file is a lot longer that I have put up there and I just didn't want to manually put in 20 values in the tags section of main.tf. Is there a way to "loop" thru the json file and add it in? Thanks for any help you can provide.



Solution 1:[1]

Assuming that you json is already loaded into TF, you could do:

resource "aws_vpc" "example" {
  # ... other configuration ...

  tags = jsondecode(local.myjson["allMyTags"])
}

where local.myjson is the loaded json to TF.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin