Encoding Function

Encoding function in Terraform is a built-in function used for the encoding or decoding of data by using various encoding schemes.

Types of Encoding Functions

Terraform provides the following types of Encoding functions:

1. base64decode Function

base64decode in Terraform is a function used to decode a string that's encoded in Base64 format. Base64 is a method through which binary data is represented as a text string, and that's useful for transmitting or storing data that can't easily be represented as text. Syntax:
base64decode(encodedString)
For example
terraform console
> base64decode("VGVycmFmb3Jt")
"Terraform"

2. base64encode Function

The base64encode function in Terraform is used to apply Base64 encoding to a string. The syntax is:
base64decode(string)
For example
terraform console
> base64encode("Terraform")
"VGVycmFmb3Jt"

3. base64gzip Function

base64gzip is a function that allows one to compress a string using gzip and then encode the result in Base64 format, which is useful when dealing with compressed data in Terraform, such as creating efficient object storage in cloud services. The syntax is
base64gzip(string)
For example
terraform console
> base64gzip("compressed terraform")
"H4sIAAAAAAAA/0rOzy0oSi0uTk1RKEktKkpMyy/KBQAAAP//AQAA//+02JavFAAAAA=="

4. csvdecode Function

The csvdecode function in Terraform will decode a string containing CSV formatted data and produce a list of maps representing that data. The syntax is
csvdecode(csvString)
For example
terraform console
> csvdecode("a,b,c\ngit,bitbucket,gitlab\nterraform,puppet,ansible")
tolist([
    {
        "a" = "git",
        "b" = "bitbucket",
        "c" = "gitlab",
    },
    {
        "a" = "terraform",
        "b" = "puppet",
        "c" = "ansible",
    },
])

5. jsondecode Function

The jsondecode function in Terraform is used to interpret a given string that is formatted as valid JSON and to convert it into a Terraform data structure, such as an object value. The syntax for using the jsondecode function is
jsondecode(jsonFormatedstring)
The jsondecode function uses the following rules to convert each value in jsonFormatedstring into the data type that Terraform supports:
JSON Datatype
Terraform Datatype
Number
Boolean
Object
Array
Null
For example
terraform console
> jsondecode("{\"name\": \"ajay\",\"age\": 23,\"info\":{\"phone-no\": 23487234,\"email\": \"[email protected]\"},\"ranks\":[1,34,21,2]}")
{
    "age" = 23
    "name" = "ajay"
    "ranks" = [
        1
        34
        21
        2
    ]
    "info" = {
        "email" = "[email protected]"
        "phone-no" = 23487234
    }
}
> type(jsondecode("{\"name\": \"ajay\",\"age\": 23,\"info\":{\"phone-no\": 23487234,\"email\": \"[email protected]\"},\"ranks\":[1,34,21,2]}"))
object({
    "age" = number
    "name" = string
    "ranks" = [
        number
        number
        number
        number
    ]
    "info" = {
        "email" = string
        "phone-no" = number
    }
})
Also, the jsondecode function in Terraform accepts number and boolean data types as input. For example
terraform console
> jsondecode(56)
56
> jsondecode(true)
true

6. jsonencode Function

The jsonencode function is used to encode a given a JSON value to a string using JSON syntax. The syntax is
jsonencode(JSON)
The following are the conversion rules the jsonencode function does when converting each value in JSON into a json-supported data type:
Terraform Datatype
JSON Datatype
Number
Boolean
Array
Object
Null
For example
terraform console
> jsonencode({"age" = 23,"name" = "ajay","ranks" = [134212],"info" = {"email" = "[email protected]","phone-no" = 23487234}})
"{\"age\":23,\"info\":{\"email\":\"[email protected]\",\"phone-no\":23487234},\"name\":\"ajay\",\"ranks\":[134212]}"

Related Pages

Feedback

Was this page helpful?