Terraform Diary - Desired State and Current State scenarios with OCI

 


Desired state of a resource can be simply defined as the state that is mentioned in your resource block of tf file.

Current state of a resource is the actual configuration state for the resource. This may be different from desired state mentioned in .tf file.

 

Terraform will only synchronize current state(changes done manually in console) to desired state(changes mentioned in your tf file) for attributes that are explicitly mentioned in your tf file.

Case 1 –

Please consider below example, note that object bucket ‘test_bucket is already present on OCI –

resource "oci_objectstorage_bucket" "test_bucket" {

    #Required

    compartment_id = "${var.compartment_ocid}"

    name = "test_bucket"

    namespace = "${var.namespace}"

    versioning = "Enabled"

        }

  

We added versioning attribute in our tf file and current state for is bucket is as follows –



 Now we know desired state and current state (console) are different. Let us run terraform plan and observe –


      ~ versioning            = "Suspended" -> "Enabled"

    }

 

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

 

 

Case 2 –

Simply remove versioning from .tf file as follows, (commented it) –

resource "oci_objectstorage_bucket" "test_bucket" {

    #Required

    compartment_id = "${var.compartment_ocid}"

    name = "test_bucket"

    namespace = "${var.namespace}"

    #versioning = "Enabled"




This proves that terraform will only check and try to modify current state à desired state for attributes that are explicitly mentioned in you .tf file


Now what will happen if there is no explicit mention for an attribute that is modified manually in your OCI console?

In this scenario, terraform will not perform any action mentioning below -


Hence, it is important to mention attributes in your tf file to make sure terraform identifies discrepancies between desired state and current state.



No comments:

Post a Comment