【问题标题】:Error on creation AWS DB Instance via Terraform通过 Terraform 创建 AWS 数据库实例时出错
【发布时间】:2021-03-16 23:36:42
【问题描述】:

我正在尝试通过 Terraform 创建一个新的 AWS RDS 实例,但我收到了一个奇怪的错误,如下 sn-p:

resource "aws_db_subnet_group" "sng_marketplace_stg" {
  name     = "sng-marketplace-stg"

  tags = { Name = "Marketplace Staging" }

  subnet_ids = [
    "${aws_subnet.sbn_1_us.id}",
    "${aws_subnet.sbn_2_us.id}"
  ]
}

resource "aws_db_instance" "marketplace_staging" {
  identifier                  = "db-marketplace-stg-staging"
  engine                      = "postgres"
  engine_version              = "12.4"
  # ...
  # omitted
  # ...
  db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
}

    1. 第1组创建子网组sng-marketplace-stg这一步就ok了,TF可以申请了。
    1. 第二步引发错误Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'sng-marketplace-stg' not found.

好吧,当我访问 AWS 控制台 RDS 时,子网组已按预期创建,但如果我重新运行 TF 应用(或计划),则 terraform 会显示正确的更改(创建新的 RDS 实例),但应用时,再次引发错误,例如子网组不存在。

之前有人看到过这个错误或有任何修复它的建议吗?

感谢 4 任何帮助或解决问题的指导。


这里是完整的错误跟踪:

Error: Error applying plan:

1 error occurred:
        * aws_db_instance.marketplace_staging: 1 error occurred:
        * aws_db_instance.marketplace_staging: Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'sng-marketplace-stg' not found.
        status code: 404, request id: da567898-...

【问题讨论】:

  • 您提供的代码是正确的。我看不出它不起作用的原因。您确定您给出的 sn-ps 代表您的实际代码吗?
  • 是的,代码是正确的,但一个原因(我不知道)AWS Terraform 无法识别资源。

标签: amazon-web-services terraform amazon-rds


【解决方案1】:

我的代码遇到了同样的问题,我意识到您必须在 main.tf 文件中添加资源 aws_db_parameter_group。

这是从official website of terraform中提取的示例

resource "aws_db_parameter_group" "education" {
  name   = "education"
  family = "postgres13"

  parameter {
    name  = "log_connections"
    value = "1"
  }
}

然后,我运行 terraform plan 和 terraform apply 并且一切正常(确保您的 IAM 角色具有所需的所有权限,您可以在 terraform 控制台中出现与权限相关的错误消息时添加它们)。

祝你好运!

【讨论】:

    【解决方案2】:

    你必须创建 db 参数组。

    resource "aws_db_parameter_group" "education" {
      name   = "education"
      family = "postgres13"
    
      parameter {
        name  = "log_connections"
        value = "1"
      }
    }
    

    您还必须在创建数据库实例时引用它。

    在你的情况下。

    resource "aws_db_instance" "marketplace_staging" {
      identifier                  = "db-marketplace-stg-staging"
      engine                      = "postgres"
      engine_version              = "12.4"
      # ...
      # omitted
      # ...
      db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
      parameter_group_name        = aws_db_parameter_group.education.name
    }
    

    如果您想允许流量,请进一步了解。添加引用您的 vpc id 的安全组。

    resource "aws_security_group" "rds" {
      name   = "education_rds"
      vpc_id = module.vpc.vpc_id
    
      ingress {
        from_port   = 5432
        to_port     = 5432
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
    
      egress {
        from_port   = 5432
        to_port     = 5432
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
    
      tags = {
        Name = "education_rds"
      }
    }
    

    并在创建数据库时引用它。在你的情况下。

    resource "aws_db_instance" "marketplace_staging" {
      identifier                  = "db-marketplace-stg-staging"
      engine                      = "postgres"
      engine_version              = "12.4"
      # ...
      # omitted
      # ...
      db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
      parameter_group_name        = aws_db_parameter_group.education.name
      vpc_security_group_ids      = [aws_security_group.rds.id]
    }
    

    参考:https://github.com/hashicorp/learn-terraform-rds/blob/main/main.tf

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 2018-11-17
      • 2019-06-14
      • 2021-03-02
      • 2021-09-05
      • 2021-06-17
      • 2018-09-26
      • 1970-01-01
      • 2020-04-27
      相关资源
      最近更新 更多