Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform timestamp() to numbers only string

Tags:

terraform

hcl

The timestamp() function in the interpolation syntax will return an ISO 8601 formatted string, which looks like this 2019-02-06T23:22:28Z. However, I want to have a string which looks like this 20190206232240706500000001. A string with only numbers (integers) and no hyphens, white spaces, colon, Z or T. What is a simple and elegant way to achieve this?

It works if I replace every a single character class at the time hyphens, white spaces, colon Z and T:

locals {
  timestamp = "${timestamp()}"
  timestamp_no_hyphens = "${replace("${local.timestamp}", "-", "")}"
  timestamp_no_spaces = "${replace("${local.timestamp_no_hyphens}", " ", "")}"
  timestamp_no_t = "${replace("${local.timestamp_no_spaces}", "T", "")}"
  timestamp_no_z = "${replace("${local.timestamp_no_t}", "Z", "")}"
  timestamp_no_colons = "${replace("${local.timestamp_no_z}", ":", "")}"
  timestamp_sanitized = "${local.timestamp_no_colons}"
}

output "timestamp" {
  value = "${local.timestamp_sanitized}"
}

The resulting output is in the desired format, except the string is significantly shorter:

Outputs:

timestamp = 20190207000744

However, this solution is very ugly. Is there another way of doing the same thing in a more elegant way as well as producing a string with the same length as the example string 20190206232240706500000001?

like image 426
Jeeppler Avatar asked Feb 07 '19 00:02

Jeeppler


People also ask

What is the format of timestamps in TerraForm?

timestamp returns a UTC timestamp string in RFC 3339 format. In the Terraform language, timestamps are conventionally represented as strings using RFC 3339 "Date and Time format" syntax, and so timestamp returns a string in this format.

What types can be converted to numbers in TerraForm?

Explicit type conversions are rarely necessary in Terraform because it will convert types automatically where required. Use the explicit type conversion functions only to normalize types returned in module outputs. Only numbers, null, and strings containing decimal representations of numbers can be converted to number.

How to generate random number or string in TerraForm?

If you need to generate a random number or string in Terraform, then you can use the following: resource "random_id" "myrandom" { keepers = { first = "$ {timestamp ()}" } byte_length = 8 } Loading... Terraform state reflects the most up to date reference of the infrastructure.

What is timeadd in TerraForm?

timeadd adds a duration to a timestamp, returning a new timestamp. In the Terraform language, timestamps are conventionally represented as strings using RFC 3339 "Date and Time format" syntax. timeadd requires the timestamp argument to be a string conforming to this syntax.


1 Answers

Terraform 0.12.0 introduced a new function formatdate which can make this more readable:

output "timestamp" {
  value = formatdate("YYYYMMDDhhmmss", timestamp())
}

At the time of writing, formatdate's smallest supported unit is whole seconds, so this won't give exactly the same result as the regexp approach, but can work if the nearest second is accurate enough for the use-case.

like image 186
Martin Atkins Avatar answered Sep 30 '22 19:09

Martin Atkins