Deletion of resources

531 views
Skip to first unread message

Rohit Atri

unread,
Oct 17, 2015, 4:05:49 PM10/17/15
to Terraform
Hello!

Would running 'terraform destroy' on a specific resource also delete the corresponding .tf file? (of course, if its the only resource defined in the .tf file)

Also, would deleting a .tf file and running 'terraform apply', delete the resource defined in the deleted .tf file?

Thanks,
Rohit

Dave Cunningham

unread,
Oct 17, 2015, 4:07:44 PM10/17/15
to terrafo...@googlegroups.com
On Sat, Oct 17, 2015 at 4:05 PM, Rohit Atri <rohit...@gmail.com> wrote:
Hello!

Would running 'terraform destroy' on a specific resource also delete the corresponding .tf file? (of course, if its the only resource defined in the .tf file)

No, the tf file is left alone
 

Also, would deleting a .tf file and running 'terraform apply', delete the resource defined in the deleted .tf file?

No as the credentials are in the tf file and it can't do anything without them.

You can however comment out everything except the credentials and then apply it, which has the same semantics as destroy.
 

Thanks,
Rohit

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/3f685504-2c70-4f31-820e-ec6cb50a8b0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rohit Atri

unread,
Oct 17, 2015, 4:12:56 PM10/17/15
to Terraform
Great, thanks!

I was worried about someone accidentally deleting .tf files... until we setup git & stuff.

Would Terraform complain that something is missing since the last time 'apply' was run?

Thanks,
Rohit

Dave Cunningham

unread,
Oct 17, 2015, 4:14:58 PM10/17/15
to terrafo...@googlegroups.com
As long as terraform sees at least one tf file I believe it does not complain.  So yes, you could create a plan that removes resources if you delete a tf file that contains only resources.  It's always a good idea to look at the plan first, in fact I always run terraform in a script that displays the plan then prompts for [y/N].

Rohit Atri

unread,
Oct 17, 2015, 4:20:21 PM10/17/15
to Terraform
"So yes, you could create a plan that removes resources if you delete a tf file that contains only resources."

Sorry, I didn't understand this statement. Are you saying that terraform will notice something is missing (by the way of proposing to delete some resources) when I delete a .tf file? But wont succeed since it doesn't have the credentials?

Thanks,
Rohit

Cameron Stokes

unread,
Oct 17, 2015, 4:24:40 PM10/17/15
to terrafo...@googlegroups.com
If a .tf file is removed and Terraform is already managing resources that were defined in that file, then Terraform will attempt to destroy the resources that were in that file. A file being removed has the same effect as all resources in that file being removed or commented out. Terraform treats these cases the same and will attempt to remove any of the resources it already has in its state file. More on the state file is at https://terraform.io/docs/state/index.html

It is always a good idea, to do a terraform plan prior to a terraform apply to check and confirm changes before they're made. 

Dave Cunningham

unread,
Oct 17, 2015, 5:05:29 PM10/17/15
to terrafo...@googlegroups.com
Unless the tf file contained the provider in which case I think Terraform will complain because there are things in the tfstate that it can't refresh.

Cameron Stokes

unread,
Oct 17, 2015, 5:27:15 PM10/17/15
to terrafo...@googlegroups.com
That is true. If the file contains the provider details, then terraform will need be able to proceed with any changes.

Many projects separate their resources into multiple tf files, so I wanted to be clear that removing a file (that doesn't contain the provider info) will result in those resources being destroyed on the next apply. 

Hope that helps clear it up. :)

Rohit Atri

unread,
Oct 17, 2015, 11:47:45 PM10/17/15
to terrafo...@googlegroups.com
Thanks! Yes, it clears things up for me now.

Is there a way to avoid accidental deletion? I am automating the entire process and so there would be no one verifying the plan. May be the script should parse the plan to check for destructive actions?

I am guessing setting 'lifecycle' to 'prevent_destroy' would be useless if the .tf file with the resource itself is gone.

Thanks
Rohit

From: Cameron Stokes
Sent: ‎18-‎10-‎2015 02:57
To: terrafo...@googlegroups.com
Subject: Re: [terraform] Deletion of resources

You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/KknJ4XtkBCk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/573E5A41-F636-419B-A435-210D77853098%40hashicorp.com.

Cameron Stokes

unread,
Oct 19, 2015, 4:34:02 PM10/19/15
to terrafo...@googlegroups.com
There is not a built-in way to prevent all deletes from happening, but there are a couple other options...

1) If your provider supports it, you could restrict the permissions belonging to your provider keys to prevent delete operations from occurring. This is possible with AWS IAM policies as an example.
2) You could put in some additional scripting to check the output of terraform plan to inspect if it plans to destroy any resources before continuing.

$ terraform plan -destroy terraform/
Refreshing Terraform state prior to plan...
...
Plan: 0 to add, 0 to change, 4 to destroy.
$



Alex Dupuy

unread,
Oct 20, 2015, 12:05:48 AM10/20/15
to Terraform
Cameron Stokes wrote:
There is not a built-in way to prevent all deletes from happening, but there are a couple other options...

1) If your provider supports it, you could restrict the permissions belonging to your provider keys to prevent delete operations from occurring. This is possible with AWS IAM policies as an example.

Some providers may also provide ways to "lock" or otherwise protect a resource so that it cannot be destroyed using the API that Terraform is using.  For our OpenStack deployment, we protected VM instances using the "nova lock" command (deletion and other modifications of locked instances will fail), and protected storage volumes by creating snapshots (deletion of a storage volume with associated snapshots will fail).  We even automated these protections by adding them to the resources via local-exec provisioners, e.g.:

resource "openstack_compute_instance_v2" "example" {
    region = "${var.region}"
    name = "example"
    image_name = "${var.host_image}"
    flavor_name = "m1.small"
    volume {
        device = "/dev/vdb"
    }
...
    provisioner "local-exec" {
        command = "nova --os-tenant-id ${lookup(var.tenant_ids,var.tenant)} --os-auth-url ${var.auth_url} lock ${self.id}"
    }
}

resource "openstack_blockstorage_volume_v1" "example_vdb" {
    region = "${var.region}"
    name = "example_vdb"
    description = "Example storage volume"
    size = 50
    volume_type = "${var.volume_type}"
    availability_zone = "${var.availability_zone}"
    provisioner "local-exec" {
      command =
        "cinder --os-tenant-id ${lookup(var.tenant_ids,var.tenant)} --os-auth-url ${var.auth_url} snapshot-create ${self.id} --display-name '${self.name}-protect' --display-description 'Prevent deletion of ${self.name} (${self.id})'"
    }
}

@alex

Reply all
Reply to author
Forward
0 new messages