Convert AWS VPC Network HCL to JSON
Convert a Terraform VPC configuration with subnets, internet gateway, and route tables from HCL to JSON format.
Detailed Explanation
VPC Network Infrastructure
A VPC (Virtual Private Cloud) configuration typically involves multiple interdependent resources. Converting this to JSON demonstrates how cross-resource references and complex networking setups translate.
HCL Configuration
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
resource "aws_subnet" "public" {
vpc_id = "aws_vpc.main.id"
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "public-subnet"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = "aws_vpc.main.id"
tags = {
Name = "main-igw"
}
}
Cross-Resource References
In HCL, references like aws_vpc.main.id are expressions that Terraform evaluates at plan time. In the JSON conversion, these become string values since the converter handles structural translation, not expression evaluation.
Multiple Resource Types
The JSON output organizes resources by type:
aws_vpccontains the VPC definitionaws_subnetcontains subnet configurationsaws_internet_gatewaycontains the gateway
Each resource type can have multiple named instances. This structure matches Terraform's JSON configuration syntax.
CIDR Block Notation
CIDR values like 10.0.0.0/16 remain as strings in both HCL and JSON. Terraform's cidrsubnet() function for computing subnet ranges would appear as a string expression in JSON output.
Use Case
Building multi-tier network architectures on AWS, converting existing VPC configurations for infrastructure-as-code migrations, or generating networking Terraform from network diagram tools that output JSON.