AWS Budgets in Terraform

I ran into an issue working with Terraform to setup an AWS Budget for a project and didn’t find the fix in the documentation or online. Thought I’d share here.

The docs for Terraform’s aws_budgets_budget resource don’t go into any detail on how the value of the TagKeyValue property for the cost_filters parameter should be formatted. Terraform expects a string but doesn’t indicate how to encode the tag name and value into it. I found an example JSON file in the docs for the AWS CLI command here that clued me in to the key$value syntax. So, I tried this:

resource "aws_budgets_budget" "budget" {
  ...
  cost_filters = {
    TagKeyValue = "user:Environment$$${var.app_name}-${var.app_env}"
  }
}

Terrform liked it but when I tried to apply, AWS balked. Turns out the variable interpolation didn’t work as I expected. I ended up getting only part of the variables replaced; user:Environment$${var.app_name}-prod. The $$ and ${var.app_env} were replaced as expected but it left the ${var.app_name}!?

I ended up with this:

resource "aws_budgets_budget" "budget" {
  ...
  cost_filters = {
    TagKeyValue = format("user:Environment$%s-%s", var.app_name, var.app_env)
  }
}

Working now.

comments powered by Disqus