github.com/hartzell/terraform@v0.8.6-0.20180503104400-0cc9e050ecd4/website/docs/backends/config.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Backends: Configuration" 4 sidebar_current: "docs-backends-config" 5 description: |- 6 Backends are configured directly in Terraform files in the `terraform` section. 7 --- 8 9 # Backend Configuration 10 11 Backends are configured directly in Terraform files in the `terraform` 12 section. After configuring a backend, it has to be 13 [initialized](/docs/backends/init.html). 14 15 Below, we show a complete example configuring the "consul" backend: 16 17 ```hcl 18 terraform { 19 backend "consul" { 20 address = "demo.consul.io" 21 path = "example_app/terraform_state" 22 } 23 } 24 ``` 25 26 You specify the backend type as a key to the `backend` stanza. Within the 27 stanza are backend-specific configuration keys. The list of supported backends 28 and their configuration is in the sidebar to the left. 29 30 Only one backend may be specified and the configuration **may not contain 31 interpolations**. Terraform will validate this. 32 33 ## First Time Configuration 34 35 When configuring a backend for the first time (moving from no defined backend 36 to explicitly configuring one), Terraform will give you the option to migrate 37 your state to the new backend. This lets you adopt backends without losing 38 any existing state. 39 40 To be extra careful, we always recommend manually backing up your state 41 as well. You can do this by simply copying your `terraform.tfstate` file 42 to another location. The initialization process should create a backup 43 as well, but it never hurts to be safe! 44 45 Configuring a backend for the first time is no different than changing 46 a configuration in the future: create the new configuration and run 47 `terraform init`. Terraform will guide you the rest of the way. 48 49 ## Partial Configuration 50 51 You do not need to specify every required argument in the backend configuration. 52 Omitting certain arguments may be desirable to avoid storing secrets, such as 53 access keys, within the main configuration. When some or all of the arguments 54 are omitted, we call this a _partial configuration_. 55 56 With a partial configuration, the remaining configuration arguments must be 57 provided as part of 58 [the initialization process](/docs/backends/init.html#backend-initialization). 59 There are several ways to supply the remaining arguments: 60 61 * **Interactively**: Terraform will interactively ask you for the required 62 values, unless interactive input is disabled. Terraform will not prompt for 63 optional values. 64 65 * **File**: A configuration file may be specified via the `init` command line. 66 To specify a file, use the `-backend-config=PATH` option when running 67 `terraform init`. If the file contains secrets it may be kept in 68 a secure data store, such as 69 [Vault](https://www.vaultproject.io/), in which case it must be downloaded 70 to the local disk before running Terraform. 71 72 * **Command-line key/value pairs**: Key/value pairs can be specified via the 73 `init` command line. Note that many shells retain command-line flags in a 74 history file, so this isn't recommended for secrets. To specify a single 75 key/value pair, use the `-backend-config="KEY=VALUE"` option when running 76 `terraform init`. 77 78 If backend settings are provided in multiple locations, the top-level 79 settings are merged such that any command-line options override the settings 80 in the main configuration and then the command-line options are processed 81 in order, with later options overriding values set by earlier options. 82 83 The final, merged configuration is stored on disk in the `.terraform` 84 directory, which should be ignored from version control. This means that 85 sensitive information can be omitted from version control, but it will be 86 present in plain text on local disk when running Terraform. 87 88 When using partial configuration, Terraform requires at a minimum that 89 an empty backend configuration is specified in one of the root Terraform 90 configuration files, to specify the backend type. For example: 91 92 ```hcl 93 terraform { 94 backend "consul" {} 95 } 96 ``` 97 98 A backend configuration file has the contents of the `backend` block as 99 top-level attributes, without the need to wrap it in another `terraform` 100 or `backend` block: 101 102 ```hcl 103 address = "demo.consul.io" 104 path = "example_app/terraform_state" 105 ``` 106 107 The same settings can alternatively be specified on the command line as 108 follows: 109 110 ``` 111 $ terraform init \ 112 -backend-config="address=demo.consul.io" \ 113 -backend-config="path=example_app/terraform_state" 114 ``` 115 116 ## Changing Configuration 117 118 You can change your backend configuration at any time. You can change 119 both the configuration itself as well as the type of backend (for example 120 from "consul" to "s3"). 121 122 Terraform will automatically detect any changes in your configuration 123 and request a [reinitialization](/docs/backends/init.html). As part of 124 the reinitialization process, Terraform will ask if you'd like to migrate 125 your existing state to the new configuration. This allows you to easily 126 switch from one backend to another. 127 128 If you're using multiple [workspaces](/docs/state/workspaces.html), 129 Terraform can copy all workspaces to the destination. If Terraform detects 130 you have multiple workspaces, it will ask if this is what you want to do. 131 132 If you're just reconfiguring the same backend, Terraform will still ask if you 133 want to migrate your state. You can respond "no" in this scenario. 134 135 ## Unconfiguring a Backend 136 137 If you no longer want to use any backend, you can simply remove the 138 configuration from the file. Terraform will detect this like any other 139 change and prompt you to [reinitialize](/docs/backends/init.html). 140 141 As part of the reinitialization, Terraform will ask if you'd like to migrate 142 your state back down to normal local state. Once this is complete then 143 Terraform is back to behaving as it does by default.