Introduction
This article outlines how to add a remote backend configuration to an existing HCP Terraform or Terraform Enterprise workspace configuration. This allows you to use the Terraform CLI on your local machine to run plans and interact with the workspace's state.
Expected Outcome
After following this guide, you will be able to use Terraform CLI commands like terraform plan and terraform state locally to interact with a remote workspace in HCP Terraform or Terraform Enterprise.
Prerequisites
- An existing workspace in HCP Terraform or Terraform Enterprise.
- A local copy of the Terraform configuration for the workspace.
- For Terraform versions 1.1.0 and later, HashiCorp recommends using the
cloudsettings block instead of theremotebackend. Follow the CLI-driven runs configuration guide for that method. This guide specifically covers theremotebackend.
Procedure
- Navigate to the HCP Terraform or Terraform Enterprise workspace you want to manage locally.
- Obtain a local copy of the Terraform configuration used to perform runs in this workspace.
- Within the local configuration directory, create a new file, such as
backend.tf. -
In the new file, add a
terraformblock containing aremotebackend configuration.terraform { backend "remote" { hostname = "TFE_HOSTNAME" organization = "ORG_NAME" workspaces { name = "WORKSPACE_NAME" } } } - Update the placeholder values:
-
TFE_HOSTNAME: For Terraform Enterprise, use the base URL for your instance. For HCP Terraform, you can delete this line, as it defaults toapp.terraform.io. -
ORG_NAME: Use the name of your HCP Terraform or Terraform Enterprise organization. -
WORKSPACE_NAME: Use the name of the workspace.
-
-
Ensure the version of Terraform you use at the command line matches the version specified in the workspace settings (
Settings > General).You can also find the version in the
terraform_versionentry of the most recent state file.Verify your local version matches.
$ terraform version Terraform v1.0.0 on darwin_amd64
-
Log in to your HCP Terraform or Terraform Enterprise instance from the CLI. For Terraform v0.12.21 or later, run
terraform login. If connecting to Terraform Enterprise, specify its hostname.$ terraform login <hostname>
-
When prompted, enter
yesand paste your API token once. Note there is no indication once pasted. Press return/enter to complete saving local credentials.Note: If you receive a
Token is invaliderror, ensure you copied the entire token correctly. Some browsers may not be compatible with the built-in copy button in the UI. -
In your Terraform configuration directory, initialize the backend.
$ terraform init
The output confirms successful initialization with the remote backend.
- You can now use the
terraform statecommand to interact with the remote state. For a list of available subcommands, refer to the Command: state documentation. -
You can also make modifications to your local configuration and run
terraform planto queue a run in the remote workspace.Note: Workspaces connected to a Version Control System (VCS) do not allow
terraform applyoperations initiated from the CLI using theremotebackend.