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