Search code examples
terraformoracle-cloud-infrastructure

Terraform OCI, how to prevent deletion of a core volume attached to compute?


How do you prevent the deletion of a core volume attached to the instance in OCI using Terraform?

Using Terraform v1.1.6, I noticed with the provider AWS you can enable enable_deletion_protection, but on OCI there is no such option, for example:

resource "oci_core_volume" "volume_1" {
  count               = 1
  availability_domain = data.oci_identity_availability_domains.ad_list.availability_domains[0]["name"]
  compartment_id      = "test"
  display_name        = "test_disk"
  size_in_gbs         = 50
  vpus_per_gb         = 10
}

I have looked at lifecycle events, but the documentation for oci_core_volume has no examples of their usage on oci_core_volume

lifecycle {

}

I want to make sure that any updates to my "oci_core_instance" instance do not delete the "oci_core_volume" which they currectly do, instead updates should reattach.


Solution

  • The oci_core_volume documentation does not give you an example because lifecycle events are meta-arguments and their usage is the same for every type of resource. You can find examples which do apply for all kind of resources in the lifecycle documentation.

    An example of usage would in your case would be:

    resource "oci_core_volume" "volume_1" {
      count               = 1
      availability_domain = data.oci_identity_availability_domains.ad_list.availability_domains[0]["name"]
      compartment_id      = "test"
      display_name        = "test_disk"
      size_in_gbs         = 50
      vpus_per_gb         = 10
    
      lifecycle {
        prevent_destroy = true
      }
    }
    

    Just keep in mind that prevent_destroy will throw an error whenever the resource is marked for destruction. You will still be able to destroy the resource manually from the outside.