Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

feature request: fail on unexpected exit code #34

Closed
jantari opened this issue Feb 23, 2021 · 4 comments
Closed

feature request: fail on unexpected exit code #34

jantari opened this issue Feb 23, 2021 · 4 comments

Comments

@jantari
Copy link

jantari commented Feb 23, 2021

Hello, thanks for the module!

Would it be possible to add an option to supply an array of "expected" or "ok" exit codes for the command,
and the module would fail and halt terraform when the exit code of the command does not match any of those?

The problem we are facing is that we use the module to make a curl request, but the URL we are accessing is not accessible from everywhere - it is internal only. It would be nice to be able to fail the terraform apply in case curl returns exit code 7 (Failed to connect to host) instead of continuing with an empty stdout.

This is unfortunately related to yet another missing terraform feature, being able to intentionally fail on a condition.

I have tried:

module "curl_command" {
  count   = var.vm_count
  source  = "matti/resource/shell"
  command = "curl -X POST --silent -H 'Accept: text/plain' https://internal/api"
}

locals {
  # This is a workaround because there is no way in terraform to intentionally fail/error on a condition
  # See: https://github.com/hashicorp/terraform/issues/15469
  test_curl_not_empty = contains(module.curl_command.*.stdout, "") ? file("// ERROR: The API timed out! //") : null
}

resource "resource_kind" "ourname" {
  depends_on = [
    local.test_curl_not_empty
  ]
  
  ...
}

and while this does work initially for the first apply (it fails as intended when the API timed out), this empty response still gets written to the remote state (despite me intentionally erroring tf, which I would think taints everything ...) and upon retrying further applys the curl command is never executed again it just gets the empty stdout from the remote state and continuously fails/errors on the file() statement.

So I think embedding the failure condition into your module might be the only sane choice to error-handle this.

Thank you!

@jantari jantari changed the title Fail on unexpected exit code feature request: fail on unexpected exit code Feb 23, 2021
@matti
Copy link
Owner

matti commented Feb 23, 2021

I think you should wrap curl in a shell script that handles that logic?

@jantari
Copy link
Author

jantari commented Feb 23, 2021

But what would I have to do within the shell script to make the module fail then?

The curl command itself already returns a non-zero exit code - that doesn't do it.

The only thing I can think of is ... to kill the terraform process LOL:

command = "/bin/bash -c \"curl -X POST --silent -H 'Accept: text/plain' https://internal/api || killall terraform\""

I need some way to fail and stop the apply - I don't think I could do that from within a shell script.

@JohnPolansky
Copy link

Did we find a resolution to this? I'm facing a similar issue.. with local-exec if the command you execute returns a non-0 exit status it fails the terraform apply which to me feels like the basic expected behavior. I find this module very promising but I"m confused am I missing a feature how do I trigger the module to fail if the command returns an error?

@matti
Copy link
Owner

matti commented Mar 6, 2022

@matti matti closed this as completed Mar 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants