Skip to content

rhythmictech/terraform-terraform-errorcheck

Repository files navigation

terraform-terraform-errorcheck

A Terraform module that throws an error at plan time if an arbitrary assertion fails.

tests tflint tfsec yamllint misspell pre-commit-check follow on Twitter

Example

Here's what using the module will look like
The errorcheck_valid module will pass, but the errorcheck_invalid module will fail, aborting the plan.

locals {
  compare     = "success"
  testSuccess = "success"
  testFail    = "fail"
}

module "errorcheck_valid" {
  source = "rhythmictech/errorcheck/terraform"
  version = "~> 1.0.0"

  assert = local.compare == local.testSuccess
  error_message = "Your assertion is not valid"
}

module "errorcheck_invalid" {
  source = "rhythmictech/errorcheck/terraform"
  version = "~> 1.0.0"

  assert = local.compare == local.testFail
  error_message = "Your assertion is not valid"
}

Output:

  Error: failed to execute "/bin/sh": jq: error (at <stdin>:0): Your assertion is not valid

  on main.tf line 1, in data "external" "this":
   1: data "external" "this" {

About

This module exists because Terraform's native ability to throw errors at plan time is extremely limited if your provider doesn't do plan-time validation. Using this module you can use any arbitrary assertion to do plan-time validation. Our particular use-case was making sure the targeted environment matches the workspace.

Requirements

Name Version
terraform >= 0.12.28
external >= 2.2.0

Providers

Name Version
external 2.2.2

Modules

No modules.

Resources

Name Type
external_external.this data source

Inputs

Name Description Type Default Required
assert Make your assertion here so that it resolves to a boolean value. If this value is false, the module will throw an error. bool n/a yes
error_message Error message to return if the assertion fails string "Assertion failed" no
python_program Python executable to call errorcheck with string "python" no
use_jq Use jq to check for truthiness. Defaults to python bool false no

Outputs

Name Description
asset Make your assertion here so that it resolves to a boolean value. If this value is false, the module will throw an error.
error_message Error message to return if the assertion fails
result Result of data source
use_jq Use jq to check for truthiness. Defaults to python

The Giants Underneath this Module

About

Use the `external` module to implement custom error checking at plan time

Resources

License

Stars

Watchers

Forks

Packages

No packages published