Hi everyone, I need to add a dependency from module sg1.id to module sg2 but I have this error.
Error: Invalid value for input variable
│
│ on security_groups.tf line 62, in module “security_groups_2”:
│ 62: ingress_with_source_security_group_id = [for k in module.security_groups_1 : k.security_group_id]
│
│ The given value is not suitable for module.security_groups_2[“sg2”].var.ingress_with_source_security_group_id
│ declared at .terraform/modules/security_groups_2/variables.tf:97,1-49: element 0: map of string required.
Can you explain me as could make this? This is my code.
module "security_groups_1" {
source = "terraform-aws-modules/security-group/aws"
version = "5.1.0"
for_each = var.sg1
name = each.value["name"]
description = each.value["description"]
vpc_id = data.aws_vpc.vpc_datasource.id
ingress_with_cidr_blocks = each.value["ingress_with_cidr_blocks"]
egress_with_cidr_blocks = each.value["egress_with_cidr_blocks"]
ingress_with_source_security_group_id = each.value["ingress_with_source_security_group_id"]
egress_with_source_security_group_id = each.value["egress_with_source_security_group_id"]
tags = merge({
}, local.tags)
}
module "security_groups_2" {
source = "terraform-aws-modules/security-group/aws"
version = "5.1.0"
for_each = var.sg2
name = each.value["name"]
description = each.value["description"]
vpc_id = data.aws_vpc.vpc_datasource.id
ingress_with_cidr_blocks = each.value["ingress_with_cidr_blocks"]
egress_with_cidr_blocks = each.value["egress_with_cidr_blocks"]
ingress_with_source_security_group_id = [for k in module.security_groups_1 : k.security_group_id]
egress_with_source_security_group_id = each.value["egress_with_source_security_group_id"]
tags = merge({
}, local.tags)
}
sg1 = {
"sg1" = {
name = "sg1"
description = "security group for database"
ingress_with_cidr_blocks =[]
egress_with_cidr_blocks =[
{
from_port = -1
to_port = -1
protocol = -1
description = "Allow all outbound traffic by default"
cidr_blocks = "0.0.0.0/0"
}
]
ingress_with_source_security_group_id = [
{
from_port = 2049
to_port = 2049
protocol = "tcp"
description = "from 1.2.3.4/5:443"
source_security_group_id = "sg-1234abcd"
}
]
egress_with_source_security_group_id = []
},
}
sg2 = {
"sg2" = {
name = "sg2"
description = "security group for database"
ingress_with_cidr_blocks =[]
egress_with_cidr_blocks =[
{
from_port = -1
to_port = -1
protocol = -1
description = "Allow all outbound traffic by default"
cidr_blocks = "0.0.0.0/0"
}
]
ingress_with_source_security_group_id = [
{
from_port = 2049
to_port = 2049
protocol = "tcp"
description = "from 1.2.3.4/5:443"
source_security_group_id = null
}
]
egress_with_source_security_group_id = []
},
}
variable "sg1" {
type = map(object({
name = string
description = string
ingress_with_cidr_blocks = list(object({
from_port = number
to_port = number
protocol = string
description = string
cidr_blocks = string
}))
egress_with_cidr_blocks = list(object({
from_port = number
to_port = number
protocol = string
description = string
cidr_blocks = string
}))
ingress_with_source_security_group_id = list(object({
from_port = number
to_port = number
protocol = string
description = string
source_security_group_id = string
}))
egress_with_source_security_group_id = list(object({
from_port = number
to_port = number
protocol = string
description = string
source_security_group_id = string
}))
}))
# default = {}
}
variable "sg2" {
type = map(object({
name = string
description = string
ingress_with_cidr_blocks = list(object({
from_port = number
to_port = number
protocol = string
description = string
cidr_blocks = string
}))
egress_with_cidr_blocks = list(object({
from_port = number
to_port = number
protocol = string
description = string
cidr_blocks = string
}))
ingress_with_source_security_group_id = list(object({
from_port = number
to_port = number
protocol = string
description = string
source_security_group_id = any
}))
egress_with_source_security_group_id = list(object({
from_port = number
to_port = number
protocol = string
description = string
source_security_group_id = string
}))
}))
default = {}
}