Hi Team,
I am still not clear on how to use the feature Terraform v0.13 Beta: Module for_each
and count
i specified this
variable “vpc_id” {
type = string
}
variable “subnets” {
type = map(object({
cidr_block = string
availability_zone = string
}))
}
How do i specify the value for cidr_block and availability_zone ?
I tried this
subnets = {
test = {
cidr_block = “10.0.0.0/16”
availability_zone = “eu-west-1”
}
test2= {
cidr_block = “10.10.0.0/16”
availability_zone = “eu-west-1”
}}
it gave me this error Error: Unsupported argument . Any idea how can i fix it ?
Hi there,
We recently published Learn tutorials that cover how to use for_each
and count
in both resources and modules. Both tutorials walks you through an example configuration where you have to implement either for_each
or count
to manage multiple pieces of infrastructure.
- Manage Similar Resources With Count
- Manage Similar Resources With For Each
To answer your question, the configuration below would iterate through the subnets
variable and create two subnets with differing cidr_block
values.
variable "vpc_id" {
type = string
}
variable "subnets" {
type = map(object({
cidr_block = string
availability_zone = string
}))
default = {
test = {
cidr_block = "10.0.0.0/16"
availability_zone = "eu-west-1"
},
test = {
cidr_block = "10.10.0.0/16"
availability_zone = "eu-west-1"
}
}
}
resource "aws_subnet" "main" {
vpc_id = var.vpc_id
# Looping through subnet variable
for_each = var.subnets
cidr_block = each.value.cidr_block
availability_zone = each.value.availability_zone
}
Let me know if that helps or if you have any additional questions!
Thank you for the help. It is better situation now
1 Like
So i have next issue in the module writing and i will try to explain more .
This is my sample module
module “test1” {
for_each = var.subnets
name = each.value.name
}
and test1 pass the value to a module which pass this to 2 more module name as test1build and test1deploy . And test 1 is invoke by 2 options of subnets
So my questions are
-
Is the output for test 1 module should it have output also as value = { for p in sort(keys(var.project)) : p => module.elb_http[p].this_elb_dns_name } ?
-
And i have another module name test2 which need output value from test1 . So how should i declare output and how should be my module test 2 ?
Currently i have test 2 module as module “test2” {
for_each = var.subnets
role_name = module.test1[each.key].role_name
}