I have a following (simplified) Terraform code:
variable "cluster_id" {
default = 1
}
resource "aws_instance" "instance" {
... some instance properties ...
tags {
"Name" = "${format("cluster-%02d", var.cluster_id)}"
}
}
And when I run terraform apply
the plan shows:
tags.Name: "%!d(string=1)"
The cluster_id
in format()
is not handled as a number so formatting fails. I would expect that I get cluster-01
but that's not the case.
Am I doing something wrong or is it really not possible to use custom variables as numbers in formatting?
Terraform, pre 0.12, only supports string
, list
and map
types as an input variable so despite you providing an integer
(or a float
or a boolean
) it will be cast to a string
.
Both Terraform and Go allow you to use the same padding for integers and strings though so you can just use the following to 0 pad the cluster_id
:
resource "aws_instance" "instance" {
# ... some instance properties ...
tags {
"Name" = "${format("cluster-%02s", var.cluster_id)}"
}
}
Another option I found is to do ${format("cluster-%02d", var.cluster_id+0)}
. Adding zero produces real number and then %02d
works correctly. But using %02s
is cleaner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.