github.com/hugorut/terraform@v1.1.3/website/docs/language/state/workspaces.mdx (about)

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