Alex
Alex

Reputation: 79

Dynamically retrieve key's value from a terraform yaml file

I am trying to get a value from a key in a yaml file after decoding it in locals:

document.yaml

name: RandomName
emailContact: [email protected]
tags:
  - key: "BusinessUnit"
    value: "BUnit" 
  - key: "Criticality"
    value: "Criticality-Eng" 
  - key: "OpsCommitment"
    value: "OpsCommitment-Eng" 
  - key: "OpsTeam"
    value: "OpsTeam-Eng" 
  - key: "BudgetAmount"
    value: "100"

Then I have locals in main.tf:

locals {
  file = yamldecode(file(document.yaml))
}

And a have a budget.tf file where I need to retrieve the BudgetAmount of 100 dollars based on the tag key: BudgetAmount

resource "azurerm_consumption_budget_subscription" "budget" {
  name            = format("%s_%s", lower(var.subscription_name), "budget")
  subscription_id = data.azurerm_subscription.current.id

  amount     = local.landing_zone.tags[5].value
  time_grain = "Monthly"

  time_period {
    start_date = formatdate("YYYY-MM-01'T'00:00:00'Z'", timestamp())
    end_date   = local.future_10_years
  }

  notification {
    enabled   = true
    threshold = 80.0
    operator  = "EqualTo"

    contact_emails = [
      
    ]

    contact_roles = [
      "Owner"
    ]
  }
}

This local.landing_zone.tags[5].value works, but it's not a good idea if I have multiple yaml files and the position changes

Q: how do I get the BudgetAmount value of 100 from the yaml file without specifying its location inside the file, but referring to the tag's name?

I did try this:

matchkeys([local.file .tags[*].key], [local.file .tags[*].value], ["BudgetAmount"])

but it keeps telling me the value needs to be a number (obviously is getting a value, but it's a text, from one of the many key/value pairs I have in the yaml file)

Upvotes: 0

Views: 1532

Answers (1)

Jake Nelson
Jake Nelson

Reputation: 2043

I managed to get the budget by converting the list of maps into a single map with each tag being a key value.

The way you were doing it would result in the following data structure under local.file.tags:

[
  {
    "key" = "BusinessUnit"
    "value" = "BUnit"
  },
  {
    "key" = "Criticality"
    "value" = "Criticality-Eng"
  },
  {
    "key" = "OpsCommitment"
    "value" = "OpsCommitment-Eng"
  },
  {
    "key" = "OpsTeam"
    "value" = "OpsTeam-Eng"
  },
  {
    "key" = "BudgetAmount"
    "value" = "100"
  },
]

That was hard to work with and I couldn't think of any functions to help at the time so I went with changing it via the following locals:

locals {
  file = yamldecode(file("document.yaml"))

  tags = {
   for tag in local.file.tags :
        tag.key => tag.value
  }    
}

which got the tags to a structure of:

> local.tags
{
  "BudgetAmount" = "100"
  "BusinessUnit" = "BUnit"
  "Criticality" = "Criticality-Eng"
  "OpsCommitment" = "OpsCommitment-Eng"
  "OpsTeam" = "OpsTeam-Eng"
}

You can reference each of the tags in this state by using something like:

budget = local.tags["BudgetAmount"]

This was tested on Terraform v1.0.10 via terraform console

Upvotes: 2

Related Questions