Home > Blockchain >  Terraform and tagging AWS resources from an external json file
Terraform and tagging AWS resources from an external json file

Time:05-15

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.

CodePudding user response:

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.

  • Related