2018-07-04 17:24:49 +02:00
---
2021-11-23 00:57:25 +01:00
layout: "language"
page_title: "Backend Type: remote"
sidebar_current: "docs-backends-types-enhanced-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-08 18:49:12 +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](/docs/language/settings/terraform-cloud.html)** instead of this backend. The `cloud` option includes an improved user experience and more features.
2018-07-04 17:24:49 +02:00
2021-12-08 18:43:18 +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 ](/docs/cloud/run/cli.html ). It used to be called an "enhanced" backend.
2021-12-07 22:07:22 +01:00
2018-08-17 07:40:51 +02:00
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
Terraform Cloud can also be used 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
2019-11-07 02:03:20 +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
remote workspace's full name (like `networking` ).
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-11-23 00:57:25 +01:00
mapping multiple Terraform CLI [workspaces ](/docs/language/state/workspaces.html )
2019-11-07 02:03:20 +01:00
used in a single Terraform configuration to multiple Terraform Cloud
workspaces.
When interacting with workspaces on the command line, Terraform uses
shortened names without the common prefix. For example, if
`prefix = "networking-"` , use `terraform workspace select prod` to switch to
the Terraform CLI workspace `prod` within the current configuration. Remote
Terraform operations such as `plan` and `apply` executed against that Terraform
CLI workspace will be executed in the Terraform Cloud workspace `networking-prod` .
2021-12-08 23:01:45 +01:00
Additionally, the [`terraform.workspace` ](/docs/language/state/workspaces.html#current-workspace-interpolation )
2021-12-07 22:07:22 +01:00
expression shouldn't be used in Terraform configurations that use Terraform 1.0.x or earlier and run
2019-11-07 02:03:20 +01:00
remote operations against Terraform Cloud workspaces. The reason for this is that
2021-12-07 22:07:22 +01:00
prior to Terraform 1.1.0, Terraform Cloud workspaces only used the single `default` Terraform
2019-11-07 02:03:20 +01:00
CLI workspace internally. In other words, if your 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 had set with the `terraform workspace select` command. That
2021-12-07 22:07:22 +01:00
would most likely not be what you wanted. (It is ok to use `terraform.workspace`
in local operations, and with remote operations in workspaces configured to use Terraform 1.1.0 or later.)
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
remote workspaces are empty or absent, Terraform will create workspaces and/or
2019-11-07 02:03:20 +01:00
update the remote state accordingly. However, if your workspace needs variables
set or requires a specific version of Terraform for remote operations, we
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
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-11-23 00:57:25 +01:00
[`terraform login` ](/docs/cli/commands/login.html ) or manually configuring
`credentials` in the [CLI config file ](/docs/cli/config/config-file.html#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-11-23 00:57:25 +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-11-23 00:57:25 +01:00
* `organization` - (Required) The name of the organization containing the
2018-07-04 17:24:49 +02:00
targeted workspace(s).
2021-11-23 00:57:25 +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-11-23 00:57:25 +01:00
[`terraform login` ](/docs/cli/commands/login.html ) or manually configuring
2020-02-04 17:30:40 +01:00
`credentials` in the
2021-11-23 00:57:25 +01:00
[CLI config file ](/docs/cli/config/config-file.html#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-11-23 00:57:25 +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-11-23 00:57:25 +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-11-23 00:57:25 +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-11-23 00:57:25 +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-11-23 00:57:25 +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-11-23 00:57:25 +01:00
When executing a remote `plan` or `apply` in a [CLI-driven run ](/docs/cloud/run/cli.html ),
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-11-25 16:35:03 +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-11-23 00:57:25 +01:00
* Comments (starting with `#` ) or blank lines are ignored
2021-11-25 16:35:03 +01:00
* End a pattern with a forward slash `/` to specify a directory
2021-11-23 00:57:25 +01:00
* 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.