如何使用 terraform 在 AWS 中备份 RDS 数据库但在每次应用后不销毁它

How to backup a RDS database in AWS using terraform but not destroying it after each apply

我想使用 terraform 在我的 RDS 数据库中进行自动备份。我已经这样做了:

`

resource "aws_db_instance" "main" {

    snapshot_identifier = data.aws_db_snapshot.from[0].id
    identifier          = "${local.prefix}-db"
    storage_type = "gp2"
    instance_class       = "db.t2.micro"
    db_subnet_group_name = aws_db_subnet_group.main.name
    backup_retention_period = 7
    multi_az                = false
    skip_final_snapshot     = true
    vpc_security_group_ids  = [aws_security_group.rds.id]

    tags = merge(
        local.common_tags,
        tomap({ "Name" = "${local.prefix}-main" })
  )
}

data "aws_db_snapshot" "from" {

    count                  = length("test-dev-db") > 0 ? 1 : 0
    most_recent            = true
    db_instance_identifier = "test-db"

}

`

问题在于,在我执行的每个应用程序中,数据库都会被删除并重新创建。那么我该如何避免呢?

谢谢

伙计们,我已经用这个解决了这个问题:

        lifecycle {
        ignore_changes = [snapshot_identifier]
        }

使用这个 meta-argument 它对我有用。我从这里拿走了它: https://www.terraform.io/language/meta-arguments/lifecycle