Hi, I need help in knowing how can I use local variable with multiple key and values to a resource using for_each, I have created Subnets Dynamically using Map variable and far each with Module. Now I want to create Interfaces which is into different module, I need subnet-id to be used from vpc module to the ec2 module for multiple interface creations.
Below are the code:
#CSR Interfaces Creation
resource "aws_network_interface" "csr_inf" {
for_each = local.interface
subnet_id = each.value.inf_subnet_id
private_ips = [each.value.inf_private_ip]
security_groups = [aws_security_group.vpn_role_csr.id]
tags = { "Name" = format("%s-%s", var.vpc_name, each.value.inf_name) }
}
Local Variable into Variables.tf file
locals {
interface = {
"CSR1000-Ext" = {
inf_subnet_id = module.csr_vpn_vpc.subnet_id.sub-1
inf_private_ip = "192.168.0.11"
inf_name = "CSR1000-External"
},
"CSR1001-Ext" = {
inf_subnet_id = module.csr_vpn_vpc.subnet_id.sub-2
inf_private_ip = "192.168.0.141"
inf_name = "CSR1001-External"
},
}
}
I am getting below error:
Error: Unsupported attribute
on ../aws-modules/ec2/instances/main.tf line 67, in resource "aws_network_interface" "csr_inf":
67: subnet_id = each.value.inf_subnet_id
|----------------
| each.value is "subnet-03e29c398eaf41c62"
This value does not have any attributes.
Error: Unsupported attribute
on ../aws-modules/ec2/instances/main.tf line 67, in resource "aws_network_interface" "csr_inf":
67: subnet_id = each.value.inf_subnet_id
|----------------
| each.value is "CSR1000-External"
This value does not have any attributes.
Error: Unsupported attribute
on ../aws-modules/ec2/instances/main.tf line 67, in resource "aws_network_interface" "csr_inf":
67: subnet_id = each.value.inf_subnet_id
|----------------
| each.value is "192.168.0.11"
This value does not have any attributes.
Error: Unsupported attribute
on ../aws-modules/ec2/instances/main.tf line 68, in resource "aws_network_interface" "csr_inf":
68: private_ips = [each.value.inf_private_ip]
|----------------
| each.value is "subnet-03e29c398eaf41c62"
This value does not have any attributes.
Any Idea how can I call Local variable value to the resource section using for_each?
Since Variable interpolation is not supported in terraform, we can’t use Variable input in this scenario as I need subnet-id to be called from the module output.
Any help on this will be appreciated.
Thanks