Terraform outputs 'Error: Variables not allowed' when doing a plan

2 min read 06-10-2024
Terraform outputs 'Error: Variables not allowed' when doing a plan


Terraform: "Error: Variables not allowed" – A Common Pitfall and Its Solution

When working with Terraform, encountering the error "Error: Variables not allowed" during a terraform plan can be frustrating. This typically happens when you attempt to use variables within a resource block that doesn't support them. This article will clarify the issue, provide a solution, and offer best practices to avoid similar errors in the future.

Understanding the Problem

The "Error: Variables not allowed" message means that Terraform detected an attempt to use a variable in a resource attribute that doesn't accept dynamic values. This is common when working with resources that require predefined values during resource creation, not variable inputs.

Scenario and Code

Let's illustrate with a common scenario:

variable "instance_type" {
  type = string
  default = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-0842886230f737a2e"
  instance_type = var.instance_type
  # ... other attributes
}

resource "aws_security_group" "allow_ssh" {
  name   = "allow_ssh"
  # ... other attributes
}

resource "aws_security_group_rule" "ssh" {
  type              = "ingress"
  from_port          = 22
  to_port            = 22
  protocol           = "tcp"
  cidr_blocks        = ["0.0.0.0/0"]
  security_group_id = aws_security_group.allow_ssh.id
}

In this code, we aim to create an EC2 instance with a configurable instance_type. However, using var.instance_type directly within the aws_instance resource will trigger the "Error: Variables not allowed" error.

Analysis and Solution

The error arises because aws_instance resource expects the instance_type attribute to be a fixed value during creation, not a dynamic variable.

Solution: To fix this, we need to create a resource that supports dynamic values. This is where local values come in handy:

variable "instance_type" {
  type = string
  default = "t2.micro"
}

locals {
  instance_type = var.instance_type
}

resource "aws_instance" "example" {
  ami           = "ami-0842886230f737a2e"
  instance_type = local.instance_type
  # ... other attributes
}

# ... rest of the code

By using a locals block, we create a local variable that stores the value from the instance_type variable. Now, the aws_instance resource uses this local variable, which allows for dynamic configuration while staying within the resource's limitations.

Best Practices

  • Understand Resource Requirements: Carefully examine the documentation for each resource to determine which attributes support dynamic values and which require fixed values.
  • Local Values for Flexibility: Use locals blocks to create dynamic values within your Terraform code. This allows for flexible configurations without breaking resource limitations.
  • Leverage Terraform Modules: For complex configurations, consider using Terraform modules to encapsulate related resources and their dynamic configurations.

Conclusion

The "Error: Variables not allowed" error in Terraform usually occurs when you try to apply a variable to a resource attribute that doesn't accept dynamic values. Understanding the distinction between fixed and dynamic attributes is crucial to prevent this error. By using local values and adhering to best practices, you can ensure your Terraform code remains flexible, efficient, and error-free.

Resources: