Bird
Raised Fist0
Terraformcloud~15 mins

JSON configuration alternative in Terraform - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - JSON configuration alternative
What is it?
JSON configuration alternative refers to using a different format instead of JSON to write infrastructure code in Terraform. Terraform supports HashiCorp Configuration Language (HCL), which is designed to be easier for humans to read and write. Instead of JSON's strict syntax, HCL uses a cleaner, more natural style with blocks and attributes.
Why it matters
Using HCL instead of JSON makes writing and understanding infrastructure code simpler and less error-prone. Without this alternative, users would struggle with JSON's verbosity and strict formatting, making infrastructure management harder and slower. This can lead to mistakes that affect cloud resources and costs.
Where it fits
Before learning this, you should understand basic Terraform concepts like providers and resources. After this, you can explore advanced Terraform features like modules, state management, and automation pipelines.
Mental Model
Core Idea
Terraform's JSON alternative, HCL, is a human-friendly language designed to describe infrastructure clearly and simply.
Think of it like...
It's like choosing between writing a letter in formal legal language (JSON) versus writing it as a friendly note (HCL) that is easier to read and understand.
Terraform Configuration Formats
┌───────────────┐       ┌───────────────┐
│   JSON File   │       │   HCL File    │
│ { "resource": {│       │ resource "" { │
│   "aws_s3_bucket": {│   │   bucket = "" │
│     "mybucket": {│     │ }             │
│       "bucket": "mybucket"│ │ }             │
│     }           │       │               │
│   }             │       │               │
│ }               │       │               │
└───────────────┘       └───────────────┘
Build-Up - 6 Steps
1
FoundationWhat is Terraform Configuration
🤔
Concept: Terraform uses configuration files to define cloud resources.
Terraform configuration files tell Terraform what cloud resources to create and how to set them up. These files can be written in JSON or HCL formats.
Result
You understand that Terraform needs configuration files to work.
Knowing that Terraform requires configuration files is the base for learning how to write them effectively.
2
FoundationBasics of JSON Format in Terraform
🤔
Concept: JSON is a strict, machine-readable format used for Terraform configurations.
JSON uses braces {}, quotes, and commas to organize data. Every key and string must be quoted, and commas separate items. For example, a resource is defined inside nested objects.
Result
You can recognize Terraform JSON configuration structure.
Understanding JSON's strict syntax helps appreciate why alternatives exist.
3
IntermediateIntroduction to HCL Syntax
🤔Before reading on: do you think HCL looks more like code or data? Commit to your answer.
Concept: HCL is a domain-specific language designed to be human-friendly and readable.
HCL uses blocks with labels and attributes without requiring quotes everywhere. It looks more like code with clear sections and assignments, making it easier to read and write.
Result
You can identify HCL syntax and see how it differs from JSON.
Knowing HCL's syntax reduces errors and speeds up writing infrastructure code.
4
IntermediateComparing JSON and HCL Side-by-Side
🤔Before reading on: which format do you think is easier to edit by hand? Commit to your answer.
Concept: Comparing both formats highlights the practical benefits of HCL over JSON.
JSON requires many quotes and commas, making it verbose. HCL uses simpler blocks and assignments. For example, defining an AWS S3 bucket is shorter and clearer in HCL.
Result
You see why HCL is preferred for manual editing.
Understanding the differences helps choose the right format for your workflow.
5
AdvancedUsing JSON with Terraform Commands
🤔Before reading on: do you think Terraform treats JSON and HCL configurations the same internally? Commit to your answer.
Concept: Terraform can process both JSON and HCL files equivalently during execution.
Terraform accepts JSON files with a .tf.json extension and treats them the same as HCL files. This means you can use JSON if you prefer or generate configurations programmatically.
Result
You know how to use JSON files with Terraform commands.
Knowing Terraform's flexibility allows integration with tools that output JSON.
6
ExpertWhy HCL is Terraform's Default Language
🤔Before reading on: do you think HCL was designed mainly for machines or humans? Commit to your answer.
Concept: HCL was created to balance human readability with machine parsing efficiency.
HCL was designed by HashiCorp to make infrastructure code easier to write and understand, reducing errors and improving collaboration. JSON was supported for compatibility but is less user-friendly.
Result
You understand the design philosophy behind Terraform's language choice.
Recognizing design goals explains why HCL dominates Terraform usage today.
Under the Hood
Terraform parses both JSON and HCL files into the same internal data structures before planning and applying changes. The parser converts the configuration text into a graph of resources and dependencies. HCL's syntax is easier for humans but compiles down to the same representation as JSON.
Why designed this way?
HashiCorp created HCL to solve the problem of JSON's verbosity and difficulty for humans to write and maintain. They wanted a language that was both easy to read and write, yet still machine-friendly. JSON support was kept for compatibility with tools that generate JSON automatically.
Terraform Configuration Parsing
┌───────────────┐
│  HCL or JSON  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│  Parser Layer │
│ (HCL & JSON)  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Internal Graph│
│  of Resources │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Terraform Plan│
│  & Apply      │
└───────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Is JSON the only way to write Terraform configurations? Commit to yes or no.
Common Belief:Many think JSON is the only format Terraform accepts.
Tap to reveal reality
Reality:Terraform primarily uses HCL, a simpler and more readable language, but also supports JSON as an alternative.
Why it matters:Believing JSON is the only option can discourage beginners who find JSON hard to read and write.
Quick: Does Terraform treat JSON and HCL files differently when applying changes? Commit to yes or no.
Common Belief:Some believe Terraform processes JSON and HCL configurations differently.
Tap to reveal reality
Reality:Terraform converts both formats into the same internal representation and treats them equally during execution.
Why it matters:Misunderstanding this can lead to unnecessary format conversions or confusion about behavior.
Quick: Is HCL just JSON with a different name? Commit to yes or no.
Common Belief:Some think HCL is just JSON with a friendlier syntax.
Tap to reveal reality
Reality:HCL is a distinct language designed specifically for infrastructure, with features like comments and better readability not possible in JSON.
Why it matters:This misconception limits appreciation of HCL's design benefits and features.
Expert Zone
1
HCL supports expressions and functions, enabling dynamic and reusable configurations beyond static JSON.
2
Terraform's JSON support allows programmatic generation of configurations, useful in automated pipelines or tools.
3
HCL files can include comments, which JSON does not support, aiding collaboration and documentation.
When NOT to use
Avoid using JSON for manual editing or complex configurations because it is verbose and error-prone. Use JSON only when configurations are generated automatically by other software or scripts.
Production Patterns
In production, teams write main configurations in HCL for clarity and maintainability, while using JSON for generated modules or automated workflows. This hybrid approach leverages the strengths of both formats.
Connections
Domain-Specific Languages (DSLs)
HCL is a DSL designed for infrastructure configuration.
Understanding HCL as a DSL helps grasp why it balances human readability with machine parsing, a common goal in many specialized languages.
Code Generation
JSON configurations can be generated by programs automatically.
Knowing that JSON is easy to generate programmatically explains why Terraform supports it alongside HCL.
Human-Computer Interaction (HCI)
HCL improves the user experience of writing infrastructure code.
Recognizing HCL as a usability improvement connects cloud infrastructure to broader HCI principles about designing for humans.
Common Pitfalls
#1Writing Terraform configurations manually in JSON leads to syntax errors.
Wrong approach:{ "resource": { "aws_s3_bucket": { "mybucket": { "bucket": "mybucket" } } }, "extra_comma": true }
Correct approach:{ "resource": { "aws_s3_bucket": { "mybucket": { "bucket": "mybucket" } } } }
Root cause:JSON requires strict syntax with no trailing commas; beginners often add extra commas causing errors.
#2Trying to add comments in JSON configuration files.
Wrong approach:{ // This is a comment "resource": { "aws_s3_bucket": { "mybucket": { "bucket": "mybucket" } } } }
Correct approach:# This is a comment resource "aws_s3_bucket" "mybucket" { bucket = "mybucket" }
Root cause:JSON does not support comments, so adding them causes parsing errors; HCL supports comments.
#3Assuming Terraform will automatically convert JSON to HCL.
Wrong approach:Writing JSON files and expecting Terraform to output HCL automatically.
Correct approach:Manually write or convert JSON to HCL if needed; Terraform treats both formats equally but does not convert between them.
Root cause:Misunderstanding Terraform's format handling leads to confusion about conversion capabilities.
Key Takeaways
Terraform supports two configuration formats: JSON and HCL, with HCL being the preferred human-friendly language.
HCL is designed to be easier to read and write, reducing errors and improving collaboration compared to JSON.
Terraform parses both formats into the same internal structure, so they behave identically during execution.
JSON is useful for automated generation of configurations, while HCL is better for manual editing and complex setups.
Understanding the differences helps choose the right format for your workflow and avoid common mistakes.

Practice

(1/5)
1. What is the main reason to use JSON configuration files in Terraform instead of the usual HCL language?
easy
A. To enable automation and integration with other tools
B. Because JSON files are easier to read for humans
C. To reduce the size of the configuration files
D. Because Terraform does not support HCL anymore

Solution

  1. Step 1: Understand Terraform configuration formats

    Terraform supports both HCL and JSON formats for configuration files.
  2. Step 2: Identify the benefit of JSON format

    JSON is often used for automation and integration with other tools because it is a widely supported data format.
  3. Final Answer:

    To enable automation and integration with other tools -> Option A
  4. Quick Check:

    JSON helps automation = C [OK]
Hint: JSON is best for automation and tool integration [OK]
Common Mistakes:
  • Thinking JSON is easier for humans to read than HCL
  • Believing Terraform no longer supports HCL
  • Assuming JSON reduces file size
2. Which of the following is the correct way to start a Terraform JSON configuration file?
easy
A. { "resource": { "aws_instance": { "example": { "ami": "ami-123456", "instance_type": "t2.micro" } } } }
B. { resource: { aws_instance: { example: { ami: "ami-123456", instance_type: "t2.micro" } } } }
C. { "resource": [ { "aws_instance": { "example": { "ami": "ami-123456", "instance_type": "t2.micro" } } } ] }
D. [ "resource": { "aws_instance": { "example": { "ami": "ami-123456", "instance_type": "t2.micro" } } } ]

Solution

  1. Step 1: Check JSON syntax rules

    JSON keys and strings must be in double quotes, and objects use curly braces {}.
  2. Step 2: Identify correct Terraform JSON structure

    Terraform expects "resource" as a key with nested objects, not arrays or unquoted keys.
  3. Final Answer:

    { "resource": { "aws_instance": { "example": { "ami": "ami-123456", "instance_type": "t2.micro" } } } } -> Option A
  4. Quick Check:

    Proper JSON syntax with quoted keys = A [OK]
Hint: Always use double quotes for keys and strings in JSON [OK]
Common Mistakes:
  • Using unquoted keys in JSON
  • Using arrays instead of objects for resources
  • Mixing JSON and HCL syntax
3. Given this Terraform JSON snippet:
{
  "variable": {
    "region": {
      "default": "us-west-2"
    }
  },
  "provider": {
    "aws": {
      "region": "${var.region}"
    }
  }
}

What will happen when you run terraform apply?
medium
A. Terraform will deploy resources in the us-west-2 region
B. Terraform will throw an error because variable interpolation is not supported in JSON
C. Terraform will ignore the provider region and use the default AWS region
D. Terraform will deploy resources but with an empty region value

Solution

  1. Step 1: Understand variable interpolation in Terraform JSON

    Terraform JSON does not support interpolation syntax like "${var.region}" inside JSON files.
  2. Step 2: Identify the error cause

    Using interpolation in JSON causes Terraform to fail parsing or applying the configuration.
  3. Final Answer:

    Terraform will throw an error because variable interpolation is not supported in JSON -> Option B
  4. Quick Check:

    Interpolation not allowed in JSON = A [OK]
Hint: No interpolation syntax allowed in Terraform JSON files [OK]
Common Mistakes:
  • Assuming interpolation works same as HCL
  • Thinking Terraform ignores invalid region values
  • Believing Terraform defaults region silently
4. You have this JSON Terraform config snippet:
{
  "resource": {
    "aws_instance": {
      "ami": "ami-123456",
      "instance_type": "t2.micro",
      "tags": {
        "Name": "ExampleInstance"
      }
    }
  }
}

When running terraform apply, you get a syntax error. What is the most likely cause?
medium
A. The JSON keys are not properly quoted
B. The "tags" block should be an array, not an object
C. The JSON file is missing a root-level "terraform" key
D. The JSON file uses incorrect nesting for resource blocks

Solution

  1. Step 1: Review Terraform JSON resource structure

    Terraform expects resource blocks nested as: resource type -> resource name -> attributes.
  2. Step 2: Check the nesting in the snippet

    The snippet places attributes directly under the resource type "aws_instance" without the required instance name (e.g., "example") as an intermediate object key.
  3. Final Answer:

    The JSON file uses incorrect nesting for resource blocks -> Option D
  4. Quick Check:

    Resource nesting must follow Terraform JSON schema = B [OK]
Hint: Check resource nesting carefully in JSON configs [OK]
Common Mistakes:
  • Confusing JSON object vs array for tags
  • Expecting a "terraform" root key in JSON
  • Not quoting JSON keys properly
5. You want to convert this HCL Terraform config to JSON:
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
  acl    = "private"
  tags = {
    Environment = "Dev"
    Team        = "Cloud"
  }
}

Which JSON configuration correctly represents this resource?
hard
A. { "resource": { "aws_s3_bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } }
B. { "resource": [ { "aws_s3_bucket": { "bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } } ] }
C. { "resource": { "aws_s3_bucket": { "bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } } }
D. { "resources": { "aws_s3_bucket": { "bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } } }

Solution

  1. Step 1: Understand Terraform JSON resource format

    Terraform JSON uses "resource" (singular) as the root key with nested resource type and name.
  2. Step 2: Compare options for correct keys and structure

    { "resource": { "aws_s3_bucket": { "bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } } } correctly uses "resource" with nested "aws_s3_bucket" and resource name "bucket" with attributes.
  3. Final Answer:

    { "resource": { "aws_s3_bucket": { "bucket": { "bucket": "my-bucket", "acl": "private", "tags": { "Environment": "Dev", "Team": "Cloud" } } } } } -> Option C
  4. Quick Check:

    Correct root key and nesting = D [OK]
Hint: Use singular "resource" key with nested type and name [OK]
Common Mistakes:
  • Using "resources" instead of "resource"
  • Using arrays instead of objects for resources
  • Incorrect nesting of resource name and attributes