I’m getting an Incorrect attribute value type
when I try to create a variable in TF.
I’m on this version of TF:
Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/aws v2.70.0
This is the error that I’m getting:
λ terraform apply
Error: Incorrect attribute value type
on main.tf line 18, in resource "aws_instance" "example":
18: vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"]
Inappropriate value for attribute "vpc_security_group_ids": set of string
required.
This is the code block in my main.tf
code:
resource "aws_instance" "example" {
ami = var.amis["us-east-1"]
instance_type = "t2.micro"
vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"] # < --- the line causing the problem
subnet_id = var.subnet_id["jf-master-pd"]
key_name = "jf-timd-keypair"
}
These are the variables that I’m trying to set in the variables.tf
file:
variable "vpc_security_group_ids" {
type = map(string)
default = {
"jf-master-pd" = "sg-0333d9eaaeb3ab1b0"
"jf-master-pd-gov" = "sg-7f051404"
}
}
variable "subnet_id" {
type = map(string)
default = {
"jf-master-pd" = "subnet-3ab1835d"
"jf-master-pd-gov" = "subnet-4dad6304"
}
}
These two lines seem functionally the same:
vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"] # <--- line causing the problem
subnet_id = var.subnet_id["jf-master-pd"]
If I replace:
vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"]
with vpc_security_group_ids = ["sg-0333d9eaaeb3ab1b0"]
The terraform code applies without error. I just don’t see what’s wrong with this line:
vpc_security_group_ids = var.vpc_security_group_ids["jf-master-pd"]
The main.tf
and variables.tf
files are living in the same directory.
Why am I getting this error? How do I correct it?