github.com/hugorut/terraform@v1.1.3/website/docs/language/state/purpose.mdx (about)

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