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