Using Encoding Functions
1. Using base64encode and base64decode Functions
| Function | Signature | Description | Example |
|---|---|---|---|
| base64encode | base64encode(string) |
Encodes a string into base64 format | base64encode("hello world") |
| base64decode | base64decode(string) |
Decodes a base64-encoded string back to original | base64decode("aGVsbG8gd29ybGQ=") |
Example: Encoding and Decoding a Secret
resource "aws_ssm_parameter" "secret" {
name = "my-secret"
type = "SecureString"
value = base64encode("my-sensitive-data")
}
Example: Decoding a Base64 Value
output "decoded_secret" {
value = base64decode("aHR0cHM6Ly93d3cueW91dHViZS5jb20v")
}
2. Using jsonencode and jsondecode Functions
| Function | Signature | Description | Example |
|---|---|---|---|
| jsonencode | jsonencode(value) |
Encodes a value into a JSON string | |
| jsondecode | jsondecode(string) |
Decodes a JSON string into a Terraform value | jsondecode("{\"name\": \"example\", \"port\": 8080}") |
Example: Using jsonencode to format a map
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
tags = jsonencode({
Name = "ExampleBucket"
Environment = "Production"
})
}
Example: Using jsondecode to parse a JSON string
variable "bucket_tags" {
type = string
default = "{\"Name\": \"ExampleBucket\", \"Environment\": \"Production\"}"
}
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
tags = jsondecode(var.bucket_tags)
}
3. Using yamlencode and yamldecode Functions
| Function | Signature | Description | Example |
|---|---|---|---|
| yamlencode | yamlencode(any) |
Encodes a value into a YAML string | |
| yamldecode | yamldecode(string) |
Decodes a YAML string into a value | yamldecode("name: example\nport: 8080") |
Example: Using yamlencode to format data
resource "local_file" "config" {
content = yamlencode({
database = {
host = "localhost"
port = 5432
}
})
filename = "config.yaml"
}
Example: Using yamldecode to parse data
data "local_file" "config" {
filename = "config.yaml"
}
locals {
parsed_config = yamldecode(data.local_file.config.content)
}
4. Using csvdecode Function
| Function | Signature | Description | Examples |
|---|---|---|---|
| csvdecode | csvdecode(string) |
Decodes a CSV-formatted string into a list of lists of strings. | csvdecode("name,age\nJohn,30\nJane,25") |
Example: Decoding CSV Data
locals {
csv_data = "id,name,role\n1,John,Developer\n2,Jane,Designer"
decoded = csvdecode(local.csv_data)
}
output "first_row" {
value = local.decoded[0]
}
Note: The
csvdecode function expects a valid CSV string and will throw an error if the format is invalid.
5. Using urlencode Function
| Function | Signature | Description | Example |
|---|---|---|---|
| urlencode | urlencode(string) |
Encodes a string for use in a URL, replacing special characters with their percent-encoded equivalents. | urlencode("hello world") = "hello%20world" |
Example: Encoding a URL Parameter
resource "aws_s3_object" "my_object" {
bucket = "my-bucket"
key = "${urlencode(var.object_name)}"
}
Note: The
urlencode function is particularly useful when constructing URLs dynamically, ensuring that special characters like spaces or ampersands are properly encoded to avoid URL parsing errors.
6. Using textencodebase64 and textdecodebase64 Functions
| Function | Description | Example |
|---|---|---|
| textencodebase64 | Encodes a string into Base64 format. | textencodebase64("Hello, World!") |
| textdecodebase64 | Decodes a Base64-encoded string back to its original form. | textdecodebase64("SGVsbG8sIFdvcmxkIQ==") |
Example: Encoding and Decoding with Base64
resource "aws_s3_object" "encoded_data" {
bucket = aws_s3_bucket.example.id
key = "data.txt"
content = textencodebase64("Sensitive data to encode")
}
Example: Decoding Base64 Data
data "aws_s3_object" "decoded_data" {
bucket = aws_s3_bucket.example.id
key = "data.txt"
content = textdecodebase64(aws_s3_object.encoded_data.content)
}
7. Using base64gzip Function
| Function | Signature | Description | Example |
|---|---|---|---|
| base64gzip | base64gzip(string) |
Encodes a string in base64 after compressing it with gzip. | base64gzip("Hello, World!") |
Example: Encoding and Decoding with base64gzip
data "external" "decode_base64gzip" {
program = ["sh", "-c", "echo '${base64gzip("Hello, World!")}' | base64 -d | gzip -dc"]
}
Note: The
base64gzip function is useful for reducing the size of large strings or configuration data, particularly when storing them in variables or as part of a larger payload.
8. Using md5 and sha256 Functions
| Function | Description | Example | Notes |
|---|---|---|---|
| md5() | Calculates the MD5 hash of a given string | md5("hello world") |
Returns a 32-character hexadecimal string |
| sha256() | Calculates the SHA-256 hash of a given string | sha256("hello world") |
Returns a 64-character hexadecimal string |
Example: Using md5() in a variable
variable "input_string" {
type = string
default = "hello world"
}
output "md5_hash" {
value = md5(var.input_string)
}
Example: Using sha256() in a resource
resource "aws_s3_bucket" "example" {
bucket = "example-bucket-" + sha256(var.environment)
}
Note: These functions are deterministic and can be used for generating consistent identifiers or checksums. They are not cryptographically secure for security-sensitive applications.
9. Using uuid and uuidv5 Functions
| Function | Description | Example | Notes |
|---|---|---|---|
| uuid() | Generates a random UUID version 4 (RFC 4122). | |
Useful for generating unique identifiers for resources. |
| uuidv5() | Generates a UUID version 5 (SHA-1 namespace) based on input. | |
Ensures deterministic UUIDs for consistent resource naming. |
10. Using bcrypt Function
| Function | Signature | Description | Notes |
|---|---|---|---|
| bcrypt | bcrypt(password, cost) |
Hashes a password using the bcrypt algorithm | Useful for securely storing passwords in configuration files or state files. |
Example: Hashing a Password with bcrypt
resource "aws_iam_user" "user" {
name = "example-user"
}
resource "aws_iam_user_login_profile" "user_profile" {
user = aws_iam_user.user.name
password = bcrypt("mySecretPassword123", 12)
password_reset_required = false
}
Note: The
bcrypt function is available in Terraform v0.12.0 and later.