Introduction
Expected Outcome
This article outlines how to add a remote backend configuration to an existing Terraform Enterprise/Cloud workspace configuration.
Prerequisites
- Terraform Cloud/Terraform Enterprise
- Terraform < 1.1.0
For Terraform versions 1.1.0 and later, use the cloud
settings block instead of the remote backend. Follow the guide here.
Procedure
-
- Navigate to the Terraform Enterprise/Cloud workspace you want to investigate.
- Obtain a local copy of the Terraform configuration used to perform runs in this workspace.
- Within the local configuration directory, create a new
*.tf
file, e.g.backend.tf
.
- In the file, create a terraform block containing a remote backend block:
terraform {
backend "remote" {
hostname = "TFE_HOSTNAME"
organization = "ORG_NAME"
workspaces {
name = "WORKSPACE_NAME"
}
}
}
- For
TFE_HOSTNAME
, use the base url for your Terraform Enterprise instance. For Terraform Cloud, you can delete this hostname line as it will default to app.terraform.io.
- For
ORG_NAME
, use the Terraform Enterprise/Cloud organization name, and forWORKSPACE_NAME
, use the name of the Terraform Enterprise/Cloud workspace.
-
Ensure the version of Terraform used at the command line matches the version used in Terraform Enterprise. This is likely either the version of Terraform shown in the workspace settings under Settings > General, or the
terraform_version
entry of the most recent state in the state list., e.g.
or
matches:
$ terraform version
Terraform v1.0.0
on darwin_amd64
- On Terraform versions v0.12.21 or later, run
terraform login [hostname]
to generate credentials to your Terraform Enterprise/Cloud instance. If you do not include hostname, it will default to Terraform Cloud.
- Enter 'yes'. Paste the token once. Note there is no indication once pasted. Press return/enter to complete saving local credentials.
- If you receive an error indicating "Token is invalid", make sure you successfully copied the token from your browser. Note some browsers are not compatible with the inbuilt "copy" button on the token modal in Terraform Enterprise/Cloud.
- In the Terraform configuration directory, run
terraform init
.
- You should see the following output:
- The Terraform
state
command can now be used to interact with the remote state; for a list of the possible options, see Command: state.
- Additionally, you may now make modifications to the local configuration and run
terraform plan
to initiate a run in Terraform Enterprise/Cloud. Note that workspaces connected to a Version Control will not allow aterraform apply
operation initiated using theremote
backend.
- Navigate to the Terraform Enterprise/Cloud workspace you want to investigate.