github.com/hugorut/terraform@v1.1.3/website/docs/language/settings/backends/configuration.mdx (about)

     1  ---
     2  page_title: Backend Configuration - Configuration Language
     3  ---
     4  
     5  # Backend Configuration
     6  
     7  Each Terraform configuration can specify a backend, which defines where
     8  [state](/language/state) snapshots are stored.
     9  
    10  You do not need to configure a backend when using Terraform Cloud because
    11  Terraform Cloud automatically manages state in the workspaces associated with your configuration. If your configuration includes [a `cloud` block](/language/settings/terraform-cloud), it cannot include a `backend` block.
    12  
    13  Most non-trivial Terraform configurations store state remotely so that multiple
    14  people can work with the same infrastructure.
    15  
    16  ## Using a Backend Block
    17  
    18  Backends are configured with a nested `backend` block within the top-level
    19  `terraform` block:
    20  
    21  ```hcl
    22  terraform {
    23    backend "remote" {
    24      organization = "example_corp"
    25  
    26      workspaces {
    27        name = "my-app-prod"
    28      }
    29    }
    30  }
    31  ```
    32  
    33  There are some important limitations on backend configuration:
    34  
    35  - A configuration can only provide one backend block.
    36  - A backend block cannot refer to named values (like input variables, locals, or data source attributes).
    37  
    38  ### Backend Types
    39  
    40  The block label of the backend block (`"remote"`, in the example above) indicates which backend type to use. Terraform has a built-in selection of backends, and the configured backend must be available in the version of Terraform you are using.
    41  
    42  The arguments used in the block's body are specific to the chosen backend type; they configure where and how the backend will store the configuration's state, and in some cases configure other behavior.
    43  
    44  Some backends allow providing access credentials directly as part of the configuration for use in unusual situations, for pragmatic reasons. However, in normal use we _do not_ recommend including access credentials as part of the backend configuration. Instead, leave those arguments completely unset and provide credentials via the credentials files or environment variables that are conventional for the target system, as described in the documentation for each backend.
    45  
    46  See the list of backend types in the navigation sidebar for details about each supported backend type and its configuration arguments.
    47  
    48  ### Default Backend
    49  
    50  If a configuration includes no backend block, Terraform defaults to using the `local` backend, which stores state as a plain file in the current working directory.
    51  
    52  ## Initialization
    53  
    54  Whenever a configuration's backend changes, you must run `terraform init` again
    55  to validate and configure the backend before you can perform any plans, applies,
    56  or state operations.
    57  
    58  When changing backends, Terraform will give you the option to migrate
    59  your state to the new backend. This lets you adopt backends without losing
    60  any existing state.
    61  
    62  To be extra careful, we always recommend manually backing up your state
    63  as well. You can do this by simply copying your `terraform.tfstate` file
    64  to another location. The initialization process should create a backup
    65  as well, but it never hurts to be safe!
    66  
    67  ## Partial Configuration
    68  
    69  You do not need to specify every required argument in the backend configuration.
    70  Omitting certain arguments may be desirable if some arguments are provided
    71  automatically by an automation script running Terraform. When some or all of
    72  the arguments are omitted, we call this a _partial configuration_.
    73  
    74  With a partial configuration, the remaining configuration arguments must be
    75  provided as part of [the initialization process](/cli/init).
    76  
    77  There are several ways to supply the remaining arguments:
    78  
    79  - **File**: A configuration file may be specified via the `init` command line.
    80    To specify a file, use the `-backend-config=PATH` option when running
    81    `terraform init`. If the file contains secrets it may be kept in
    82    a secure data store, such as [Vault](https://www.vaultproject.io/),
    83    in which case it must be downloaded to the local disk before running Terraform.
    84  
    85  - **Command-line key/value pairs**: Key/value pairs can be specified via the
    86    `init` command line. Note that many shells retain command-line flags in a
    87    history file, so this isn't recommended for secrets. To specify a single
    88    key/value pair, use the `-backend-config="KEY=VALUE"` option when running
    89    `terraform init`.
    90  
    91  - **Interactively**: Terraform will interactively ask you for the required
    92    values, unless interactive input is disabled. Terraform will not prompt for
    93    optional values.
    94  
    95  If backend settings are provided in multiple locations, the top-level
    96  settings are merged such that any command-line options override the settings
    97  in the main configuration and then the command-line options are processed
    98  in order, with later options overriding values set by earlier options.
    99  
   100  The final, merged configuration is stored on disk in the `.terraform`
   101  directory, which should be ignored from version control. This means that
   102  sensitive information can be omitted from version control, but it will be
   103  present in plain text on local disk when running Terraform.
   104  
   105  When using partial configuration, Terraform requires at a minimum that
   106  an empty backend configuration is specified in one of the root Terraform
   107  configuration files, to specify the backend type. For example:
   108  
   109  ```hcl
   110  terraform {
   111    backend "consul" {}
   112  }
   113  ```
   114  
   115  ### File
   116  
   117  A backend configuration file has the contents of the `backend` block as
   118  top-level attributes, without the need to wrap it in another `terraform`
   119  or `backend` block:
   120  
   121  ```hcl
   122  address = "demo.consul.io"
   123  path    = "example_app/terraform_state"
   124  scheme  = "https"
   125  ```
   126  
   127  `*.backendname.tfbackend` (e.g. `config.consul.tfbackend`) is the recommended
   128  naming pattern. Terraform will not prevent you from using other names but following
   129  this convention will help your editor understand the content and likely provide
   130  better editing experience as a result.
   131  
   132  ### Command-line key/value pairs
   133  
   134  The same settings can alternatively be specified on the command line as
   135  follows:
   136  
   137  ```
   138  $ terraform init \
   139      -backend-config="address=demo.consul.io" \
   140      -backend-config="path=example_app/terraform_state" \
   141      -backend-config="scheme=https"
   142  ```
   143  
   144  The Consul backend also requires a Consul access token. Per the recommendation
   145  above of omitting credentials from the configuration and using other mechanisms,
   146  the Consul token would be provided by setting either the `CONSUL_HTTP_TOKEN`
   147  or `CONSUL_HTTP_AUTH` environment variables. See the documentation of your
   148  chosen backend to learn how to provide credentials to it outside of its main
   149  configuration.
   150  
   151  ## Changing Configuration
   152  
   153  You can change your backend configuration at any time. You can change
   154  both the configuration itself as well as the type of backend (for example
   155  from "consul" to "s3").
   156  
   157  Terraform will automatically detect any changes in your configuration
   158  and request a [reinitialization](/cli/init). As part of
   159  the reinitialization process, Terraform will ask if you'd like to migrate
   160  your existing state to the new configuration. This allows you to easily
   161  switch from one backend to another.
   162  
   163  If you're using multiple [workspaces](/language/state/workspaces),
   164  Terraform can copy all workspaces to the destination. If Terraform detects
   165  you have multiple workspaces, it will ask if this is what you want to do.
   166  
   167  If you're just reconfiguring the same backend, Terraform will still ask if you
   168  want to migrate your state. You can respond "no" in this scenario.
   169  
   170  ## Unconfiguring a Backend
   171  
   172  If you no longer want to use any backend, you can simply remove the
   173  configuration from the file. Terraform will detect this like any other
   174  change and prompt you to [reinitialize](/cli/init).
   175  
   176  As part of the reinitialization, Terraform will ask if you'd like to migrate
   177  your state back down to normal local state. Once this is complete then
   178  Terraform is back to behaving as it does by default.