Variable with $ before doesn't get (wrong) parsed

Hello,

locals {
  TagKeyValue = "user:Service$$${var.Service}"
}

cost_filter {
    name   = "TagKeyValue"
    values = [
      local.TagKeyValue
    ]

Results in

          + values = [
              + "user:Service$${var.Service}",
            ]

Other tried:

locals {
  TagKeyValue = "user:Service$'${var.Service}'"
}

which results in

      + values = [
          + "user:Service$'Test",
        ]

but the β€˜β€™ is wrong on place,

This is an unfortunate case where your desired string includes the HCL escape sequence for an interpolation, $${. There’s no direct way to escape the escape sequence, so instead you need to break up your string template and recombine it.

Here are a couple of ways of doing that:

variable "service" {
  default = "Test"
}

locals {
  double_interpolation = "user::Service${"$$"}${var.service}"
  join_function = join("$$", ["user::Service", var.service])
}

output "result" {
  value = [local.double_interpolation, local.join_function]
}
$ terraform plan

Changes to Outputs:
  + result = [
      + "user::Service$$Test",
      + "user::Service$$Test",
    ]
1 Like

Thanks @alisdair got it working with your code and some little modification:

# variables
variable "LinkedAccount" {
  type = number
}

variable "limit_amount" {
  type = number
}

variable "Customer" {
  type = string
}

variable "Service" {}

# locals
locals {
  interpolation = "user:Service${"$"}${var.Service}"
}

# ressource
resource "aws_budgets_budget" "Service" {
  name              = "AWS Org - ${var.Customer} - ${var.Service}"
  budget_type       = "COST"
  limit_amount      = var.limit_amount
  limit_unit        = "USD"
  time_unit         = "MONTHLY"

  cost_filter {
    name = "LinkedAccount"
    values = [
      var.LinkedAccount,
    ]
  }

  cost_filter {
    name   = "TagKeyValue"
    values = [
      local.interpolation
    ]
  }
}

Results in

      + cost_filter {
          + name   = "TagKeyValue"
          + values = [
              + "user:Service$Shopware Staging",
            ]
        }