vashishth
vashishth

Reputation: 3370

A reference to resource type must be followed by at least one attribute access, specifying the resource name

I am trying to use terraform string function and string concatenation on a terraform tfvars variable. but when run the terraform plan it through the below exception

Error: A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

Following is the terraform code

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
} 

Here namespace value will be resolved at runtime.

Terraform version 0.12.7

Upvotes: 66

Views: 112307

Answers (4)

vashishth
vashishth

Reputation: 3370

It was a silly mistake. Instead of name_suffix, I should have written it as local.name_suffix inside the acr resource.

Upvotes: 90

Alex
Alex

Reputation: 457

I had the same for acl and it was because I was following out of date docs so I had acl = private instead of acl = "private"

Upvotes: 2

Charlie Fish
Charlie Fish

Reputation: 20526

Another thing to check. Make sure you have the index specifier in the correct position.

I had the following code and ran into this problem:

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
}

Turns out I made the mistake of putting the [0] before the current selector instead of after. So I just had to change the certificate_chain line to the following:

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

Upvotes: 1

Promise Preston
Promise Preston

Reputation: 28900

Had a similar issue when setting up Terraform configuration files for AWS Fargate.

Got the error below:

│ 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.

The issue was that I missed the var prefix for variables, so instead of this:

ordered_placement_strategy {
    type  = order_placement_type
    field = order_placement_field
}

I corrected it to this:

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

That's all.

Upvotes: 25

Related Questions