I am trying to create an aws_instance resource and in order to avoid resource duplication and using the same template for different Environment qa/prod, i am using the below -
variable "region" {
description = "east or west"
type = "string"
default = "east"
}
variable "env" {
description = "Environment - test, it, perf, or prod"
type = "string"
default = "qa"
}
variable "qa_subnets" {
description = "Subnets for EC2"
type = "map"
default = {
"qa-east" = ["subnet-xxxxx", "subnet-xxxxxx", "subnet-xxxxx", "subnet-xxxx"]
"prod-east" = ["subnet-xxxx", "subnet-xxxxx", "subnet-xxx", "subnet-95d292bf"]
}
subnet_id = "${element(lookup(var.qa_subnets, format("%s-%s", lower(var.env),var.region)), count.index)}"
I am getting the below error when i do a Plan -
Errors:
* At column 3, line 1: element: argument 1 should be type list, got type string in:
${element(lookup(var.qa_subnets, format("%s-%s", lower(var.env),var.region)), count.index)}
* At column 3, line 1: element: argument 1 should be type list, got type string in:
${element(lookup(var.qa_subnets, format("%s-%s", lower(var.env),var.region)), count.index)}
Is this type of lookup currently possible in Terraform (0.9.3)
Hey @roman1627 – your map is named qa_slave_subnets in the example, but it's qa_subnets in the error. Also, the format in the map key using _ not -. Are those just typos in sharing the error/snippet, or actual typos?
@catsby - Yeah, those were typo's, thanks for pointing them out.
@roman1627 ah, well that's good... so I did some reading and found that lookup only works on flat maps, and will return an error for maps that include nested lists or maps. So the error we're seeing argument 1 should be type list, got type string I think got string part is the string of the error returned, it seems. I'm not sure why that is, unfortunately, but you could change your subnet-xxx list to just a comma separated string, and then expand it into an actual list with split after you do the lookup... an extra, maybe confusing step, but should work 🤔
@catsby - Do you mean something like this -
variable "region" {
description = "east or west"
type = "string"
default = "east"
}
variable "env" {
description = "Environment - test, it, perf, or prod"
type = "string"
default = "qa"
}
variable "qa_subnets" {
description = "Subnets for EC2"
type = "map"
default = {
"qa-east" = "subnet-xxxxx, subnet-xxxxx, subnet-xxxx, subnet-xxxxx"
}
```
subnet_id = "${element(split(",", lookup(var.qa_subnets, format("%s-%s", lower(var.env),var.region))), count.index)}"
````
Wow that totally worked.... thanks @catsby Closing the Issue.
Awesome! Glad it worked, albeit it not very simple 😄
Thanks for reporting back
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
@catsby - Do you mean something like this -
```
subnet_id = "${element(split(",", lookup(var.qa_subnets, format("%s-%s", lower(var.env),var.region))), count.index)}"
````