如何在“ final_snapshot_identifier”中使用函数。 “ aws_db_instance”

发布于 2025-01-25 14:28:16 字数 1476 浏览 3 评论 0 原文

我是Terraform的新手。我想使用Terraform构建RDS(MySQL),并制作其快照名称包含时间戳。我该如何编码才能意识到这一点?

我检查了有关 timestamp()函数的官方文件(),但是页面没有清楚地提及 in .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)

I am new to Terraform. I would like to build RDS (MySQL) using Terraform and make its snapshot name contain timestamp. How can I code to realize this?

I checked the official document about timestamp() function (https://www.terraform.io/language/functions/timestamp#examples), but the page does not clearly mention how to use timestamp() in .tf files.

Code

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 message

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2025-02-01 14:28:16

时间戳返回一个UTC时间戳字符串,例如 2022-05-02T05:20:12Z ,其中包括时间戳时间部分的结肠。

不允许结肠名称中。

您可以使用 formatdate 任何仅包括字母数字&的格式连字符。

例如,这应该有效:

final_snapshot_identifier = "db-dev-${formatdate('YYYYMMDDhhmmss', timestamp())}" 

timestamp returns a UTC timestamp string like 2022-05-02T05:20:12Z which includes colons for the time part of the timestamp.

Colons are not permitted to be in snapshot names.

You can use the formatdate function to format the timestamp to any format that only includes alphanumeric & hyphen characters.

For example, this should work:

final_snapshot_identifier = "db-dev-${formatdate('YYYYMMDDhhmmss', timestamp())}" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文