Search code examples
amazon-web-servicesterraformaws-security-group

putting security group ids of a vpc in a list using terraform


While trying to create vpc endpoint , i have to dynamically create the security groups within the vpc and then attach it to the vpc endpoints in the same terraform plan . Is there a way I can put all the security group ids of a VPC in a list using terraform?


Solution

  • Is there a way I can put all the security group ids of a VPC in a list using terraform?

    Yes, you can use aws_security_groups data source:

    data "aws_security_groups" "test" {
      filter {
        name   = "vpc-id"
        values = ["your-vpc-id"]
      }
    }
    
    output "test" {
      value = data.aws_security_groups.test.ids
    }