0
0
Terraformcloud~10 mins

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

Choose your learning style9 modes available
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.