How to pass the -detailed-exitcode argument to terraform plan

I’m trying to run terraspace in my CI server. With Terraform we use

terraform plan -detailed-exitcode
echo $?

The Terraform exit code tells us then:-

  • 0 = No change
  • 1 = Some error
  • 2 = A diff was detected and must be applied

But when I tried the following:-

terraspace plan my_stack -detailed-exitcode --auto

ERROR: "terraspace plan" was called with arguments ["my_stack", "-detailed-exitcode"]
Usage: "terraspace plan STACK"

Currently, would have to be done with customizing args.

Hope that helps. Would like to also be able to pass it at the CLI too in a more generalized manner. Will consider PRs. No sweat either way of course.

Thanks @tung I gave that a go, it does indeed pass in the flag but the response code of “2” (meaning a diff has been found) is then treated as an error by Terraspace. So it would need a PR to permit exit codes of “0” and “2” to be viewed as successful.
I dont know where I would need to make that change though.

Did a little debugging:

Debugging Session
$ terraspace plan demo                          
Building .terraspace-cache/us-west-2/dev/stacks/demo
Built in .terraspace-cache/us-west-2/dev/stacks/demo
Current directory: .terraspace-cache/us-west-2/dev/stacks/demo
=> terraform plan -detailed-exitcode -input=false

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
  # random_pet.this will be created
  + resource "random_pet" "this" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }
  # module.bucket.aws_s3_bucket.this will be created
  + resource "aws_s3_bucket" "this" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = (known after apply)
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags_all                    = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }
Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + bucket_name = (known after apply)

─────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
Error running command: terraform plan -detailed-exitcode -input=false
$ echo $?
2
$ cd .terraspace-cache/us-west-2/dev/stacks/demo
$ terraform plan -detailed-exitcode -input=false

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # random_pet.this will be created
  + resource "random_pet" "this" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }

  # module.bucket.aws_s3_bucket.this will be created
  + resource "aws_s3_bucket" "this" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = (known after apply)
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags_all                    = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + bucket_name = (known after apply)

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
$ echo $?
2
$ 

Looks like the exit code of terraform is passed back to the terraspace process and simply passed through. It happens here:

There’s already an internal exit_on_fail option that raises an error instead of calling exit and exiting the process entirely. The raising of an error would still stop the process for the plan command. The exit_on_fail option is really used internally for the terraspace all run. And can be configured with all.exit_on_fail.down and all.exit_on_fail.up. See:

Gut says that passing that option all the way from the CLI to shell.rb is not worth the complexity.

Instead, if you want to suppress the failure signal, and have a success signal you can:

terraspace plan my_stack -detailed-exitcode --auto || true