github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/website/docs/language/settings/backends/remote.mdx (about)

     1  ---
     2  page_title: 'Backend Type: remote'
     3  description: >-
     4    Terraform can store the state and run operations remotely, making it easier to
     5    version and work with in a team.
     6  ---
     7  
     8  # remote
     9  
    10  -> **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.
    11  
    12  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.
    13  
    14  When using full remote operations, operations like `terraform plan` or `terraform apply` can be executed in Terraform
    15  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.
    16  
    17  You can also use Terraform Cloud with local operations, in which case only state is stored in the Terraform Cloud backend.
    18  
    19  ## Command Support
    20  
    21  Currently the remote backend supports the following Terraform commands:
    22  
    23  - `apply`
    24  - `console` (supported in Terraform >= v0.11.12)
    25  - `destroy`
    26  - `fmt`
    27  - `get`
    28  - `graph` (supported in Terraform >= v0.11.12)
    29  - `import` (supported in Terraform >= v0.11.12)
    30  - `init`
    31  - `output`
    32  - `plan`
    33  - `providers`
    34  - `show`
    35  - `state` (supports all sub-commands: list, mv, pull, push, rm, show)
    36  - `taint`
    37  - `untaint`
    38  - `validate`
    39  - `version`
    40  - `workspace`
    41  
    42  ## Workspaces
    43  
    44  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
    45  determines which mode it uses:
    46  
    47  - To use a single remote Terraform Cloud workspace, set `workspaces.name` to the
    48    remote workspace's full name (like `networking-prod`).
    49  
    50  - To use multiple remote workspaces, set `workspaces.prefix` to a prefix used in
    51    all of the desired remote workspace names. For example, set
    52    `prefix = "networking-"` to use Terraform cloud workspaces with
    53    names like `networking-dev` and `networking-prod`. This is helpful when
    54    mapping multiple Terraform CLI [workspaces](/language/state/workspaces)
    55    used in a single Terraform configuration to multiple Terraform Cloud
    56    workspaces.
    57  
    58  
    59  The backend configuration requires either `name` or `prefix`. Omitting both or
    60  setting both results in a configuration error.
    61  
    62  If previous state is present when you run `terraform init` and the corresponding
    63  remote workspaces are empty or absent, Terraform will create workspaces and
    64  update the remote state accordingly. However, if your workspace requires variables or a specific version of Terraform for remote operations, we
    65  recommend that you create your remote workspaces on Terraform Cloud before
    66  running any remote operations against them.
    67  
    68  ### Workspace Names
    69  
    70  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`.
    71  
    72  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:
    73  
    74  - For local operations, `terraform.workspace` = `prod`
    75  - For remote operations, `terraform.workspace`=  `networking-prod`
    76  
    77  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
    78  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.
    79  
    80  ### Determining Run Environment
    81  
    82  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`.
    83  
    84  ```
    85  output "current_workspace_name" {
    86    value = terraform.workspace
    87  }
    88  
    89  variable "TFC_RUN_ID" {
    90    type    = string
    91    default = ""
    92  }
    93  
    94  output "remote_execution_determine" {
    95    value = "Remote run environment? %{if var.TFC_RUN_ID != ""}Yes%{else}No this is local%{endif}!"
    96  }
    97  ```
    98  
    99  
   100  ## Example Configurations
   101  
   102  ->  **Note:** We recommend omitting the token from the configuration, and instead using
   103  [`terraform login`](/cli/commands/login) or manually configuring
   104  `credentials` in the [CLI config file](/cli/config/config-file#credentials).
   105  
   106  ### Basic Configuration
   107  
   108  ```hcl
   109  # Using a single workspace:
   110  terraform {
   111    backend "remote" {
   112      hostname = "app.terraform.io"
   113      organization = "company"
   114  
   115      workspaces {
   116        name = "my-app-prod"
   117      }
   118    }
   119  }
   120  
   121  # Using multiple workspaces:
   122  terraform {
   123    backend "remote" {
   124      hostname = "app.terraform.io"
   125      organization = "company"
   126  
   127      workspaces {
   128        prefix = "my-app-"
   129      }
   130    }
   131  }
   132  ```
   133  
   134  ### Using CLI Input
   135  
   136  ```hcl
   137  # main.tf
   138  terraform {
   139    required_version = "~> 0.12.0"
   140  
   141    backend "remote" {}
   142  }
   143  ```
   144  
   145  Backend configuration file:
   146  
   147  ```hcl
   148  # config.remote.tfbackend
   149  workspaces { name = "workspace" }
   150  hostname     = "app.terraform.io"
   151  organization = "company"
   152  ```
   153  
   154  Running `terraform init` with the backend file:
   155  
   156  ```sh
   157  terraform init -backend-config=config.remote.tfbackend
   158  ```
   159  
   160  ### Data Source Configuration
   161  
   162  ```hcl
   163  data "terraform_remote_state" "foo" {
   164    backend = "remote"
   165  
   166    config = {
   167      organization = "company"
   168  
   169      workspaces = {
   170        name = "workspace"
   171      }
   172    }
   173  }
   174  ```
   175  
   176  ## Configuration Variables
   177  
   178  !> **Warning:**  We recommend using environment variables to supply credentials and other sensitive data. If you use `-backend-config` or hardcode these values directly in your configuration, Terraform will include these values in both the `.terraform` subdirectory and in plan files. Refer to [Credentials and Sensitive Data](/language/settings/backends/configuration#credentials-and-sensitive-data) for details.
   179  
   180  The following configuration options are supported:
   181  
   182  - `hostname` - (Optional) The remote backend hostname to connect to. Defaults
   183    to app.terraform.io.
   184  - `organization` - (Required) The name of the organization containing the
   185    targeted workspace(s).
   186  - `token` - (Optional) The token used to authenticate with the remote backend.
   187    We recommend omitting the token from the configuration, and instead using
   188    [`terraform login`](/cli/commands/login) or manually configuring
   189    `credentials` in the
   190    [CLI config file](/cli/config/config-file#credentials).
   191  - `workspaces` - (Required) A block specifying which remote workspace(s) to use.
   192    The `workspaces` block supports the following keys:
   193  
   194    - `name` - (Optional) The full name of one remote workspace. When configured,
   195      only the default workspace can be used. This option conflicts with `prefix`.
   196    - `prefix` - (Optional) A prefix used in the names of one or more remote
   197      workspaces, all of which can be used with this configuration. The full
   198      workspace names are used in Terraform Cloud, and the short names
   199      (minus the prefix) are used on the command line for Terraform CLI workspaces.
   200      If omitted, only the default workspace can be used. This option conflicts with `name`.
   201  
   202  ->  **Note:** You must use the `name` key when configuring a `terraform_remote_state`
   203  data source that retrieves state from another Terraform Cloud workspace. The `prefix` key is only
   204  intended for use when configuring an instance of the remote backend.
   205  
   206  ## Command Line Arguments
   207  
   208  For configurations that include a `backend "remote"` block, commands that
   209  make local modifications to Terraform state and then push them back up to
   210  the remote workspace accept the following option to modify that behavior:
   211  
   212  - `-ignore-remote-version` - Override checking that the local and remote
   213    Terraform versions agree, making an operation proceed even when there is
   214    a mismatch.
   215  
   216    Normally state-modification operations require using a local version of
   217    Terraform CLI which is compatible with the Terraform version selected
   218    for the remote workspace as part of its settings. This is to avoid the
   219    local operation creating a new state snapshot which the workspace's
   220    remote execution environment would then be unable to decode.
   221  
   222    Overriding this check can result in a Terraform Cloud workspace that is
   223    no longer able to complete remote operations, so we recommend against
   224    using this option.
   225  
   226  ## Excluding Files from Upload with .terraformignore
   227  
   228  -> **Version note:** `.terraformignore` support was added in Terraform 0.12.11.
   229  
   230  When executing a remote `plan` or `apply` in a [CLI-driven run](/cloud-docs/run/cli),
   231  an archive of your configuration directory is uploaded to Terraform Cloud. You can define
   232  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:
   233  
   234  - `.git/` directories
   235  - `.terraform/` directories (exclusive of `.terraform/modules`)
   236  
   237  The `.terraformignore` file can include rules as one would include in a
   238  [`.gitignore` file](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring)
   239  
   240  - Comments (starting with `#`) or blank lines are ignored
   241  - End a pattern with a forward slash `/` to specify a directory
   242  - Negate a pattern by starting it with an exclamation point `!`
   243  
   244  Note that unlike `.gitignore`, only the `.terraformignore` at the root of the configuration
   245  directory is considered.