Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

amazon web services - Terraform add multiple security groups from a data function output

Having issues attempting to add three securiy groups to the ec2 instance below. How do I add the two shared-services-sg* from the data "aws_security_groups" list as well as newly created SG? The data aws_security_groups will return two security groups shared-services-sg1 and shared-services-sg2. Im also creating a new security group john_app_sec_group2.

data "aws_security_groups" "shared"{
    filter {
      name = "tag:Name"
      values = ["shared-services-sg*"]
    }
}

resource "aws_security_group" "john_app_sec_group2" {
  name   = "app_sec_group"
  vpc_id = aws_vpc.vpc_john.id

   #Allow HTTP from anywhere
  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

resource "aws_instance" "Server1" {
    instance_type = var.instance_type [0]
    ami = data.aws_ami.aws-linux.id
    subnet_id = aws_subnet.subnet1.id
    key_name = var.key_name
    vpc_security_group_ids = concat(
      aws_security_groups."shared-services-sg*"shared.ids,
      [aws_security_group.john_app_sec_group2.id]
    )
}

Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It should be:

    vpc_security_group_ids = concat(
      data.aws_security_groups.shared.ids,
      [aws_security_group.john_app_sec_group2.id]
    )

because you have one data source called shared which returns multiple ids.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...