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