Skip to content

poseidon/terraform-provider-cue

Repository files navigation

terraform-provider-cue

GoDoc Workflow Downloads Sponsors Mastodon

terraform-provider-cue allows Terraform to evaluate CUE configs and render JSON for use in Terraform.

Author's Note CUE has potential to be a better Jsonnet (if it gets a proper module manager). But like Jsonnet, its usage should be limited to preparing JSON-only configs where there are no viable alternatives (e.g. Grafana dashboards). Prefer native Terraform where possible, its ecosystem and design is simpler, more powerful, more mature, and ubiquitous.

Usage

Configure the cue provider (e.g. providers.tf).

provider "cue" {}

terraform {
  required_providers {
    ct = {
      source  = "poseidon/cue"
      version = "0.4.1"
    }
  }
}

Run terraform init to ensure version requirements are met.

$ terraform init -upgrade

Define a cue_config data source to validate CUE content.

data "cue_config" "example" {
  pretty_print = true
  content = <<-EOT
    a: 1
    b: 2
    sum: a + b
    _hidden: 3
    l: [a, b]

    map: [string]:int
    map: {a: 1 * 5}
    map: {"b": b * 5}
  EOT
}

Optionally provide paths to CUE files (supports imports).

data "cue_config" "example" {
  paths = [
    "core.cue",
    "box.cue",
  ]
}

Or unify content and path based expressions together.

data "cue_config" "example" {
  paths = [
    "partial.cue",
  ]
  content = <<-EOT
    package example

    _config: {
      name: "ACME"
      amount: "$20.00"
    }
  EOT
}

Customize the root directory CUE uses during loading (defaults to current directory).

data "cue_config" "example" {
  paths = [
    "foo.cue",
  ]
  dir = "internal/testmod"
  pretty_print = false
}

Render the CUE config as JSON for use in Terraform expressions.

output "out" {
  description = "Show Cue rendered as JSON"
  value       = data.cue_config.example.rendered
}

The rendered content example looks like:

{
  "a": 1,
  "b": 2,
  "sum": 3,
  "l": [
    1,
    2
  ],
  "map": {
    "a": 5,
    "b": 10
  }
}

Requirements

Development

Binary

To develop the provider plugin locally, build an executable with Go v1.18+.

make