2
votes

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!

1

1 Answers

1
votes

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.