github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/state/workspaces.html.md (about) 1 --- 2 layout: "language" 3 page_title: "State: Workspaces" 4 sidebar_current: "docs-state-workspaces" 5 description: |- 6 Workspaces allow the use of multiple states with a single configuration directory. 7 --- 8 9 # Workspaces 10 11 Each Terraform configuration has an associated [backend](/docs/language/settings/backends/index.html) 12 that defines how operations are executed and where persistent data such as 13 [the Terraform state](https://www.terraform.io/docs/language/state/purpose.html) are 14 stored. 15 16 The persistent data stored in the backend belongs to a _workspace_. Initially 17 the backend has only one workspace, called "default", and thus there is only 18 one Terraform state associated with that configuration. 19 20 Certain backends support _multiple_ named workspaces, allowing multiple states 21 to be associated with a single configuration. The configuration still 22 has only one backend, but multiple distinct instances of that configuration 23 to be deployed without configuring a new backend or changing authentication 24 credentials. 25 26 Multiple workspaces are currently supported by the following backends: 27 28 * [AzureRM](/docs/language/settings/backends/azurerm.html) 29 * [Consul](/docs/language/settings/backends/consul.html) 30 * [COS](/docs/language/settings/backends/cos.html) 31 * [GCS](/docs/language/settings/backends/gcs.html) 32 * [Kubernetes](/docs/language/settings/backends/kubernetes.html) 33 * [Local](/docs/language/settings/backends/local.html) 34 * [Manta](/docs/language/settings/backends/manta.html) 35 * [Postgres](/docs/language/settings/backends/pg.html) 36 * [Remote](/docs/language/settings/backends/remote.html) 37 * [S3](/docs/language/settings/backends/s3.html) 38 39 In the 0.9 line of Terraform releases, this concept was known as "environment". 40 It was renamed in 0.10 based on feedback about confusion caused by the 41 overloading of the word "environment" both within Terraform itself and within 42 organizations that use Terraform. 43 44 -> **Note**: The Terraform CLI workspace concept described in this document is 45 different from but related to the Terraform Cloud 46 [workspace](/docs/cloud/workspaces/index.html) concept. 47 If you use multiple Terraform CLI workspaces in a single Terraform configuration 48 and are migrating that configuration to Terraform Cloud, see this [migration 49 document](/docs/cloud/migrate/workspaces.html). 50 51 ## Using Workspaces 52 53 Terraform starts with a single workspace named "default". This 54 workspace is special both because it is the default and also because 55 it cannot ever be deleted. If you've never explicitly used workspaces, then 56 you've only ever worked on the "default" workspace. 57 58 Workspaces are managed with the `terraform workspace` set of commands. To 59 create a new workspace and switch to it, you can use `terraform workspace new`; 60 to switch workspaces you can use `terraform workspace select`; etc. 61 62 For example, creating a new workspace: 63 64 ```text 65 $ terraform workspace new bar 66 Created and switched to workspace "bar"! 67 68 You're now on a new, empty workspace. Workspaces isolate their state, 69 so if you run "terraform plan" Terraform will not see any existing state 70 for this configuration. 71 ``` 72 73 As the command says, if you run `terraform plan`, Terraform will not see 74 any existing resources that existed on the default (or any other) workspace. 75 **These resources still physically exist,** but are managed in another 76 Terraform workspace. 77 78 ## Current Workspace Interpolation 79 80 Within your Terraform configuration, you may include the name of the current 81 workspace using the `${terraform.workspace}` interpolation sequence. This can 82 be used anywhere interpolations are allowed. However, it should **not** be 83 used in remote operations against Terraform Cloud workspaces. For an 84 explanation, see the [remote backend](/docs/language/settings/backends/remote.html#workspaces) 85 document. 86 87 Referencing the current workspace is useful for changing behavior based 88 on the workspace. For example, for non-default workspaces, it may be useful 89 to spin up smaller cluster sizes. For example: 90 91 ```hcl 92 resource "aws_instance" "example" { 93 count = "${terraform.workspace == "default" ? 5 : 1}" 94 95 # ... other arguments 96 } 97 ``` 98 99 Another popular use case is using the workspace name as part of naming or 100 tagging behavior: 101 102 ```hcl 103 resource "aws_instance" "example" { 104 tags = { 105 Name = "web - ${terraform.workspace}" 106 } 107 108 # ... other arguments 109 } 110 ``` 111 112 ## When to use Multiple Workspaces 113 114 Named workspaces allow conveniently switching between multiple instances of 115 a _single_ configuration within its _single_ backend. They are convenient in 116 a number of situations, but cannot solve all problems. 117 118 A common use for multiple workspaces is to create a parallel, distinct copy of 119 a set of infrastructure in order to test a set of changes before modifying the 120 main production infrastructure. For example, a developer working on a complex 121 set of infrastructure changes might create a new temporary workspace in order 122 to freely experiment with changes without affecting the default workspace. 123 124 Non-default workspaces are often related to feature branches in version control. 125 The default workspace might correspond to the "main" or "trunk" branch, 126 which describes the intended state of production infrastructure. When a 127 feature branch is created to develop a change, the developer of that feature 128 might create a corresponding workspace and deploy into it a temporary "copy" 129 of the main infrastructure so that changes can be tested without affecting 130 the production infrastructure. Once the change is merged and deployed to the 131 default workspace, the test infrastructure can be destroyed and the temporary 132 workspace deleted. 133 134 When Terraform is used to manage larger systems, teams should use multiple 135 separate Terraform configurations that correspond with suitable architectural 136 boundaries within the system so that different components can be managed 137 separately and, if appropriate, by distinct teams. Workspaces _alone_ 138 are not a suitable tool for system decomposition, because each subsystem should 139 have its own separate configuration and backend, and will thus have its own 140 distinct set of workspaces. 141 142 In particular, organizations commonly want to create a strong separation 143 between multiple deployments of the same infrastructure serving different 144 development stages (e.g. staging vs. production) or different internal teams. 145 In this case, the backend used for each deployment often belongs to that 146 deployment, with different credentials and access controls. Named workspaces 147 are _not_ a suitable isolation mechanism for this scenario. 148 149 Instead, use one or more [re-usable modules](/docs/language/modules/develop/index.html) to 150 represent the common elements, and then represent each instance as a separate 151 configuration that instantiates those common elements in the context of a 152 different backend. In that case, the root module of each configuration will 153 consist only of a backend configuration and a small number of `module` blocks 154 whose arguments describe any small differences between the deployments. 155 156 Where multiple configurations are representing distinct system components 157 rather than multiple deployments, data can be passed from one component to 158 another using paired resources types and data sources. For example: 159 160 * Where a shared [Consul](https://consul.io/) cluster is available, use 161 [`consul_key_prefix`](https://registry.terraform.io/providers/hashicorp/consul/latest/docs/resources/key_prefix) to 162 publish to the key/value store and [`consul_keys`](https://registry.terraform.io/providers/hashicorp/consul/latest/docs/data-sources/keys) 163 to retrieve those values in other configurations. 164 165 * In systems that support user-defined labels or tags, use a tagging convention 166 to make resources automatically discoverable. For example, use 167 [the `aws_vpc` resource type](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) 168 to assign suitable tags and then 169 [the `aws_vpc` data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) 170 to query by those tags in other configurations. 171 172 * For server addresses, use a provider-specific resource to create a DNS 173 record with a predictable name and then either use that name directly or 174 use [the `dns` provider](https://registry.terraform.io/providers/hashicorp/dns/latest/docs) to retrieve 175 the published addresses in other configurations. 176 177 * If a Terraform state for one configuration is stored in a remote backend 178 that is accessible to other configurations then 179 [`terraform_remote_state`](/docs/language/state/remote-state-data.html) 180 can be used to directly consume its root module outputs from those other 181 configurations. This creates a tighter coupling between configurations, 182 but avoids the need for the "producer" configuration to explicitly 183 publish its results in a separate system. 184 185 ## Workspace Internals 186 187 Workspaces are technically equivalent to renaming your state file. They 188 aren't any more complex than that. Terraform wraps this simple notion with 189 a set of protections and support for remote state. 190 191 For local state, Terraform stores the workspace states in a directory called 192 `resource_state.json.d`. This directory should be treated similarly to 193 local-only `resource_state.json`; some teams commit these files to version 194 control, although using a remote backend instead is recommended when there are 195 multiple collaborators. 196 197 For [remote state](/docs/language/state/remote.html), the workspaces are stored 198 directly in the configured [backend](/docs/language/settings/backends/index.html). For example, if you 199 use [Consul](/docs/language/settings/backends/consul.html), the workspaces are stored 200 by appending the workspace name to the state path. To ensure that 201 workspace names are stored correctly and safely in all backends, the name 202 must be valid to use in a URL path segment without escaping. 203 204 The important thing about workspace internals is that workspaces are 205 meant to be a shared resource. They aren't a private, local-only notion 206 (unless you're using purely local state and not committing it). 207 208 The "current workspace" name is stored only locally in the ignored 209 `.terraform` directory. This allows multiple team members to work on 210 different workspaces concurrently. The "current workspace" name is **not** 211 currently meaningful in Terraform Cloud workspaces since it will always 212 have the value `default`.