我有以下(简化的)Terraform 代码:
variable "cluster_id" {
default = 1
}
resource "aws_instance" "instance" {
... some instance properties ...
tags {
"Name" = "${format("cluster-%02d", var.cluster_id)}"
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行terraform apply计划时显示:
tags.Name: "%!d(string=1)"
Run Code Online (Sandbox Code Playgroud)
将cluster_id在format()这样格式化失败并不像一些处理。我希望我得到,cluster-01但事实并非如此。
我做错了什么还是真的不可能在格式中使用自定义变量作为数字?
0.12 之前的 Terraform 仅支持string,list和maptypes 作为输入变量,因此尽管您提供了integer( 或 afloat或 a boolean),它仍将被强制转换为 a string。
Terraform和 Go 都允许您对整数和字符串使用相同的填充,因此您只需使用以下内容来填充 0 cluster_id:
resource "aws_instance" "instance" {
# ... some instance properties ...
tags {
"Name" = "${format("cluster-%02s", var.cluster_id)}"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4407 次 |
| 最近记录: |