CodexBloom - Programming Q&A Platform

Terraform: scenarios 'Invalid function argument' when using local variables for EC2 instance tags

👀 Views: 40 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
terraform aws ec2 local-variables HCL

Hey everyone, I'm running into an issue that's driving me crazy. I'm wondering if anyone has experience with I'm performance testing and I'm working with the behavior `Invalid function argument` when I try to apply a Terraform configuration that uses local variables to define tags for an AWS EC2 instance. The behavior seems to occur when I'm trying to reference these local variables within the resource block. Here's a snippet of the code I'm working with: ```hcl locals { environment = "production" owner = "devteam" } resource "aws_instance" "my_instance" { ami = "ami-12345678" instance_type = "t2.micro" tags = { Name = "MyInstance" Environment = local.environment Owner = local.owner } } ``` When I run `terraform apply`, I get the following behavior message: ``` behavior: Invalid function argument on main.tf line 9, in resource "aws_instance" "my_instance": 9: Environment = local.environment Invalid function argument ``` I've double-checked that the local variables are defined correctly, and I've tried running `terraform validate`, which passes without issues. I've also looked into the documentation but couldn't find anything that seems off. I suspect the scenario might be related to how I'm defining or referencing the local variables. I've tried simplifying the tags by directly using string literals instead of locals, and that works fine. However, I really want to keep using locals for maintainability and clarity in my configuration. Any insights on what could be going wrong or how to correctly define the tags using local variables? I'm using Terraform version 1.3.6. Any pointers in the right direction? I'm working in a CentOS environment. Has anyone dealt with something similar? I'm using Hcl latest in this project. What's the correct way to implement this?