CodexBloom - Programming Q&A Platform

Terraform Plan scenarios with "scenarios: Invalid function argument" when Using Conditional Expressions

👀 Views: 5 đŸ’Ŧ Answers: 1 📅 Created: 2025-05-31
terraform aws conditional-expressions HCL

I'm testing a new approach and I'm refactoring my project and I'm optimizing some code but I'm trying to use conditional expressions in my Terraform configuration to set the desired instance type based on a variable that indicates the environment (dev or prod)... However, when I run `terraform plan`, I get the following behavior: `behavior: Invalid function argument`. Here's the relevant snippet of my Terraform code: ```hcl variable "environment" { description = "Deployment environment" type = string } resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = var.environment == "prod" ? "t2.large" : "t2.micro" } ``` I defined the `environment` variable in my `variables.tf` file and set it like this: ```hcl variable "environment" { default = "dev" } ``` When I explicitly set `environment = "prod"`, it works as expected, but when I leave it at the default value, I get the behavior. I suspect it might be related to how I'm defining the conditional expression, but I need to quite pinpoint it. I've tried wrapping the conditional expression with `tostring()` and checking for null values, but the behavior continues. Is there a specific requirement or limitation with using conditional expressions in Terraform that I'm missing? I'm using Terraform version 1.3.0, and I would appreciate any insights or suggestions for troubleshooting this scenario. How would you solve this? This is my first time working with Hcl stable. Has anyone else encountered this? This is my first time working with Hcl latest. Any pointers in the right direction? This is for a application running on Windows 10. Has anyone else encountered this?