The CLI-driven Run Workflow
Hands-on: Try the Log in to Terraform Cloud from the CLI tutorial.
Terraform Cloud has three workflows for managing Terraform runs.
- The UI/VCS-driven run workflow, which is the primary mode of operation.
- The API-driven run workflow, which is more flexible but requires you to create some tooling.
- The CLI-driven run workflow described below, which uses Terraform's standard CLI tools to execute runs in Terraform Cloud.
Summary
The CLI integration brings Terraform Cloud's collaboration features into the familiar Terraform CLI workflow. It offers the best of both worlds to developers who are already comfortable with using the Terraform CLI, and it can work with existing CI/CD pipelines.
You can start runs with the standard terraform plan
and terraform apply
commands and then watch the progress of the run from your terminal. These runs execute remotely in Terraform Cloud, use variables from the appropriate workspace, enforce any applicable Sentinel or OPA policies, and can access Terraform Cloud's private registry and remote state inputs.
Terraform Cloud offers three kinds of CLI-driven runs, to support different stages of your workflow:
terraform plan
starts a speculative plan in a Terraform Cloud workspace, using configuration files from a local directory. You can quickly check the results of edits (including compliance with Sentinel policies) without needing to copy sensitive variables to your local machine.Speculative plans work with all workspaces, and can co-exist with the VCS-driven workflow.
terraform apply
starts a standard plan and apply in a Terraform Cloud workspace, using configuration files from a local directory.Remote
terraform apply
is for workspaces without a linked VCS repository. It replaces the VCS-driven workflow with a more traditional CLI workflow.terraform plan -out <FILE>
andterraform apply <FILE>
perform a two-part saved plan run in a Terraform Cloud workspace, using configuration files from a local directory. The first command performs and saves the plan, and the second command applies it. You can useterraform show <FILE>
to inspect a saved plan.Like remote
terraform apply
, remote saved plans are for workspaces without a linked VCS repository.Saved plans require at least Terraform CLI v1.6.0.
To supplement these remote operations, you can also use the optional Terraform Enterprise Provider, which interacts with the Terraform Cloud-supported resources. This provider is useful for editing variables and workspace settings through the Terraform CLI.
Configuration
To enable the CLI-driven workflow, you must:
Run
terraform login
to authenticate with Terraform Cloud. Alternatively, you can manually configure credentials in the CLI config file or through environment variables. Refer to CLI Configuration for details.Add the
cloud
block to your Terraform configuration. You can define its arguments directly in your configuration file or supply them through environment variables, which can be useful for non-interactive workflows. Refer to Using Terraform Cloud for configuration details.The following example shows how to map CLI workspaces to Terraform Cloud workspaces with a specific tag.
Note: The
cloud
block is available in Terraform v1.1 and later. Previous versions can use theremote
backend to configure the CLI workflow and migrate state.Run
terraform init
.
Implicit Workspace Creation
If you configure the cloud
block to use a workspace that doesn't yet exist in your organization, Terraform Cloud will create a new workspace with that name when you run terraform init
. The output of terraform init
will inform you when this happens.
Automatically created workspaces might not be immediately ready to use, so use Terraform Cloud's UI to check a workspace's settings and data before performing any runs. In particular, note that:
- No Terraform variables or environment variables are created by default, unless your organization has configured one or more global variable sets. Terraform Cloud will use
*.auto.tfvars
files if they are present, but you will usually still need to set some workspace-specific variables. - The execution mode defaults to "Remote," so that runs occur within Terraform Cloud's infrastructure instead of on your workstation.
- New workspaces are not automatically connected to a VCS repository and do not have a working directory specified.
- A new workspace's Terraform version defaults to the most recent release of Terraform at the time the workspace was created.
Variables in CLI-Driven Runs
Remote runs in Terraform Cloud use:
- Run-specific variables set through the command line or in your local environment. Terraform can use shell environment variables prefixed with
TF_VAR_
as input variables for the run, but you must still set all required environment variables, like provider credentials, inside the workspace. - Workspace-specific Terraform and environment variables set in the workspace.
- Any variable sets applied globally, on the project containing the workspace, or on the workspace itself.
- Terraform variables from any
*.auto.tfvars
files included in the configuration.
Refer to Variables for more details about variable types, variable scopes, variable precedence, and how to set run-specific variables through the command line.
Remote Working Directories
If you manage your Terraform configurations in self-contained repositories, the remote working directory always has the same content as the local working directory.
If you use a combined repository and specify a working directory on workspaces, you can run Terraform from either the real working directory or from the root of the combined configuration directory. In both cases, Terraform will upload the entire combined configuration directory.
Excluding Files from Upload
Version note: .terraformignore
support was added in Terraform 0.12.11.
CLI-driven runs upload an archive of your configuration directory
to Terraform Cloud. If the directory contains files you want to exclude from upload,
you can do so by defining a .terraformignore
file in your configuration directory.
Remote Speculative Plans
You can run speculative plans in any workspace where you have permission to queue plans. Speculative plans use the configuration code from the local working directory, but will use variable values from the specified workspace.
To run a speculative plan on your configuration, use the terraform plan
command. The plan will run in Terraform Cloud, and the logs will stream back to the command line along with a URL to view the plan in the Terraform Cloud UI.
Remote Applies
In workspaces that are not connected to a VCS repository, users with permission to apply runs can use the CLI to trigger remote applies. Remote applies use the configuration code from the local working directory, but use the variable values from the specified workspace.
Note: You cannot run remote applies in workspaces that are linked to a VCS repository, since the repository serves as the workspace’s source of truth. To apply changes in a VCS-linked workspace, merge your changes to the designated branch.
When you are ready to apply configuration changes, use the terraform apply
command. Terraform Cloud will plan your changes, and the command line will prompt you for approval before applying them.
Non-Interactive Workflows
Hands On: Try the Deploy Infrastructure with Terraform Cloud and CircleCI tutorial.
External systems cannot run the traditional apply workflow because Terraform requires console input from the user to approve plans. We recommend using the API-driven Run Workflow for non-interactive workflows when possible.
If you prefer to use the CLI in a non-interactive environment, we recommend first running a speculative plan to preview the changes Terraform will make to your infrastructure. Then, use one of the following approaches with the -auto-approve
flag based on the execution mode of your workspace. The -auto-approve
flag skips prompting you to approve the plan.
Local Execution: Save the approved speculative plan and then run
terraform apply -auto-approve
with the saved plan.Remote Execution: Terraform Cloud does not support uploading saved plans for remote execution, so we recommend running
terraform apply -auto-approve
immediately after approving the speculative plan to prevent the plan from becoming stale.Warning: Remote execution with non-interactive workflows requires auto-approved deployments. Minimize the risk of unpredictable infrastructure changes and configuration drift by making sure that no one can change your infrastructure outside of your automated build pipeline.
Remote Saved Plans
Version note: Saved plans require at least Terraform CLI v1.6.0.
In workspaces that support terraform apply
, you also have the option of performing the plan and apply as separate steps, using the standard variations of the relevant Terraform commands:
terraform plan -out <FILE>
performs and saves a plan.terraform apply <FILE>
applies a previously saved plan.terraform show <FILE>
(andterraform show -json <FILE>
) inspect a plan you previously saved.
Saved plan runs are halfway between speculative plans and standard plan and apply runs. They allow you to:
- Perform cheap exploratory plans while retaining the option of applying a specific plan you are satisfied with.
- Perform other tasks in your terminal between the plan and apply stages.
- Perform the plan and apply operations on separate machines (as is common in continuous integration workflows).
Saved plans become stale once the state Terraform planned them against is no longer valid (usually due to someone applying a different run). In Terraform Cloud, stale saved plan runs are automatically detected and discarded. When examining a remote saved plan, the terraform show
command (without the -json
option) informs you if a plan has been discarded or is otherwise unable to be applied.
File Contents and Permissions
You can only apply remote saved plans in the same remote Terraform Cloud workspace that performed the plan. Additionally, you can not apply locally executed saved plans in a remote workspace.
In order to abide by Terraform Cloud's permissions model, remote saved plans do not use the same local file format as locally executed saved plans. Instead, remote saved plans are a thin reference to a remote run, and the Terraform CLI relies on authenticated network requests to inspect and apply remote plans. This helps avoid the accidental exposure of credentials or other sensitive information.
The terraform show -json
command requires workspace admin permissions to inspect a remote saved plan; this is because the machine-readable JSON plan format contains unredacted sensitive information (alongside redaction hints for use by systems that consume the format). The human-readable version of terraform show
only requires the read runs permission, because it uses pre-redacted information.
Policy Enforcement
Note: Terraform Cloud Free Edition includes one policy set of up to five policies. In Terraform Cloud Plus Edition, you can connect a policy set to a version control repository or create policy set versions via the API. Refer to Terraform Cloud pricing for details.
Policies are rules that Terraform Cloud enforces on Terraform runs. You can use two policy-as-code frameworks to define fine-grained, logic-based policies: Sentinel and Open Policy Agent (OPA).
If the specified workspace uses policies, Terraform Cloud runs those policies against all speculative plans and remote applies in that workspace. Failed policies can pause or prevent an apply, depending on the enforcement level. Refer to Policy Enforcement for details.
For Sentinel, the Terraform CLI prints policy results for CLI-driven runs. CLI support for policy results is not available for OPA.
The following example shows Sentinel policy output in the terminal.
Options for Plans and Applies
Run Modes and Options contains more details about the various options available for plans and applies when you use the CLI-driven workflow.
Networking/Connection Issues
Sometimes during a CLI-driven run, errors relating to network connectivity issues arise. Examples of these kinds of errors include:
Sometimes there are network problems beyond our control. If you have network errors, verify your network connection is operational. Then, check the following common configuration settings:
- Determine if any firewall software on your system blocks the
terraform
command and explicitly approve it. - Verify that you have a valid DNS server IP address.
- Remove any expired TLS certificates for your system.