Skip to content

Instantly share code, notes, and snippets.

@xbalaji
Created March 11, 2022 07:14
Show Gist options
  • Save xbalaji/0a98d0a1a10a7616d3d34d1bf260afd7 to your computer and use it in GitHub Desktop.
Save xbalaji/0a98d0a1a10a7616d3d34d1bf260afd7 to your computer and use it in GitHub Desktop.
execute conditional logic and exit terraform execution - exception
#
# Filename : condition.tf
# Date : 04 Mar 2022
# Author : Balaji Venkataraman ([email protected])
# Description : terraform script to push updated modules to registry
#
#
# case 1:
# $terraform plan
#
# No changes. Your infrastructure matches the configuration.
#
# Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
#
# case 2:
# $terraform plan -var 'kms=test'
#
# No changes. Your infrastructure matches the configuration.
#
# Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
#
# case 3:
# $terraform plan -var 'env=test'
# ╷
# │ Error: External Program Execution Failed
# │
# │ with data.external.kmstest[0],
# │ on condition.tf line 19, in data "external" "kmstest":
# │ 19: program = ["sh", "-c", ">&2 echo kms has to be with valid env; exit 1"]
# │
# │ The data source received an unexpected error while attempting to execute the program.
# │
# │ Program: /usr/bin/sh
# │ Error Message: kms has to be with valid env
# │
# │ State: exit status 1
# ╵
#
variable "kms" {
default = null
}
variable "env" {
type = string
default = null
}
data "external" "kmstest" {
count = (var.kms == null && var.env != null) ? 1 : 0
program = ["sh", "-c", ">&2 echo kms has to be with valid env; exit 1"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment