Terraform Version
Terraform v0.12.20
- provider.aws v2.61.0
...
Terraform Configuration Files
locals {
vpc = csvdecode(file("./vpc.csv"))
subnet = csvdecode(file("./subnet.csv"))
}
resource "aws_vpc" "main" {
for_each = { for inst in local.vpc : inst.local_id => inst }
cidr_block = each.value.CIDR_Block
enable_dns_support = each.value.DNS_Support
enable_dns_hostnames = each.value.DNS_Hostnames
tags = {
Name = each.value.Name
Environment = each.value.Environment
}
}
resource "aws_subnet" "main" {
for_each = { for inst in local.subnet : inst.subnet_id => inst }
vpc_id = aws_vpc.main[each.key]
cidr_block = each.value.Subnets
tags = {
Name = each.value.Subnet_Names
}
}
Error Message:
Error: Incorrect attribute value type
on vpc.tf line 22, in resource "aws_subnet" "main":
22: vpc_id = aws_vpc.main[each.key]
|----------------
| aws_vpc.main is object with 1 attribute "1"
| each.key is "1"
Inappropriate value for attribute "vpc_id": string required.
Error: Invalid index
on vpc.tf line 22, in resource "aws_subnet" "main":
22: vpc_id = aws_vpc.main[each.key]
|----------------
| aws_vpc.main is object with 1 attribute "1"
| each.key is "2"
The given key does not identify an element in this collection value.
Error: Invalid index
on vpc.tf line 22, in resource "aws_subnet" "main":
22: vpc_id = aws_vpc.main[each.key]
|----------------
| aws_vpc.main is object with 1 attribute "1"
| each.key is "3"
The given key does not identify an element in this collection value.