如何使用 "final_snapshot_identifier" of "aws_db_instance" 中的函数?

How to use a function in "final_snapshot_identifier" of "aws_db_instance"?

我是 Terraform 的新手。我想使用 Terraform 构建 RDS (MySQL) 并使其快照名称包含时间戳。我如何编写代码来实现这一点?

我查看了关于timestamp()函数的官方文档(https://www.terraform.io/language/functions/timestamp#examples),但是页面没有明确提到如何在.tf文件中使用timestamp()

代码

resource "aws_db_instance" "db-dev" {
    identifier                = "db-dev"
    allocated_storage         = 30
    storage_type              = "gp2"
    engine                    = "mysql"
    engine_version            = "8.0.27"
    instance_class            = "db.t3.micro"
    db_name                   = "test"
    username                  = "admin"
    password                  = "admin"
    port                      = 3306
    publicly_accessible       = false
    availability_zone         = "${var.az_1_private}"
    security_group_names      = []
    vpc_security_group_ids    = ["${aws_security_group.sg-1-dev.id}"]
    db_subnet_group_name      = "db-dev-subnet"
    parameter_group_name      = "db-dev-parameter"
    multi_az                  = false
    backup_retention_period   = 0
    backup_window             = "04:30-05:00"
    maintenance_window        = "wed:01:00-wed:03:00"
    final_snapshot_identifier = "db-dev-${timestamp()}"
}

错误信息

Error: invalid value for final_snapshot_identifier (must only contain alphanumeric characters and hyphens)

timestamp returns 像 2022-05-02T05:20:12Z 这样的 UTC 时间戳字符串,其中包括时间戳的时间部分的冒号。

快照名称中不允许使用冒号。

您可以使用 formatdate 函数将时间戳格式化为任何仅包含字母数字和连字符的格式。

例如,这应该有效:

final_snapshot_identifier = “db-dev-${formatdate(“YYYMMDDhhmmss”, timestamp())}”