github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/state/purpose.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "State"
     4  sidebar_current: "docs-state-purpose"
     5  description: |-
     6    Terraform must store state about your managed infrastructure and configuration. This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures.
     7  ---
     8  
     9  # Purpose of Terraform State
    10  
    11  State is a necessary requirement for Terraform to function. It is often
    12  asked if it is possible for Terraform to work without state, or for Terraform
    13  to not use state and just inspect cloud resources on every run. This page
    14  will help explain why Terraform state is required.
    15  
    16  As you'll see from the reasons below, state is required. And in the scenarios
    17  where Terraform may be able to get away without state, doing so would require
    18  shifting massive amounts of complexity from one place (state) to another place
    19  (the replacement concept).
    20  
    21  ## Mapping to the Real World
    22  
    23  Terraform requires some sort of database to map Terraform config to the real
    24  world. When you have a resource `resource "aws_instance" "foo"` in your
    25  configuration, Terraform uses this map to know that instance `i-abcd1234`
    26  is represented by that resource.
    27  
    28  For some providers like AWS, Terraform could theoretically use something like
    29  AWS tags. Early prototypes of Terraform actually had no state files and used
    30  this method. However, we quickly ran into problems. The first major issue was
    31  a simple one: not all resources support tags, and not all cloud providers
    32  support tags.
    33  
    34  Therefore, for mapping configuration to resources in the real world,
    35  Terraform uses its own state structure.
    36  
    37  Terraform expects that each remote object is bound to only one resource
    38  instance, which is normally guaranteed by Terraform being responsible for
    39  creating the objects and recording their identities in the state. If you
    40  instead import objects that were created outside of Terraform, you'll need
    41  to check yourself that each distinct object is imported to only one resource
    42  instance.
    43  
    44  If one remote object is bound to two or more resource instances then Terraform
    45  may take unexpected actions against those objects, because the mapping from
    46  configuration to the remote object state has become ambiguous.
    47  
    48  ## Metadata
    49  
    50  Alongside the mappings between resources and remote objects, Terraform must
    51  also track metadata such as resource dependencies.
    52  
    53  Terraform typically uses the configuration to determine dependency order.
    54  However, when you delete a resource from a Terraform configuration, Terraform
    55  must know how to delete that resource. Terraform can see that a mapping exists
    56  for a resource not in your configuration and plan to destroy. However, since
    57  the configuration no longer exists, the order cannot be determined from the
    58  configuration alone.
    59  
    60  To ensure correct operation, Terraform retains a copy of the most recent set
    61  of dependencies within the state. Now Terraform can still determine the correct
    62  order for destruction from the state when you delete one or more items from
    63  the configuration.
    64  
    65  One way to avoid this would be for Terraform to know a required ordering
    66  between resource types. For example, Terraform could know that servers must be
    67  deleted before the subnets they are a part of. The complexity for this approach
    68  quickly explodes, however: in addition to Terraform having to understand the
    69  ordering semantics of every resource for every cloud, Terraform must also
    70  understand the ordering _across providers_.
    71  
    72  Terraform also stores other metadata for similar reasons, such as a pointer
    73  to the provider configuration that was most recently used with the resource
    74  in situations where multiple aliased providers are present.
    75  
    76  ## Performance
    77  
    78  In addition to basic mapping, Terraform stores a cache of the attribute
    79  values for all resources in the state. This is the most optional feature of
    80  Terraform state and is done only as a performance improvement.
    81  
    82  When running a `terraform plan`, Terraform must know the current state of
    83  resources in order to effectively determine the changes that it needs to make
    84  to reach your desired configuration.
    85  
    86  For small infrastructures, Terraform can query your providers and sync the
    87  latest attributes from all your resources. This is the default behavior
    88  of Terraform: for every plan and apply, Terraform will sync all resources in
    89  your state.
    90  
    91  For larger infrastructures, querying every resource is too slow. Many cloud
    92  providers do not provide APIs to query multiple resources at once, and the
    93  round trip time for each resource is hundreds of milliseconds. On top of this,
    94  cloud providers almost always have API rate limiting so Terraform can only
    95  request a certain number of resources in a period of time. Larger users
    96  of Terraform make heavy use of the `-refresh=false` flag as well as the
    97  `-target` flag in order to work around this. In these scenarios, the cached
    98  state is treated as the record of truth.
    99  
   100  ## Syncing
   101  
   102  In the default configuration, Terraform stores the state in a file in the
   103  current working directory where Terraform was run. This is okay for getting
   104  started, but when using Terraform in a team it is important for everyone
   105  to be working with the same state so that operations will be applied to the
   106  same remote objects.
   107  
   108  [Remote state](/docs/language/state/remote.html) is the recommended solution
   109  to this problem. With a fully-featured state backend, Terraform can use
   110  remote locking as a measure to avoid two or more different users accidentally
   111  running Terraform at the same time, and thus ensure that each Terraform run
   112  begins with the most recent updated state.