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