Bird
Raised Fist0
Terraformcloud~5 mins

JSON configuration alternative in Terraform - Commands & Configuration

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
Introduction
Terraform usually uses a special language to define infrastructure, but it can also use JSON format. This helps when you want to generate configurations automatically or prefer JSON syntax.
When you want to generate Terraform configurations programmatically from other tools that output JSON.
When you prefer JSON syntax over Terraform's usual language for defining infrastructure.
When integrating Terraform with systems that only accept JSON input.
When you want to validate your infrastructure code with JSON tools.
When you need to share configurations with teams familiar with JSON.
Config File - main.tf.json
main.tf.json
{
  "resource": {
    "aws_s3_bucket": {
      "example_bucket": {
        "bucket": "my-example-bucket",
        "acl": "private"
      }
    }
  }
}

This JSON file defines a Terraform configuration that creates an AWS S3 bucket named my-example-bucket with private access.

The resource key holds the resources to create. Inside it, aws_s3_bucket specifies the type, and example_bucket is the resource name. The properties like bucket and acl set the bucket name and access control.

Commands
This command initializes the Terraform working directory, downloads necessary plugins, and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will do based on the JSON configuration, without making any changes yet.
Terminal
terraform plan
Expected OutputExpected
Terraform used the selected providers to generate an execution plan. Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" later.
This command applies the configuration, creating the S3 bucket as defined in the JSON file. The flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=my-example-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply step without asking for confirmation
This command removes the created resources, cleaning up the infrastructure. The flag skips the confirmation prompt.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
→
-auto-approve - Automatically approve the destroy step without asking for confirmation
Key Concept

Terraform configurations can be written in JSON format as an alternative to the usual language, enabling automation and integration with JSON-based tools.

Common Mistakes
Using invalid JSON syntax like missing commas or quotes.
Terraform will fail to parse the configuration and show errors.
Always validate your JSON syntax with a JSON validator before running Terraform.
Naming the file with .tf extension instead of .tf.json for JSON configs.
Terraform expects JSON configs to have .tf.json extension to parse them correctly.
Save JSON Terraform configs with the .tf.json extension.
Mixing JSON and HCL syntax in the same file.
Terraform cannot parse mixed syntax in one file, causing errors.
Use either JSON or HCL syntax per file, not both.
Summary
Terraform supports JSON format for configuration files as an alternative to its usual language.
Use .tf.json extension for JSON configuration files so Terraform recognizes them.
Run terraform init, plan, apply, and destroy commands to manage infrastructure defined in JSON.

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