Skip to content

Commit

Permalink
Switch method to not use external data source
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKotowick committed Jul 30, 2021
1 parent 6136ce2 commit 81eb6d7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Terraform Assertion
A simple module that asserts that a condition is true. The assertion is checked during planning, unless the condition depends on a value that will only be known during apply.

The module forces Terraform to fail if the condition is false by attempting to call a non-existant external command. It uses the error message in the command name so that the resulting Terraform failure message is descriptive (although it is slightly confusing in the format; see the example below).
The module forces Terraform to fail if the condition is false by providing an invalid input to a Terraform function. It uses the error message in the invalid input so that the resulting Terraform failure message is descriptive (although it is slightly confusing in the format; see the example below).

## Usage
```
Expand All @@ -24,5 +24,5 @@ No changes. Your infrastructure matches the configuration.

Output on Windows machines:
```
Error: can't find external program "ERROR: This Terraform configuration can only be run on Unix-based machines."
Invalid value for "number" parameter: cannot parse "ERROR: This Terraform configuration can only be run on Unix-based machines." as a base 2 integer.
```
7 changes: 2 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ terraform {
required_version = ">= 0.13.0"
}

// Terraform has no built-in customizable error system, so instead we try to run a non-existant program with the error message in the name.
// It's not pretty, but it works.
data "external" "assertion" {
count = var.condition ? 0 : 1
program = ["ERROR: ${var.error_message}"]
locals {
content = var.condition ? "" : parseint("ERROR: ${var.error_message}", 2)
}
3 changes: 1 addition & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
output "checked" {
description = "Whether the condition has been checked (used for assertion dependencies)."
depends_on = [data.external.assertion]
value = true
value = local.content == "" ? true : true
}

0 comments on commit 81eb6d7

Please sign in to comment.