Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong parsing ot the destroy-time provisioner command, during a targeted destroy #17158

Closed
mvitale1989 opened this issue Jan 22, 2018 · 1 comment · Fixed by #17241
Closed

Comments

@mvitale1989
Copy link

mvitale1989 commented Jan 22, 2018

Terraform Version

Terraform v0.11.2                                  
+ provider.null v1.0.0    

Terraform Configuration Files

locals {
  msg_creation = "I should be shown at creation-time"
  msg_destruction = "I should be shown at destruction-time"
}

resource "null_resource" "myresource" {
  provisioner "local-exec" {
    command = "echo ${local.msg_creation}"
  }
  provisioner "local-exec" {
    command = "echo ${local.msg_destruction}"
    when = "destroy"
  }
}

Expected Behavior

Doing a targeted destroy of myresource should execute the destroy-time provisioner, and then remove the resource from the state.

Actual Behavior

Invoking terraform destroy --target null_resource.myresource --force prints out a possibly faulty error message:

null_resource.myresource: Refreshing state... (ID: 95776629882402507)
null_resource.myresource: Destroying... (ID: 95776629882402507)
null_resource.myresource: Provisioning with 'local-exec'...

Error: Error applying plan:

1 error(s) occurred:

* null_resource.myresource (destroy): 1 error(s) occurred:

* local-exec provisioner command must be a non-empty string

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

It's worth noting that this error is not printed out when doing a targetless destroy, e.g. terraform destroy --force. Also, a targeted apply does not incur in any problems.

Steps to Reproduce

Given the above config:

  1. terraform init
  2. terraform apply --auto-approve
  3. terraform destroy --target null_resource.myresource --force

Found workaround

The workaround i used to overcome this problem might be useful in identifying the cause: to make the above steps work, I added the variable that appears in the destroy-time command into the creation-time provisioner block, as a comment.

Thus, the following configuration ensures that the terraform destroy --target null_resource.myresource --force works as well (notice the commented variable, in the creation-time provisioner command):

locals {
  msg_creation = "I should be shown at creation-time"
  msg_destruction = "I should be shown at destruction-time"
}

resource "null_resource" "myresource" {
  provisioner "local-exec" {
    command = "echo ${local.msg_creation} #${local.msg_destruction}"
  }
  provisioner "local-exec" {
    command = "echo ${local.msg_destruction}"
    when = "destroy"
  }
}
@ghost
Copy link

ghost commented Apr 5, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants