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

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