Terraform JSON schemas for bazel rule creation

I currently have some bazel rules that produce a main.tf file to tie code to terraform deploys, however these are all coded by hand, an example is below:

load("//:rules/terraform/rules.bzl", "terraform_google_provider", "terraform_google_compute_engine_instance", "terraform_google_boot_disk")
load("//:rules/terraform/macros.bzl", "terraform_deploy")

terraform_google_boot_disk(
    name="ghe-instance-disk",
    image="projects/github-enterprise-public/global/images/github-enterprise-3-10-3",
    size=201,
)

terraform_google_compute_engine_instance(
    name="ghe-instance",
    boot_disk=":ghe-instance-disk",
    machine_type="n1-standard-16",
)

terraform_google_provider(
    name = "main_google_project",
    project = "XXX",
    region = "us-east1",
    zone = "us-east1-c",
    deps = [":ghe-instance"],
)

terraform_deploy(
    name = "deploy",
    provider = ":main_google_project",
)

These are handmade, and I am wondering if there might be a way for me to pull down the schemas for these different modules so I can automate the creation of these rules?

Hi @8W9aG,

I’m afraid I’m not familiar enough with Bazel to guess what the items in this configuration file represent. Is each one representing what would be a module block or a resource block in the Terraform language?

There are some ways to obtain schema information about modules and providers, but they involve local code execution rather than being some data you can just download and use.

For modules you can analyze a module you’ve already retrieved using the library terraform-config-inspect, which returns metadata about a subset of the Terraform language that tends to be useful for needs like this. The official Terraform modules registry at registry.terraform.io uses that library to generate the module reference docs in its web UI.

For provider plugins the easiest answer is to write a Terraform configuration that refers to the provider you are interested in, use terraform init to get the provider installed to the local cache, and then run terraform providers schema -json to export the provider schemas in JSON format.

In this case I am rendering resources into a main.tf file, like so:

def create_google_compute_disk(dep, project, zone):
    return """
resource "google_compute_disk" "%s" {
    image = "%s"
    name = "%s"
    physical_block_size_bytes = %d
    project = "%s"
    size = %d
    type = "%s"
    zone = "%s"
}
""" % (
        dep[GoogleComputeDiskProvider].image,
        dep[GoogleComputeDiskProvider].name,
        dep[GoogleComputeDiskProvider].physical_block_size_bytes,
        project,
        dep[GoogleComputeDiskProvider].size,
        dep[GoogleComputeDiskProvider].type,
        dep[GoogleComputeDiskProvider].zone if dep[GoogleComputeDiskProvider].zone is not None else zone,
    )

I did try and run terraform providers schema -json but I just get the following output:

{"format_version":"1.0"}

This might be user error here, maybe I’ll just keep doing things by hand since it seems quite hard to make a generation script.