Bird
Raised Fist0
Terraformcloud~10 mins

JSON configuration alternative in Terraform - Step-by-Step Execution

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
Process Flow - JSON configuration alternative
Start with JSON config
↓
Parse JSON into Terraform
↓
Terraform reads JSON values
↓
Apply config to create resources
↓
Resources created as per JSON
↓
End
Terraform can use JSON files as an alternative to its usual HCL syntax by parsing JSON configs to create infrastructure resources.
Execution Sample
Terraform
{
  "resource": {
    "aws_s3_bucket": {
      "example": {
        "bucket": "my-json-bucket"
      }
    }
  }
}
This JSON defines an AWS S3 bucket resource named 'example' with the bucket name 'my-json-bucket'.
Process Table
StepActionInputTerraform InterpretationResult
1Read JSON file{"resource":{"aws_s3_bucket":{"example":{"bucket":"my-json-bucket"}}}}Parse JSON structureParsed JSON object with resource block
2Identify resource typeaws_s3_bucketRecognize AWS S3 bucket resourcePrepare to create S3 bucket
3Identify resource nameexampleAssign resource instance nameResource instance named 'example'
4Read resource properties{"bucket":"my-json-bucket"}Set bucket propertyBucket name set to 'my-json-bucket'
5Apply Terraform planParsed JSON configCreate AWS S3 bucket with given nameS3 bucket 'my-json-bucket' created
6EndAll resources createdNo more resources to processTerraform apply complete
💡 All JSON-defined resources processed and created successfully.
Status Tracker
VariableStartAfter Step 2After Step 4Final
resource_typeundefinedaws_s3_bucketaws_s3_bucketaws_s3_bucket
resource_nameundefinedundefinedexampleexample
bucket_nameundefinedundefinedmy-json-bucketmy-json-bucket
Key Moments - 3 Insights
Why does Terraform accept JSON when it usually uses HCL?
Terraform supports JSON as an alternative syntax because JSON can represent the same configuration structure. The execution_table shows Terraform parsing JSON just like HCL.
How does Terraform know which resource to create from JSON?
Terraform reads the 'resource' block keys in JSON to identify resource types and names, as shown in steps 2 and 3 of the execution_table.
Can JSON files include variables or interpolation like HCL?
No, JSON configs are static and do not support interpolation. Variables must be defined separately or use HCL for dynamic expressions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resource name identified at step 3?
Aexample
Baws_s3_bucket
Cmy-json-bucket
Dresource
💡 Hint
Check the 'Terraform Interpretation' column at step 3 in the execution_table.
At which step does Terraform set the bucket name property?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Terraform Interpretation' columns in the execution_table.
If the JSON had multiple resources, how would the execution_table change?
ANo change, Terraform processes only one resource at a time
BFewer rows because resources are combined
CMore rows for each resource parsing and creation steps
DExecution would stop after first resource
💡 Hint
Refer to the flow of steps in the execution_table showing one resource processed per set of steps.
Concept Snapshot
Terraform supports JSON as an alternative to HCL for configuration.
JSON files must follow Terraform's schema structure.
Terraform parses JSON to identify resource types, names, and properties.
JSON configs are static; no interpolation or variables inside JSON.
Use JSON when you prefer JSON syntax or need to generate configs programmatically.
Full Transcript
Terraform can use JSON files instead of its usual HCL syntax to define infrastructure. The process starts by reading the JSON file, parsing its structure, and identifying resource types and names. Then Terraform reads resource properties like bucket names and applies the configuration to create resources. JSON configs must follow Terraform's schema and are static without interpolation. This allows users to write Terraform configs in JSON format, which Terraform understands and applies to create infrastructure resources.

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