Transform Inputs with Built-in Functions
HCL provides a rich suite of built-in functions to transform strings, query list lengths, manipulate delimeter strings, retrieve map values safely, and set evaluation fallbacks without external scripts.
Core Implementation Highlights
The Complete Terraform Code
Save the code below into main.tf. It demonstrates string casing, collection inspection, string joins/splits, map lookups, and error fallback evaluation using built-in Terraform functions.
# Uppercase function
resource "aws_s3_bucket" "uppercasefunction" {
bucket = "shan-${upper("uppercasefunction")}-bucket"
}
# Lowercase function
resource "aws_s3_bucket" "lowercasefunction" {
bucket = "shan-${lower("LOWERCASEFUNCTION")}-bucket"
}
# Length function
variable "lengthfunction" {
default = ["web", "db"]
}
resource "aws_instance" "webserver" {
count = length(var.lengthfunction)
ami = "ami-xxxxxxxxxxxxxxxxx"
instance_type = "t2.micro"
associate_public_ip_address = true
}
# Join function
resource "aws_s3_bucket" "joinfunction" {
bucket = join("-", ["shan", "joinfunction", "bucket"])
}
output "joinfunctionoutput" {
value = aws_s3_bucket.joinfunction.bucket
}
# Split function
resource "aws_s3_bucket" "splitfunction" {
bucket = join("-", split(",", "shan,split,function,bucket"))
}
output "splitfunctionoutput" {
value = aws_s3_bucket.splitfunction.bucket
}
# Lookup function
variable "lookupfunction" {
default = {
dev = "ami-0c02fb55956c7d316"
prod = "ami-0abcdef1234567890"
}
}
resource "aws_instance" "instanceforlookup" {
ami = lookup(var.lookupfunction, "dev")
instance_type = "t2.micro"
}
# Try function
variable "tryfunction" {
default = {
dev = "ami-0c02fb55956c7d316"
}
}
resource "aws_instance" "instancefortry" {
ami = try(
var.tryfunction["prod"],
var.tryfunction["dev"],
"ami-default"
)
instance_type = "t2.micro"
}Resource-by-Resource Explanation
1. upper() & lower()
Converts all characters in a string to uppercase or lowercase:
upper("uppercasefunction")converts"uppercasefunction"to"UPPERCASEFUNCTION".lower("LOWERCASEFUNCTION")converts"LOWERCASEFUNCTION"to"lowercasefunction".
resource "aws_s3_bucket" "uppercasefunction" {
bucket = "shan-${upper("uppercasefunction")}-bucket"
}
resource "aws_s3_bucket" "lowercasefunction" {
bucket = "shan-${lower("LOWERCASEFUNCTION")}-bucket"
}2. length()
Returns the number of elements in a list, map, or string length. Here, length(var.lengthfunction) counts 2 items (["web", "db"]), automatically setting EC2 count = 2.
variable "lengthfunction" {
default = ["web", "db"]
}
resource "aws_instance" "webserver" {
count = length(var.lengthfunction)
ami = "ami-xxxxxxxxxxxxxxxxx"
instance_type = "t2.micro"
}3. join() & split()
Manipulates delimited lists and strings:
join("-", [...])concatenates list items with a hyphen separator into a single bucket name string.split(",", "shan,split,function,bucket")splits comma-separated text into a list, which is then joined with hyphen delimiters.
resource "aws_s3_bucket" "joinfunction" {
bucket = join("-", ["shan", "joinfunction", "bucket"])
}
resource "aws_s3_bucket" "splitfunction" {
bucket = join("-", split(",", "shan,split,function,bucket"))
}4. lookup()
Retrieves the value of a single key from a map variable. lookup(var.lookupfunction, "dev") searches var.lookupfunction map for key "dev", returning "ami-0c02fb55956c7d316".
variable "lookupfunction" {
default = {
dev = "ami-0c02fb55956c7d316"
prod = "ami-0abcdef1234567890"
}
}
resource "aws_instance" "instanceforlookup" {
ami = lookup(var.lookupfunction, "dev")
}5. try()
Evaluates a list of expressions in order and returns the first one that does not produce an error:
- 1st attempt:
var.tryfunction["prod"](Fails because"prod"key is missing). - 2nd attempt:
var.tryfunction["dev"](Succeeds! Returns"ami-0c02fb55956c7d316"). - 3rd attempt:
"ami-default"(Fallback default if all prior items fail).
resource "aws_instance" "instancefortry" {
ami = try(
var.tryfunction["prod"],
var.tryfunction["dev"],
"ami-default"
)
instance_type = "t2.micro"
}Testing & Verification Commands
You can test Terraform built-in functions interactively using terraform console or run a full plan evaluation:
# 1. Test expressions interactively using Terraform Console
$ terraform console
> upper("uppercasefunction")
"UPPERCASEFUNCTION"
> join("-", ["shan", "joinfunction", "bucket"])
"shan-joinfunction-bucket"
> exit
# 2. Initialize provider plugins
$ terraform init
# 3. Dry-run plan evaluation to preview evaluated function values
$ terraform plan