我正在尝试在 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
这是一个愚蠢的错误。而不是
name_suffix
,我应该把它写成 local.name_suffix
在 acr
资源中
为 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
}
仅此而已。
我对 acl 有相同的,这是因为我遵循过时的文档,所以我有 acl = private 而不是 acl =“private”
还有一件事要检查。确保索引说明符位于正确的位置。
我有以下代码并遇到了这个问题:
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