Convert Terraform Module Calls to JSON

Convert Terraform module blocks with source, version, and input variables from HCL to JSON format for programmatic module composition.

Modules

Detailed Explanation

Terraform Module Calls

Module blocks reference reusable Terraform configurations. They specify a source (local path, registry, or Git URL) and pass input variables to the child module.

HCL Module Definition

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.1.0"

  name = "my-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1a", "us-east-1b", "us-east-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
  single_nat_gateway = true

  tags = {
    Environment = "production"
    Terraform   = "true"
  }
}

module "security_group" {
  source  = "terraform-aws-modules/security-group/aws"
  version = "5.1.0"

  name        = "web-sg"
  description = "Security group for web servers"
  vpc_id      = "module.vpc.vpc_id"

  ingress_rules       = ["http-80-tcp", "https-443-tcp"]
  ingress_cidr_blocks = ["0.0.0.0/0"]
}

JSON Structure

Modules follow the standard block-to-JSON mapping: module > module name > attributes:

{
  "module": {
    "vpc": {
      "source": "terraform-aws-modules/vpc/aws",
      "version": "5.1.0",
      "name": "my-vpc",
      "cidr": "10.0.0.0/16",
      "azs": ["us-east-1a", "us-east-1b", "us-east-1c"],
      ...
    },
    "security_group": {
      "source": "terraform-aws-modules/security-group/aws",
      ...
    }
  }
}

Module Sources

The source attribute accepts multiple formats:

  • Registry: "terraform-aws-modules/vpc/aws"
  • GitHub: "github.com/org/repo"
  • Local: "./modules/vpc"
  • S3: "s3::https://bucket/module.zip"

All are represented as strings in both HCL and JSON.

Cross-Module References

References like module.vpc.vpc_id are expressions that Terraform resolves. In JSON, they appear as strings.

Use Case

Composing infrastructure from reusable Terraform modules, generating module call configurations from a service catalog, or building a Terraform module dependency analyzer that processes JSON configurations.

Try It — HCL ↔ JSON Converter

Open full tool