Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - 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

like image 640
vashishth Avatar asked Sep 04 '19 18:09

vashishth


People also ask

What happens if terraform apply fails?

Terraform can't rollback after deployment. So, if an error appears in the deployment, the issue should be solved in that moment Also, is possible to destroy the deployment (terraform destroy), but it will destroy everything and not rollback the changes.

What is terraform cycle error?

Terraform Cycle Error A cycle error is considered to be an instance of circular logic in your Terraform configuration. When a resource depends on another resource to be created first, but that resource is dependent on the other resource being created, it cause a circle of failed creation in Terraform.


2 Answers

it was a silly mistake. instead of name_suffix, I should have written it like local.name_suffix inside the acr resource

like image 77
vashishth Avatar answered Oct 22 '22 05:10

vashishth


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.

like image 5
Promise Preston Avatar answered Oct 22 '22 04:10

Promise Preston