Terraform - 对资源类型的引用必须后跟至少一个属性访问,指定资源名称

问题描述 投票:0回答:5

我正在尝试在 terraform tfvars 变量上使用 terraform 字符串函数和字符串连接。但是当运行 terraform 计划时会出现以下异常

错误:对资源类型的引用必须后跟至少一个属性 访问,指定资源名称。

以下是terraform代码

locals {
  name_suffix = "${var.namespace != "" ? var.namespace : var.env}"
}

resource "azurerm_container_registry" "my_acr" {
  name                = "myacr${replace(name_suffix, "-", "")}"
  location            = "${azurerm_resource_group.location}"
  resource_group_name = "${azurerm_resource_group.name}"
  sku                 = "Basic"
  admin_enabled       = true
} 

这里的命名空间值将在运行时解析。

Terraform 版本 0.12.7

terraform terraform-provider-azure
5个回答
71
投票

这是一个愚蠢的错误。而不是

name_suffix
,我应该将其写成
local.name_suffix
内的
acr
资源


21
投票

为 AWS Fargate 设置 Terraform 配置文件时遇到类似问题。

出现以下错误:

│ Error: Invalid reference
│ 
│   on ../ecs/main.tf line 72, in resource "aws_ecs_service" "aes":
│   72:     type  = order_placement_type
│ 
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
╵
╷
│ Error: Invalid reference
│ 
│   on ../ecs/main.tf line 73, in resource "aws_ecs_service" "aes":
│   73:     field = order_placement_field
│ 
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

问题是我错过了变量的

var
前缀,所以改为:

ordered_placement_strategy {
    type  = order_placement_type
    field = order_placement_field
}

我将其更正为:

ordered_placement_strategy {
  type  = var.order_placement_type
  field = var.order_placement_field
}

仅此而已


2
投票

我对 acl 有相同的,这是因为我遵循过时的文档,所以我有 acl = private 而不是 acl =“private”


1
投票

还有一件事要检查。确保索引说明符位于正确的位置。

我有以下代码并遇到了这个问题:

data "cloudflare_origin_ca_root_certificate" "current" {
  count     = var.domain == null ? 0 : 1
  algorithm = tls_private_key.privateKey[0].algorithm
}
resource "aws_acm_certificate" "cert" {
  count             = var.domain == null ? 0 : 1
#...
  certificate_chain = data.cloudflare_origin_ca_root_certificate[0].current.cert_pem
}

结果我犯了一个错误,将

[0]
放在
current
选择器之前而不是之后。所以我只需将
certificate_chain
行更改为以下内容:

certificate_chain = data.cloudflare_origin_ca_root_certificate.current[0].cert_pem

0
投票

我正在尝试通过 terraform 部署 EC2 实例,但执行 ec2 命令时遇到以下错误。有人可以帮我解决吗?

错误:参考无效 │ │ 在 file.tf 第 18 行,资源“aws_instance”“web1”中: │ 18: ami = ami-0395649fbe870727e │ │ 对资源类型的引用必须后跟至少一个属性 │ 访问,指定资源名称。

© www.soinside.com 2019 - 2024. All rights reserved.