github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/website/docs/language/settings/backends/configuration.mdx (about) 1 --- 2 page_title: Backend Configuration - Configuration Language 3 description: >- 4 Use the `backend` block to control where Terraform stores state. Learn about the available state backends, the backend block, initializing backends, partial backend configuration, changing backend configuration, and unconfiguring a backend. 5 --- 6 7 # Backend Configuration 8 9 A backend defines where Terraform stores its [state](/terraform/language/state) data files. 10 11 Terraform uses persisted state data to keep track of the resources it manages. Most non-trivial Terraform configurations either [integrate with Terraform Cloud](/terraform/language/settings/terraform-cloud) or use a backend to store state remotely. This lets multiple people access the state data and work together on that collection of infrastructure resources. 12 13 This page describes how to configure a backend by adding the [`backend` block](#using-a-backend-block) to your configuration. 14 15 -> **Note:** In Terraform versions before 1.1.0, we classified backends as standard or enhanced. The enhanced label differentiated the [`remote` backend](/terraform/language/settings/backends/remote), which could both store state and perform Terraform operations. This classification has been removed. Refer to [Using Terraform Cloud](/terraform/cli/cloud) for details about storing state, executing remote operations, and using Terraform Cloud directly from Terraform. 16 17 ## Available Backends 18 19 By default, Terraform uses a backend called [`local`](/terraform/language/settings/backends/local), which stores state as a local file on disk. You can also configure one of the built-in backends included in this documentation. 20 21 Some of these backends act like plain remote disks for state files, while others support locking the state while operations are being performed. This helps prevent conflicts and inconsistencies. The built-in backends listed are the only backends. You cannot load additional backends as plugins. 22 23 -> **Note:** We removed the `artifactory`, `etcd`, `etcdv3`, `manta`, and `swift` backends in Terraform v1.3. Information about their behavior in older versions is still available in the [Terraform v1.2 documentation](/terraform/language/v1.2.x/settings/backends/configuration). For migration paths from these removed backends, refer to [Upgrading to Terraform v1.3](/terraform/language/v1.3.x/upgrade-guides). 24 25 ## Using a Backend Block 26 27 You do not need to configure a backend when using Terraform Cloud because 28 Terraform Cloud automatically manages state in the workspaces associated with your configuration. If your configuration includes a [`cloud` block](/terraform/language/settings/terraform-cloud), it cannot include a `backend` block. 29 30 To configure a backend, add a nested `backend` block within the top-level 31 `terraform` block. The following example configures the `remote` backend. 32 33 ```hcl 34 terraform { 35 backend "remote" { 36 organization = "example_corp" 37 38 workspaces { 39 name = "my-app-prod" 40 } 41 } 42 } 43 ``` 44 45 There are some important limitations on backend configuration: 46 47 - A configuration can only provide one backend block. 48 - A backend block cannot refer to named values (like input variables, locals, or data source attributes). 49 50 ### Credentials and Sensitive Data 51 52 Backends store state in a remote service, which allows multiple people to access it. Accessing remote state generally requires access credentials, since state data contains extremely sensitive information. 53 54 !> **Warning:** We recommend using environment variables to supply credentials and other sensitive data. If you use `-backend-config` or hardcode these values directly in your configuration, Terraform will include these values in both the `.terraform` subdirectory and in plan files. This can leak sensitive credentials. 55 56 Terraform writes the backend configuration in plain text in two separate files. 57 - The `.terraform/terraform.tfstate` file contains the backend configuration for the current working directory. 58 - All plan files capture the information in `.terraform/terraform.tfstate` at the time the plan was created. This helps ensure Terraform is applying the plan to correct set of infrastructure. 59 60 When applying a plan that you previously saved to a file, Terraform uses the backend configuration stored in that file instead of the current backend settings. If that configuration contains time-limited credentials, they may expire before you finish applying the plan. Use environment variables to pass credentials when you need to use different values between the plan and apply steps. 61 62 ### Backend Types 63 64 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. 65 66 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. 67 68 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 using the credentials files or environment variables that are conventional for the target system, as described in the documentation for each backend. 69 70 Refer to the page for each backend type for full details and that type's configuration arguments. 71 72 ### Default Backend 73 74 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. 75 76 ## Initialization 77 78 When you change a backend's configuration, you must run `terraform init` again 79 to validate and configure the backend before you can perform any plans, applies, 80 or state operations. 81 82 After you initialize, Terraform creates a `.terraform/` directory locally. This directory contains the most recent backend configuration, including any authentication parameters you provided to the Terraform CLI. Do not check this directory into Git, as it may contain sensitive credentials for your remote backend. 83 84 The local backend configuration is different and entirely separate from the `terraform.tfstate` file that contains [state data](/terraform/language/state) about your real-world infrastruture. Terraform stores the `terraform.tfstate` file in your remote backend. 85 86 When you change backends, Terraform gives you the option to migrate 87 your state to the new backend. This lets you adopt backends without losing 88 any existing state. 89 90 ~> **Important:** Before migrating to a new backend, we strongly recommend manually backing up your state by copying your `terraform.tfstate` file 91 to another location. 92 93 ## Partial Configuration 94 95 You do not need to specify every required argument in the backend configuration. 96 Omitting certain arguments may be desirable if some arguments are provided 97 automatically by an automation script running Terraform. When some or all of 98 the arguments are omitted, we call this a _partial configuration_. 99 100 With a partial configuration, the remaining configuration arguments must be 101 provided as part of [the initialization process](/terraform/cli/init). 102 103 There are several ways to supply the remaining arguments: 104 105 - **File**: A configuration file may be specified via the `init` command line. 106 To specify a file, use the `-backend-config=PATH` option when running 107 `terraform init`. If the file contains secrets it may be kept in 108 a secure data store, such as [Vault](https://www.vaultproject.io/), 109 in which case it must be downloaded to the local disk before running Terraform. 110 111 - **Command-line key/value pairs**: Key/value pairs can be specified via the 112 `init` command line. Note that many shells retain command-line flags in a 113 history file, so this isn't recommended for secrets. To specify a single 114 key/value pair, use the `-backend-config="KEY=VALUE"` option when running 115 `terraform init`. 116 117 - **Interactively**: Terraform will interactively ask you for the required 118 values, unless interactive input is disabled. Terraform will not prompt for 119 optional values. 120 121 If backend settings are provided in multiple locations, the top-level 122 settings are merged such that any command-line options override the settings 123 in the main configuration and then the command-line options are processed 124 in order, with later options overriding values set by earlier options. 125 126 The final, merged configuration is stored on disk in the `.terraform` 127 directory, which should be ignored from version control. This means that 128 sensitive information can be omitted from version control, but it will be 129 present in plain text on local disk when running Terraform. 130 131 When using partial configuration, Terraform requires at a minimum that 132 an empty backend configuration is specified in one of the root Terraform 133 configuration files, to specify the backend type. For example: 134 135 ```hcl 136 terraform { 137 backend "consul" {} 138 } 139 ``` 140 141 ### File 142 143 A backend configuration file has the contents of the `backend` block as 144 top-level attributes, without the need to wrap it in another `terraform` 145 or `backend` block: 146 147 ```hcl 148 address = "demo.consul.io" 149 path = "example_app/terraform_state" 150 scheme = "https" 151 ``` 152 153 `*.backendname.tfbackend` (e.g. `config.consul.tfbackend`) is the recommended 154 naming pattern. Terraform will not prevent you from using other names but following 155 this convention will help your editor understand the content and likely provide 156 better editing experience as a result. 157 158 ### Command-line key/value pairs 159 160 The same settings can alternatively be specified on the command line as 161 follows: 162 163 ``` 164 $ terraform init \ 165 -backend-config="address=demo.consul.io" \ 166 -backend-config="path=example_app/terraform_state" \ 167 -backend-config="scheme=https" 168 ``` 169 170 The Consul backend also requires a Consul access token. Per the recommendation 171 above of omitting credentials from the configuration and using other mechanisms, 172 the Consul token would be provided by setting either the `CONSUL_HTTP_TOKEN` 173 or `CONSUL_HTTP_AUTH` environment variables. See the documentation of your 174 chosen backend to learn how to provide credentials to it outside of its main 175 configuration. 176 177 ## Changing Configuration 178 179 You can change your backend configuration at any time. You can change 180 both the configuration itself as well as the type of backend (for example 181 from "consul" to "s3"). 182 183 Terraform will automatically detect any changes in your configuration 184 and request a [reinitialization](/terraform/cli/init). As part of 185 the reinitialization process, Terraform will ask if you'd like to migrate 186 your existing state to the new configuration. This allows you to easily 187 switch from one backend to another. 188 189 If you're using multiple [workspaces](/terraform/language/state/workspaces), 190 Terraform can copy all workspaces to the destination. If Terraform detects 191 you have multiple workspaces, it will ask if this is what you want to do. 192 193 If you're just reconfiguring the same backend, Terraform will still ask if you 194 want to migrate your state. You can respond "no" in this scenario. 195 196 ## Unconfiguring a Backend 197 198 If you no longer want to use any backend, you can simply remove the 199 configuration from the file. Terraform will detect this like any other 200 change and prompt you to [reinitialize](/terraform/cli/init). 201 202 As part of the reinitialization, Terraform will ask if you'd like to migrate 203 your state back down to normal local state. Once this is complete then 204 Terraform is back to behaving as it does by default.