Terraform 1.4.3: implementing Limiting AWS EC2 Instance Types Dynamically Based on Input Variables
I'm refactoring my project and I'm currently working on a Terraform script to provision AWS EC2 instances and I want to dynamically limit the instance types available based on user input. The goal is to use a variable to specify a family of instance types (like `t2`, `t3`, etc.) and then filter the possible instance types accordingly. However, I'm running into issues where the filtering doesn't seem to work as expected, and I need to seem to get past the behavior `Invalid instance type: instance type 't3.micro' is not supported in the selected region`. I've set up my variable for the instance type family like this: ```hcl variable "instance_type_family" { description = "The EC2 instance type family to use" type = string } ``` And I'm attempting to filter the instance types using a locals block: ```hcl locals { allowed_instance_types = [ for type in flatten([ var.instance_type_family == "t2" ? ["t2.micro", "t2.small"] : [], var.instance_type_family == "t3" ? ["t3.micro", "t3.small"] : [] ]) if type != "" ] } ``` Then, I try to use this local variable to set the instance type in my resource block: ```hcl resource "aws_instance" "app" { count = var.instance_count ami = var.ami_id instance_type = local.allowed_instance_types[0] } ``` Even after ensuring that the `instance_type_family` variable is set to `t3`, I'm still getting errors about unsupported instance types. I've tried hardcoding the allowed types just to check if the filtering works, and I don't see any issues there. Is there something I'm missing here? Am I not accessing the local variable correctly or is there a better way to handle this kind of dynamic instance type selection? Any insights would be much appreciated! This is part of a larger application I'm building. Thanks in advance! I've been using Hcl for about a year now. Cheers for any assistance!