我正在尝试创建带有安全组的 VPC 并将其与 ec2 和 RDS 一起使用。
- 为 ec2 创建安全组 SG1,并开放端口 80
- 引用第一个安全组 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 plan 时,出现以下错误
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]
关于RDS 的 Terraform AWS 安全组条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65552221/