You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform模板中URL变量含${code}占位符引发资源类型引用错误的解决方法

Fixing Terraform's "A reference to a resource type must be followed by at least one attribute access" Error

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 19:57:34