github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/website/docs/internals/lifecycle.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Resource Lifecycle"
     4  sidebar_current: "docs-internals-lifecycle"
     5  description: |-
     6    Resources have a strict lifecycle, and can be thought of as basic state machines. Understanding this lifecycle can help better understand how Terraform generates an execution plan, how it safely executes that plan, and what the resource provider is doing throughout all of this.
     7  ---
     8  
     9  # Resource Lifecycle
    10  
    11  Resources have a strict lifecycle, and can be thought of as basic
    12  state machines. Understanding this lifecycle can help better understand
    13  how Terraform generates an execution plan, how it safely executes that
    14  plan, and what the resource provider is doing throughout all of this.
    15  
    16  ~> **Advanced Topic!** This page covers technical details
    17  of Terraform. You don't need to understand these details to
    18  effectively use Terraform. The details are documented here for
    19  those who wish to learn about them without having to go
    20  spelunking through the source code.
    21  
    22  ## Lifecycle
    23  
    24  A resource roughly follows the steps below:
    25  
    26    1. `ValidateResource` is called to do a high-level structural
    27       validation of a resource's configuration. The configuration
    28       at this point is raw and the interpolations have not been processed.
    29       The value of any key is not guaranteed and is just meant to be
    30       a quick structural check.
    31  
    32    1. `Diff` is called with the current state and the configuration.
    33       The resource provider inspects this and returns a diff, outlining
    34       all the changes that need to occur to the resource. The diff includes
    35       details such as whether or not the resource is being destroyed, what
    36       attribute necessitates the destroy, old values and new values, whether
    37       a value is computed, etc. It is up to the resource provider to
    38       have this knowledge.
    39  
    40    1. `Apply` is called with the current state and the diff. Apply does
    41       not have access to the configuration. This is a safety mechanism
    42       that limits the possibility that a provider changes a diff on the
    43       fly. `Apply` must apply a diff as prescribed and do nothing else
    44       to remain true to the Terraform execution plan. Apply returns the
    45       new state of the resource (or nil if the resource was destroyed).
    46  
    47    1. If a resource was just created and did not exist before, and the
    48       apply succeeded without error, then the provisioners are executed
    49       in sequence. If any provisioner errors, the resource is marked as
    50       _tainted_, so that it will be destroyed on the next apply.
    51  
    52  ## Partial State and Error Handling
    53  
    54  If an error happens at any stage in the lifecycle of a resource,
    55  Terraform stores a partial state of the resource. This behavior is
    56  critical for Terraform to ensure that you don't end up with any
    57  _zombie_ resources: resources that were created by Terraform but
    58  no longer managed by Terraform due to a loss of state.