format

Stores small snippets of code or other types of data that can be easily referenced in the future

format

Used to format strings in a specific way that is used by the Terraform configuration files. It takes in a format string, which contains placeholders, and a set of arguments. The placeholders are replaced with the values of the arguments, according to the Terraform-specific format.

  • FMT is a string containing placeholders for the arguments in the Terraform format.
  • ARGS is one or more arguments to be formatted into the string.
  • formatted_string is the resulting string after the placeholders have been replaced with the arguments.
formatted_string = terraform.format(FMT, ARGS, ...)

Example

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = "${terraform.format("subnet-%s", var.subnet_id_suffix)}"
}

In this example, the terraform.format() method is used to format a string in the Terraform-specific format. The placeholder %s is used to indicate a string variable, and it is replaced with the value of var.subnet_id_suffix in the resulting string. The resulting string is then used as the value for the subnet_id argument in the aws_instance resource.

Last modified July 21, 2024: update (e2ae86c)