I’m trying to extract only particular indexes from the list.
Module Block
module "app_sg" {
source = "../modules/sg/sg_rules"
ingress_rules = var.app_ingress_rules
egress_rules = var.app_egress_rules
ingress_sg = var.app_ingress_sg
egress_sg = var.app_egress_sg
src_sg_id = ["module.app_sgs.sg_id[0]", "module.app_sgs.sg_id[2]"]
sg_id = module.app_sgs.sg_id[0]
depends_on = [module.app_sgs]
}
Resource Block
# Application security group rules creation
resource "aws_security_group_rule" "ingress_cidr" {
for_each = var.ingress_rules != "" ? var.ingress_rules : {}
type = "ingress"
from_port = each.key
to_port = each.key
protocol = "tcp"
security_group_id = var.sg_id
cidr_blocks = each.value
}
resource "aws_security_group_rule" "egress_cidr" {
for_each = var.egress_rules != "" ? var.egress_rules : {}
type = "egress"
from_port = each.key
to_port = each.key
protocol = "tcp"
security_group_id = var.sg_id
cidr_blocks = each.value
}
resource "aws_security_group_rule" "ingress_sg" {
for_each = var.ingress_sg != "" ? var.ingress_sg : {}
type = "ingress"
from_port = each.key
to_port = each.key
protocol = "tcp"
security_group_id = var.sg_id
source_security_group_id = var.src_sg_id
}
resource "aws_security_group_rule" "egress_sg" {
for_each = var.egress_sg != "" ? var.egress_sg : {}
type = "egress"
from_port = each.key
to_port = each.key
protocol = "tcp"
security_group_id = var.sg_id
source_security_group_id = var.src_sg_id
}
While running terraform apply
it throws an error like below
Error: Incorrect attribute value type
on ../modules/sg/sg_rules/main.tf line 29, in resource "aws_security_group_rule" "ingress_sg":
29: source_security_group_id = var.src_sg_id
|----------------
| var.src_sg_id is tuple with 2 elements
Inappropriate value for attribute "source_security_group_id": string required.
Could someone please provide me a solution for this scenario? Eventually I need to pass multiple source_security_group_id
within the security group rule. I have tried slice
function, but it can extract only the start
and end
indexes.