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
Terraform JSON Configuration Alternative
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. Instead of using the usual .tf files, you will write the configuration using Terraform's JSON syntax. This helps when you want to generate Terraform configs programmatically or prefer JSON format.
🎯 Goal: Create a Terraform configuration in JSON format that defines a provider and a resource to launch a virtual machine instance with specific properties.
📋 What You'll Learn
Create a JSON object for Terraform configuration
Define the provider block for AWS with region us-east-1
Define a resource block for an AWS EC2 instance named example
Set the instance type to t2.micro and use AMI ID ami-0c55b159cbfafe1f0
Use valid JSON syntax compatible with Terraform
💡 Why This Matters
🌍 Real World
Terraform supports JSON configuration as an alternative to HCL. This is useful when generating configs programmatically or integrating with tools that produce JSON.
💼 Career
Understanding Terraform JSON syntax helps cloud engineers automate infrastructure provisioning and integrate Terraform with other systems.
Progress0 / 4 steps
1
Create the base JSON structure for Terraform
Create a JSON object called terraformConfig with an empty provider and resource keys as empty objects.
Terraform
Hint
Start with an object that has provider and resource keys, both set to empty objects.
2
Add AWS provider configuration
Inside the provider object, add an aws key with an object that sets region to "us-east-1".
Inside the resource object, add an aws_instance key. This key should be an object with a key example that contains an object with ami set to "ami-0c55b159cbfafe1f0" and instance_type set to "t2.micro".
Ensure the JSON object has the provider with AWS region us-east-1 and the resource with an aws_instance named example with the specified AMI and instance type. The JSON must be valid and ready for Terraform to use.
Terraform
Hint
Check that all required keys and values are present and the JSON syntax is correct.
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
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
Step 1: Review Terraform JSON resource structure
Terraform expects resource blocks nested as: resource type -> resource name -> attributes.
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.
Final Answer:
The JSON file uses incorrect nesting for resource blocks -> Option D
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: