# For each loops and ouput.tf files and how to get output values

**URL:** <https://discuss.hashicorp.com/t/for-each-loops-and-ouput-tf-files-and-how-to-get-output-values/33794>\
**Category:** Terraform\
**Tags:** hcp-terraform\
**Created:** [January 3, 2022, 11:58pm UTC](https://discuss.hashicorp.com/t/for-each-loops-and-ouput-tf-files-and-how-to-get-output-values/33794 "2022-01-03T23:58:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpor1974](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jpor1974/32/24856_2.png) [@jpor1974](https://discuss.hashicorp.com/u/jpor1974)\
**Post date:** [January 3, 2022, 11:58pm UTC](https://discuss.hashicorp.com/t/for-each-loops-and-ouput-tf-files-and-how-to-get-output-values/33794/1 "2022-01-03T23:58:24Z")

</div>

hi, lets figure this:

#### 

we have 2 diferent modules, a module to create all network objct and othe r only to create EC2  
and a general main to call all this modules:

:::::::::::::::::  
network modules  
:::::::::::::::::::  
/modules/network/main.tf

resource “aws\_vpc” “example” {  
# One VPC for each element of var.vpcs  
for\_each = var.vpcs

# each.value here is a value from var.vpcs

cidr\_block = each.value.cidr\_block  
}

resource “aws\_internet\_gateway” “example” {  
# One Internet Gateway per VPC  
for\_each = aws\_vpc.example

# each.value here is a full aws\_vpc object

vpc\_id = each.value.id  
}

/modules/network/variables.tf

variable “vpcs” {  
type = map

}

/modules/network/output.tf

output “vpc\_ids” {

```
value = {

       for k, v in aws_vpc.example : k => v.id

      }

    }

```

:::::::::::::::::  
Ec2 modules ( a very basic example only to his discusion  
:::::::::::::::::::  
/modules/ec2/main.tf

resource “aws\_instance” “ec2instance” {

```
ami = "ami-0d3c032f5934e1b41"
 instance_type = "t2.micro"

subnet_id = var.subnet_id

```

}

/modules/ec2/variables.tf

variable “subnet\_id” {  
type = string

}

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::  
:::next the main file

main.tf

(:: )

provider “aws” {

profile = “”

region = local.regiao

}

variable “vpcs” {

```
type = map(object({

  cidr_block = string

}))

default = {

  "PubSub1" = {

    cidr_block = "172.26.0.0/16"

  }

  "PubSub2" = {

    cidr_block = "172.16.13.0/24"

  }

}

```

}

```
      module "network" {

        source = "./modulos/redes"

        vpcs = var.vpcs

       

      }

      module "private_EC2" {
        source = "./modulos/ec2"
        
        
        subnet_id = "${module.network.vpc_ids}"
 
 
    
      }

```

* * *

question, i would like to send like a input value a list with all vpc id sent by : /modules/network/output.tf . but i don’t know how to do it.

* * *

terrafprm plan result

╷  
│ Error: Invalid value for module argument  
│  
│ on main.tf line 70, in module “private\_EC2”:  
│ 70: subnet\_id = “${module.network.vpc\_ids}”  
│  
│ The given value is not suitable for child module variable “subnet\_id” defined at modulos\ec2\variables.tf:1,1-21: string required.

* * *

So how can we handle with for each and output.tf values and after how can we get each values to bu used like input parameters, to other modules.

thanks a lot

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [January 4, 2022, 6:04pm UTC](https://discuss.hashicorp.com/t/for-each-loops-and-ouput-tf-files-and-how-to-get-output-values/33794/2 "2022-01-04T18:04:31Z")

</div>

Hi @jpor1974 ,  
it would really help if you edit and reformat code in your post properly.

In regards to the question, a) the EC2 module requires subnet\_id instead of an vpc\_id, b) the EC2 module variable subnet\_id is defined as a string, but you’re passing a different data type, c) I recommend also outputting vpc\_ids of the network module within the main module to understand the data structure built.
