RDS 的 Terraform AWS 安全组条目

Terraform AWS Security group entries for RDS

我正在尝试创建带有安全组的 VPC,并将它们与 ec2 和 RDS 一起使用。

  1. 为打开端口 80 的 ec2 创建了安全组 SG1
  2. 参考第一个安全组 sg1 创建了安全组 rdssg

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"]

    }

当我 运行 terraform 计划时,我得到以下错误

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.

security_groups属性是一组安全组,所以你需要提供这样的值:

security_groups = [aws_security_group.sg1.id]