1
votes

I am trying to create VPC with security groups and use them with ec2 and RDS.

  1. Created security group SG1 for ec2 with port 80 open
  2. Created security group rdssg with reference to first security group sg1

resource "aws_vpc" "dev-vpc" {
    cidr_block = var.vpc_cidr
    enable_dns_hostnames = true
    tags = {
        Name = "Dev-VPC"
    }
}

resource "aws_security_group" "sg1" {
    name = "sg1"
    vpc_id =  aws_vpc.dev-vpc.id

    ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }

    egress {
        from_port = 0
        to_port = 0
        protocol = "-1"
        cidr_blocks = ["0.0.0.0/0"]

    }

}

resource "aws_security_group" "rdssg" {
    name = "rdssg"
    vpc_id =  aws_vpc.dev-vpc.id

    ingress {
        from_port = 3306
        to_port = 3306
        protocol = "tcp"
        security_groups = aws_security_group.sg1.id

    }

    egress {
        from_port = 0
        to_port = 0
        protocol = "-1"
        cidr_blocks = ["0.0.0.0/0"]

    }

When I run terraform plan, I get following error

Error: Incorrect attribute value type

  on ../module/vpc/vpc.tf line 152, in resource "aws_security_group" "rdssg":
 152:         security_groups = aws_security_group.sg1.id

Inappropriate value for attribute "security_groups": set of string required.
``

Not able to understand the error . Appreciate the help.
1

1 Answers

1
votes

security_groups attribute is a set of security groups, so you need to provide the value like this:

security_groups = [aws_security_group.sg1.id]