Home > Software engineering >  creating security group rules, iteratively from a list of aws_security_groups datasource
creating security group rules, iteratively from a list of aws_security_groups datasource

Time:05-26

I am still new on Terraform.

I am trying to create a single security group rule in order to whitelist a single IP, onto a set of existing security groups returned by data source "aws_security_groups".

data "aws_security_groups" "test" {
 filter {
   name   = "group-name"
   values = ["*VPN*"]
 }
}

resource "aws_security_group_rule" "this" {

  type = "ingress"
  security_group_id = ["for id in data.aws_security_groups.test.ids : id"] //need to get from aws_security_groups datasource
  from_port = 1
  to_port = 1
  protocol = -1
}

however I could not get it right, the tricky part is with the "security_group_id"

can please suggest any methods without using external modules? thanks.

CodePudding user response:

ids is a list. So you can use for_each:

resource "aws_security_group_rule" "this" {

  for_each = toset(data.aws_security_groups.test.ids)

  type = "ingress"
  security_group_id = each.key
  from_port = 1
  to_port = 1
  protocol = -1
}
  • Related