Hi,
I’m trying to deploy multiple ec2 instances. This requires a variable below.
Post my variables:
variable “configuration” {
#description = “The total configuration, List of Objects/Dictionary”
default = [
{
“application_name” : “GritfyApp-dev”,
“ami” : “ami-0263e4deb427da90e”,
“no_of_instances” : “2”,
“instance_type” : “t2.medium”,
“subnet_id” : “subnet-0dd2853d1445a9990”,
“vpc_security_group_ids” : [“sg-00e9778636a7aee5e”,“sg-082e93cadbd35fd8b”]
# “vpc_security_group_ids” : [aws_security_group.ssh-sg.id]
},
{
“application_name” : “ssh-dev”,
“ami” : “ami-0747bdcabd34c712a”,
“instance_type” : “t3.micro”,
“no_of_instances” : “1”
“subnet_id” : “subnet-0dd2853d1445a9990”
“vpc_security_group_ids” : [“sg-00e9778636a7aee5e”,“sg-082e93cadbd35fd8b”]
# “vpc_security_group_ids” : [aws_security_group.ssh-sg.id]
},
{
“application_name” : “OpsGrit-dev”,
“ami” : “ami-0747bdcabd34c712a”,
“instance_type” : “t3.micro”,
“no_of_instances” : “3”
“subnet_id” : “subnet-0dd2853d1445a9990”
“vpc_security_group_ids” : [“sg-00e9778636a7aee5e”,“sg-082e93cadbd35fd8b”]
# “vpc_security_group_ids” : [aws_security_group.ssh-sg.id]
}]
}
Because vpc_security_group_ids is bound to the specified application_name. I can hard code it by “vpc_security_group_ids”: [“sg-00e9778636a7aee5e”, “sg-082e93cadbd35fd8b”]. But that’s obviously not elegant enough.
I tried to use local to achieve, but still failed.
Note: You didn’t use the -out option to save this plan, so Terraform can’t guarantee to take exactly these actions if
you run “terraform apply” now.
root@Will-T-PC:/mnt/c/terraform-leaning-will/Expressions/deploy-multiple-ec2-for_each-2# terraform plan
╷
│ Error: Variables not allowed
│
│ on variables.tf line 17, in variable “configuration”:
│ 17: “vpc_security_group_ids” : local.target_sg
│
│ Variables may not be used here.
╵