Familiarity with common use cases for Terraform built-in functions

A quick tutorial on using Terraform to learn and apply the different types of built-in functions, including Numeric, String, and Date, and Time in this IaC tool.







IT teams can use HashiCorp's Terraform to deploy and manage cloud and on-premises resources. To optimize your tool usage, learn and install Terraform features.







Administrators can use built-in Terraform functions to perform various math related to deployment and to perform operations such as encoding and decoding or capturing and displaying timestamps. Terraform only supports built-in functions ; special or user-defined functions are not available.







Use this Terraform tutorial to learn the basics of features, as well as some common ways to use them in enterprise deployments.







Let's start with the syntax



Terraform function syntax begins with the function name, followed by parentheses containing zero to multiple arguments, separated by commas:







name(arg-1, arg-2, â€Ļ arg-n)
      
      





, , timestamp()



, :







> timestamp()
2019-12-07T07:44:12Z
      
      





, file()



:













Terraform



Terraform , . $ {builtinfunction ()}



, :







resource "myinstance" "web" {
    tags = ["${var.env == "prod" ? var.prod_subnet : var.dev_subnet}"]
}
      
      





Terraform, . Terraform, .







. Terraform environment



, production, AWS (AZs). availzone



, , , us-east-1a



, us-east-1b



us-east-1c



. , , :







variable "environment" {
default = {
    "test" = "us-east-1"
    "prod" = "us-west-2"
  }
}

variable "availzone" {
  description = "Availability Zones Mapping"
  default = {
    "us-east-1" = "us-east-1a,us-east-1b,us-east-1c"
    "us-west-2" = "us-west-2a,us-west-2b,us-east-1c"
  }
}
      
      





AZ, lookup()



, , , (,)



split()



.







, element()



. AZ . $ {}



, , Terraform , .







output "availabiltyzones" {
  value = "${element(split(",", lookup(var.availzone,var.environment.prod)), 1)}"
}
      
      





, :













Terraform



Terraform .













, , , / , :



















Terraform . , title()



, lower()



upper()



:













- , :



















Terraform. , timestamp()



:













formatdate()



, :



















Terraform , . , base64encode('string')



Base64, Azure, Base64 . file()



, Base64encode()



:







resource "azurerm_virtual_machine_extension" "Example" {
  name                 = "MyVM"
  location             = "${azurerm_resource_group.test.location}"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"
  settings = <
      
      





(Collections)







Terraform , , :







length([1,12,31,14,5,2])
length("this is a string")
      
      





, , :














All Articles