2018-07-04 17:24:49 +02:00
---
2021-12-15 03:41:17 +01:00
page_title: 'Backend Type: remote'
description: >-
Terraform can store the state and run operations remotely, making it easier to
version and work with in a team.
2018-07-04 17:24:49 +02:00
---
# remote
2021-12-15 03:41:17 +01:00
-> **Note:** The remote backend was introduced in Terraform v0.11.13 and Terraform Enterprise v201809-1. As of Terraform v1.1.0 and Terraform Enterprise v202201-1, **we recommend using the Terraform Cloud's built-in [`cloud` integration](/language/settings/terraform-cloud)** instead of this backend. The `cloud` option includes an improved user experience and more features.
2018-08-17 07:40:51 +02:00
2021-12-15 03:41:17 +01:00
The remote backend is unique among all other Terraform backends because it can both store state snapshots and execute operations for Terraform Cloud's [CLI-driven run workflow](/cloud-docs/run/cli). It used to be called an "enhanced" backend.
2019-09-27 02:12:51 +02:00
When using full remote operations, operations like `terraform plan` or `terraform apply` can be executed in Terraform
2021-12-07 22:07:22 +01:00
Cloud's run environment, with log output streaming to the local terminal. Remote plans and applies use variable values from the associated Terraform Cloud workspace.
2019-09-27 02:12:51 +02:00
2022-02-09 20:10:11 +01:00
You can also use Terraform Cloud with local operations, in which case only state is stored in the Terraform Cloud backend.
2019-03-19 20:41:42 +01:00
2018-08-17 07:40:51 +02:00
## Command Support
2018-07-04 17:24:49 +02:00
Currently the remote backend supports the following Terraform commands:
2018-10-15 19:57:42 +02:00
- `apply`
2019-03-19 20:41:42 +01:00
- `console` (supported in Terraform >= v0.11.12)
2021-01-06 16:53:43 +01:00
- `destroy`
2018-08-17 07:40:51 +02:00
- `fmt`
- `get`
2019-03-19 20:41:42 +01:00
- `graph` (supported in Terraform >= v0.11.12)
- `import` (supported in Terraform >= v0.11.12)
2018-08-17 07:40:51 +02:00
- `init`
- `output`
- `plan`
- `providers`
- `show`
2019-08-16 17:45:27 +02:00
- `state` (supports all sub-commands: list, mv, pull, push, rm, show)
2018-08-17 07:40:51 +02:00
- `taint`
- `untaint`
- `validate`
- `version`
- `workspace`
## Workspaces
2022-02-09 20:10:11 +01:00
The remote backend can work with either a single remote Terraform Cloud workspace, or with multiple similarly-named remote workspaces (like `networking-dev` and `networking-prod`). The `workspaces` block of the backend configuration
2018-10-15 19:57:42 +02:00
determines which mode it uses:
2018-08-17 07:40:51 +02:00
2019-11-07 02:03:20 +01:00
- To use a single remote Terraform Cloud workspace, set `workspaces.name` to the
2022-02-09 23:37:06 +01:00
remote workspace's full name (like `networking-prod`).
2018-08-17 07:40:51 +02:00
2019-11-07 02:03:20 +01:00
- To use multiple remote workspaces, set `workspaces.prefix` to a prefix used in
2018-08-17 07:40:51 +02:00
all of the desired remote workspace names. For example, set
2019-11-07 02:03:20 +01:00
`prefix = "networking-"` to use Terraform cloud workspaces with
names like `networking-dev` and `networking-prod`. This is helpful when
2021-12-15 03:41:17 +01:00
mapping multiple Terraform CLI [workspaces](/language/state/workspaces)
2019-11-07 02:03:20 +01:00
used in a single Terraform configuration to multiple Terraform Cloud
workspaces.
2018-08-17 07:40:51 +02:00
The backend configuration requires either `name` or `prefix`. Omitting both or
setting both results in a configuration error.
If previous state is present when you run `terraform init` and the corresponding
2022-02-09 20:10:11 +01:00
remote workspaces are empty or absent, Terraform will create workspaces and
update the remote state accordingly. However, if your workspace requires variables or a specific version of Terraform for remote operations, we
2019-11-07 02:03:20 +01:00
recommend that you create your remote workspaces on Terraform Cloud before
running any remote operations against them.
2018-07-04 17:24:49 +02:00
2022-02-09 20:10:11 +01:00
### Workspace Names
Terraform uses shortened names without the common prefix to interact with workspaces on the command line. For example, if `prefix = "networking-"`, use `terraform workspace select prod` to switch to the Terraform CLI workspace `prod` within the current configuration. However, remote Terraform operations such as `plan` and `apply` for that Terraform CLI workspace will take place in the Terraform Cloud workspace `networking-prod`.
Because of this, the [`terraform.workspace`](/language/state/workspaces#current-workspace-interpolation) interpolation expression produces different results depending on whether a remote workspace is configured to perform operations locally or remotely. For example, in a remote workspace called `networking-prod` created with `prefix = "networking-"` the expression produces the following:
- For local operations, `terraform.workspace` = `prod`
- For remote operations, `terraform.workspace`= `networking-prod`
Prior to Terraform version 1.1.0, Terraform Cloud workspaces used only the single `default` Terraform CLI workspace internally. So if a Terraform configuration used `terraform.workspace` to return `dev` or `prod`, remote runs in Terraform Cloud would always evaluate it as `default`, regardless of
which workspace you set with the `terraform workspace select` command. Therefore, we do not recommend using `terraform.workspace` in Terraform configurations that use Terraform 1.0.x or earlier and run remote operations against Terraform Cloud workspaces.
### Determining Run Environment
2022-02-09 23:41:12 +01:00
If you need to determine whether a run is local or remote in your Terraform configuration, we recommend using [Terraform Cloud run environment variables](/cloud-docs/run/run-environment#environment-variables). The example below uses `TFC_RUN_ID`.
2022-02-09 20:10:11 +01:00
```
output "current_workspace_name" {
value = terraform.workspace
}
variable "TFC_RUN_ID" {
type = string
default = ""
}
output "remote_execution_determine" {
value = "Remote run environment? %{if var.TFC_RUN_ID != ""}Yes%{else}No this is local%{endif}!"
}
```
2020-02-19 01:40:14 +01:00
## Example Configurations
2020-02-19 01:28:12 +01:00
-> **Note:** We recommend omitting the token from the configuration, and instead using
2021-12-15 03:41:17 +01:00
[`terraform login`](/cli/commands/login) or manually configuring
`credentials` in the [CLI config file](/cli/config/config-file#credentials).
2020-02-19 01:28:12 +01:00
### Basic Configuration
2018-07-04 17:24:49 +02:00
```hcl
2018-08-17 07:40:51 +02:00
# Using a single workspace:
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "company"
workspaces {
name = "my-app-prod"
}
}
}
# Using multiple workspaces:
2018-07-04 17:24:49 +02:00
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "company"
workspaces {
prefix = "my-app-"
}
}
}
```
2020-02-19 01:28:12 +01:00
### Using CLI Input
2019-06-27 20:53:35 +02:00
```hcl
2019-08-07 00:28:03 +02:00
# main.tf
2019-06-27 20:53:35 +02:00
terraform {
required_version = "~> 0.12.0"
backend "remote" {}
}
```
Backend configuration file:
```hcl
2021-06-18 18:20:00 +02:00
# config.remote.tfbackend
2019-06-27 20:53:35 +02:00
workspaces { name = "workspace" }
hostname = "app.terraform.io"
organization = "company"
```
Running `terraform init` with the backend file:
```sh
2021-06-18 18:20:00 +02:00
terraform init -backend-config=config.remote.tfbackend
2019-06-27 20:53:35 +02:00
```
2020-02-19 01:40:14 +01:00
### Data Source Configuration
2020-02-19 01:28:12 +01:00
```hcl
data "terraform_remote_state" "foo" {
backend = "remote"
config = {
organization = "company"
workspaces = {
name = "workspace"
}
}
}
```
2018-07-04 17:24:49 +02:00
## Configuration variables
The following configuration options are supported:
2021-12-15 03:41:17 +01:00
- `hostname` - (Optional) The remote backend hostname to connect to. Defaults
2018-07-04 17:24:49 +02:00
to app.terraform.io.
2021-12-15 03:41:17 +01:00
- `organization` - (Required) The name of the organization containing the
2018-07-04 17:24:49 +02:00
targeted workspace(s).
2021-12-15 03:41:17 +01:00
- `token` - (Optional) The token used to authenticate with the remote backend.
2020-02-04 17:30:40 +01:00
We recommend omitting the token from the configuration, and instead using
2021-12-15 03:41:17 +01:00
[`terraform login`](/cli/commands/login) or manually configuring
2020-02-04 17:30:40 +01:00
`credentials` in the
2021-12-15 03:41:17 +01:00
[CLI config file](/cli/config/config-file#credentials).
- `workspaces` - (Required) A block specifying which remote workspace(s) to use.
2018-08-17 07:40:51 +02:00
The `workspaces` block supports the following keys:
2021-12-15 03:41:17 +01:00
- `name` - (Optional) The full name of one remote workspace. When configured,
2018-08-17 07:40:51 +02:00
only the default workspace can be used. This option conflicts with `prefix`.
2021-12-15 03:41:17 +01:00
- `prefix` - (Optional) A prefix used in the names of one or more remote
2018-08-17 07:40:51 +02:00
workspaces, all of which can be used with this configuration. The full
2019-08-07 00:28:03 +02:00
workspace names are used in Terraform Cloud, and the short names
2019-11-07 02:03:20 +01:00
(minus the prefix) are used on the command line for Terraform CLI workspaces.
If omitted, only the default workspace can be used. This option conflicts with `name`.
2021-12-07 22:07:22 +01:00
2020-02-19 01:28:12 +01:00
-> **Note:** You must use the `name` key when configuring a `terraform_remote_state`
2019-11-07 02:03:20 +01:00
data source that retrieves state from another Terraform Cloud workspace. The `prefix` key is only
intended for use when configuring an instance of the remote backend.
2019-10-16 16:15:50 +02:00
2021-05-11 20:37:32 +02:00
## Command Line Arguments
For configurations that include a `backend "remote"` block, commands that
make local modifications to Terraform state and then push them back up to
the remote workspace accept the following option to modify that behavior:
2021-12-15 03:41:17 +01:00
- `-ignore-remote-version` - Override checking that the local and remote
2021-05-11 20:37:32 +02:00
Terraform versions agree, making an operation proceed even when there is
a mismatch.
2021-12-15 03:41:17 +01:00
Normally state-modification operations require using a local version of
Terraform CLI which is compatible with the Terraform version selected
for the remote workspace as part of its settings. This is to avoid the
local operation creating a new state snapshot which the workspace's
remote execution environment would then be unable to decode.
2021-05-11 20:37:32 +02:00
2021-12-15 03:41:17 +01:00
Overriding this check can result in a Terraform Cloud workspace that is
no longer able to complete remote operations, so we recommend against
using this option.
2021-05-11 20:37:32 +02:00
2019-10-16 16:15:50 +02:00
## Excluding Files from Upload with .terraformignore
-> **Version note:** `.terraformignore` support was added in Terraform 0.12.11.
2021-12-15 03:41:17 +01:00
When executing a remote `plan` or `apply` in a [CLI-driven run](/cloud-docs/run/cli),
2019-10-16 16:15:50 +02:00
an archive of your configuration directory is uploaded to Terraform Cloud. You can define
paths to ignore from upload via a `.terraformignore` file at the root of your configuration directory. If this file is not present, the archive will exclude the following by default:
2021-12-15 03:41:17 +01:00
- `.git/` directories
- `.terraform/` directories (exclusive of `.terraform/modules`)
2019-10-16 16:15:50 +02:00
The `.terraformignore` file can include rules as one would include in a
2021-11-25 16:35:03 +01:00
[`.gitignore` file](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring)
2019-10-16 16:15:50 +02:00
2021-12-15 03:41:17 +01:00
- Comments (starting with `#`) or blank lines are ignored
- End a pattern with a forward slash `/` to specify a directory
- Negate a pattern by starting it with an exclamation point `!`
2019-10-16 16:15:50 +02:00
Note that unlike `.gitignore`, only the `.terraformignore` at the root of the configuration
directory is considered.