Terraform InvalidParameterCombination:数据库安全组只能与 VPC 数据库实例相关联

Terraform InvalidParameterCombination: DB Security Groups can only be associated with VPC DB Instances

我正在尝试使用 Terraform 模板配置 RDS 实例,我的模板如下所示

template.tf

  resource "aws_security_group" "web-server-security"{
  name = "webserver-sg"
  description = "webserver security group"
  ingress {
  from_port = 80
  to_port = 80
  protocol = "tcp"
  cidr_blocks = ["0.0.0.0/0"]
}
tags{
  Name = "web-server-sg"
}

resource "aws_security_group" "db-server-sg" {
  name = "db-server"
  description = "dbserver security group"
  ingress {
  from_port = 3306
  to_port = 3306
  protocol = "tcp"
  security_groups = ["${aws_security_group.web-server-security.id}"]
 }
  tags{
   Name = "db-server-sg"
  }
 }

resource "aws_db_instance" "echomany_db" {
   name = "echomanydb"
   engine = "mysql"
   engine_version = "5.7"
   storage_type = "gp2"
   allocated_storage = 20
   instance_class = "db.t2.micro"
   username = "${var.AWS_DB_USERNAME}"
   password = "${var.AWS_DB_PASSWORD}"
   parameter_group_name = "default.mysql5.7"
   skip_final_snapshot = true
   security_group_names = [
   "${aws_security_group.db-server-sg.id}"
  ]
  tags{
    Name = "db-server"
  }
 }

但是我收到以下错误:

发生 1 个错误: * aws_db_instance.echomany_db:发生 1 个错误:

我不知道问题出在哪里以及如何解决这个问题。

如文档所述vpc_security_group_ids should be used instead of security_group_names,这是一个已弃用的论点。

名为

的参数

security_group_names = [ "${aws_security_group.db-server-sg.id}" ]

只能在使用 ec2-classic 模式或在 VPC 外部使用。 请改用 vpc_security_group_ids

您可以使用 vpc_security_groups_ids = [ ] 而不是 security_group_names 因为它只能在使用 ec2-classic 模式时使用

示例:

vpc_security_group_ids=["${aws_security_group.rds.id}"]