Working with JSON and YAML Configuration
1. Using JSON Configuration Files
| Property | Values | Description |
|---|---|---|
| filename | main.json |
Name of the JSON configuration file |
| format | json |
Configuration file format (must be json) |
| schema_version | 1 |
Version of the JSON schema to use |
Example: Basic JSON Configuration
{
"terraform": {
"required_version": ">= 1.0"
},
"provider": {
"aws": {
"version": "~> 5.0"
}
},
"resource": {
"aws_instance": {
"web": {
"ami": "ami-12345678",
"instance_type": "t2.micro"
}
}
}
}
Note: Terraform JSON files must be valid JSON and follow the schema structure defined by the Terraform language.
2. Converting HCL to JSON
| Feature | Description | Notes |
|---|---|---|
| hcl2json | hcl2json is a command-line tool to convert HCL files to JSON format. |
Available as part of the terraform CLI since v1.0+ |
| hcl2json -output | Specifies the output file path for the converted JSON. | Defaults to stdout if not specified |
Note: The
hcl2json utility is primarily used for tooling integration and debugging, not for runtime configuration.
3. Using jsonencode Function
| Function | Description | Example |
|---|---|---|
| jsonencode() | Encodes a value into a JSON string | jsonencode({name = "John", age = 30}) |
Example: Encoding a Map to JSON
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = "*"
Action = "s3:GetObject"
Resource = "arn:aws:s3:::my-bucket/*"
}
]
})
}
Note: The
jsonencode() function is useful for generating JSON configuration for AWS IAM policies or other services that require JSON input.4. Using jsondecode Function
| Function | Description | Example |
|---|---|---|
| jsondecode() | Decodes a JSON string into a Terraform value (object, list, etc.) | jsondecode("{\"name\": \"John\", \"age\": 30}") |
Example: Parsing JSON String
locals {
user_data = jsondecode("{\"name\": \"John\", \"age\": 30}")
}
output "user_name" {
value = local.user_data.name
}
Note: The
jsondecode() function is useful for integrating external JSON data into your Terraform configurations.
5. Using yamlencode Function
| Function | Description | Example |
|---|---|---|
| yamlencode | Encodes a value into a YAML string. | |
Note: The
yamlencode function is useful for generating YAML configuration files or embedding structured data in a YAML context.6. Using yamldecode Function
| Function | Description | Example |
|---|---|---|
| yamldecode() | Decodes a YAML string into a Terraform value (list, map, or primitive). | yamldecode("name: "terraform"\nversion: "1.8"") |
Note: The input must be valid YAML. Invalid YAML will cause a parse error.
7. Loading External JSON Files
| Property | Description | Example |
|---|---|---|
| jsondecode() | Function to parse JSON strings into Terraform expressions | jsondecode(file("config.json")) |
| file() | Reads content of a file as a string | file("data.json") |
Example: Using jsondecode() in Terraform
resource "aws_db_instance" "main" {
identifier = jsondecode(file("config.json")).database.host
port = jsondecode(file("config.json")).database.port
}
8. Generating JSON Output
When working with Terraform configurations, you can generate JSON output for various purposes such as integration with other tools or exporting state information. Terraform provides several mechanisms to achieve this.
| Method | Description | Usage Example |
|---|---|---|
| terraform output | Exports all outputs in JSON format when used with the -json flag. |
terraform output -json |
| output values | Specific output values can be exported in JSON format using the -json flag with a specific output name. |
terraform output -json my_output |
| tfstate files | Terraform state files are stored in JSON format by default, which can be parsed directly. | cat terraform.tfstate |
Note: The
-json flag ensures that the output is formatted as valid JSON, making it easier to integrate with other systems or scripts.
9. Using JSON in Templates
| Feature | Description | Usage |
|---|---|---|
| jsonencode() | Encodes a Terraform value into a JSON string | jsonencode({key = "value"}) |
| jsondecode() | Decodes a JSON string into a Terraform value | jsondecode("{\"key\": \"value\"}") |
Example: Encoding a Map to JSON
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
tags = jsonencode({
Name = "example-bucket"
Environment = "production"
})
}
Example: Decoding JSON from a Variable
variable "bucket_tags" {
type = string
default = "{\"Name\": \"example-bucket\", \"Environment\": \"production\"}"
}
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
tags = jsondecode(var.bucket_tags)
}
10. Validating JSON Schema
| Property | Values | Description |
|---|---|---|
| schema | string |
Path to the JSON schema file used for validation |
| data | object |
JSON data to validate against the schema |
| strict | true | false |
If true, validation fails on unknown properties |
Example: Validating JSON with Schema
{
"schema": "./schema.json",
"data": {
"name": "John",
"age": 30
},
"strict": true
}
Note: Terraform does not natively support JSON schema validation in its core. You must use external tools or custom scripts for this.