Sling Academy
Home/DevOps/JSON encoding/decoding in Terraform: Explained with examples

JSON encoding/decoding in Terraform: Explained with examples

Last updated: February 04, 2024

Introduction

Terraform, an Infrastructure as Code (IaC) tool developed by HashiCorp, is a popular choice amongst developers for automating the deployment of server and application infrastructure. With its user-friendly HCL (HashiCorp Configuration Language), Terraform allows for easy definition and provisioning of cloud services. A critical capability in this process is handling JSON data, as JSON is a ubiquitous format for configuration files and API communication. In this tutorial, we’ll dive deep into how to effectively handle JSON encoding and decoding within Terraform, backed by comprehensive examples.

Understanding JSON in Terraform

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Terraform, though primarily uses HCL, can also interpret JSON syntax. This flexibility is pivotal when working with configurations or APIs that exclusively support JSON.

DECODING JSON

Decoding JSON in Terraform is extracting data from a JSON-formatted string into Terraform’s native HCL structure. This process is essential when dealing with dynamic or external data. The jsondecode() function comes to the rescue here.

Example 1: Decoding a JSON string

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

To decode this JSON string in Terraform, you can use:

variable "user_details" {
  type = string
  default = "{\"name\": \"John Doe\", \"age\": 30, \"city\": \"New York\"}"
}

output "user" {
  value = jsondecode(var.user_details)
}

This will convert the JSON string into an HCL object, allowing you to access its attributes like name, age, and city directly.

ENCODING JSON

Conversely, encoding JSON involves converting HCL objects into a JSON-formatted string. Terraform’s jsonencode() function enables this.

Example 2: Encoding an HCL object into a JSON string

locals {
  user_details = {
    name = "John Doe",
    age = 30,
    city = "New York"
  }
}

output "user_json" {
  value = jsonencode(locals.user_details)
}

This HCL object gets encoded into a JSON string, facilitating its use in applications or APIs requiring JSON format.

Advanced Usage: Dynamic Blocks and JSON

Dynamically generating parts of your configuration based on external data is a powerful aspect of Terraform’s functionality. The use of dynamic blocks with JSON data decodes can streamline the process.

Example 3: Using dynamic blocks with decoded JSON

variable "user_roles" {
  type = string
  default = "[{\"role\":\"admin\",\"active\":true},{\"role\":\"user\",\"active\":false}]"
}

resource "some_resource" "example" {
  dynamic "role" {
    for_each = jsondecode(var.user_roles)

    content {
      role_name = role.value.role
      role_active = role.value.active
    }
  }
}

This demonstrates how decoded JSON data can be iteratively applied to dynamic blocks, resulting in a more flexible and powerful resource configuration.

Best Practices When Dealing with JSON in Terraform

  • Validate JSON Inputs: Always ensure the JSON input is correctly formatted. Use tools like JSONLint for validation.
  • Avoid Hard-Coding Sensitive Data: Utilize Terraform’s variables and secrets management practices to protect sensitive data.
  • Utilize Terraform’s Data Sources: When working with external data, prefer data sources over hard-coded JSON strings to fetch data dynamically.

Conclusion

Understanding how to handle JSON encoding and decoding in Terraform enables better integration with other services, automation of configurations, and overall more dynamic infrastructure management. While JSON and HCL are distinct, their synergy within Terraform provides a powerful toolkit for cloud infrastructure deployment and management.

Next Article: Terraform: Read CSV data and convert it into a list of maps

Previous Article: Base64 Encoding/Decoding in Terraform: A Practical Guide

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List