Terraform模板中URL变量含${code}占位符引发资源类型引用错误的解决方法
Let's break down what's happening here and how to fix it quickly:
Why This Error Happens
Terraform treats ${...} as its own interpolation syntax. When you passed the URL https://nonprod.com/auth/prod/${code} in your module call, Terraform tried to interpret ${code} as a reference to a Terraform variable, resource, or local value—but you don't have a code defined in your Terraform context. That's exactly why you're seeing that confusing error message.
The ${code} is actually a placeholder for your Freemarker template, not Terraform syntax, so we need to tell Terraform to leave it alone.
Solution 1: Escape ${} with Double Dollar Signs
The simplest fix is to escape the $ character by doubling it. Terraform will render a single $ in the final string, leaving the ${code} intact for your template:
module "fusionauth_sit" { source = "../modules/fusionauth" env_name = "Test" tenant_host = "test" url = "https://nonprod.com/auth/prod/$${code}" # Double $ to escape }
Solution 2: Use a Heredoc for Cleaner Syntax
If you have multiple template placeholders in your URL (or just prefer cleaner code), use a Terraform heredoc string. Heredocs don't interpolate ${...} by default, so you can write the placeholder exactly as you need it:
module "fusionauth_sit" { source = "../modules/fusionauth" env_name = "Test" tenant_host = "test" url = <<-EOT https://nonprod.com/auth/prod/${code} EOT }
The - in <<-EOT lets you indent the URL for readability without adding extra whitespace to the final string.
Verify the Fix
After making either change, run terraform plan to confirm the error is gone. Terraform will now pass the full URL (including ${code}) to your templatefile call, and your Freemarker template will be able to use that placeholder as intended.
内容的提问来源于stack exchange,提问作者cloudbud

