Introduction
When working with HCP Terraform or Terraform Enterprise, you may need to set a workspace's execution type to Local. While you can configure this in the UI, you may also need to set the Local execution mode by default programmatically or through a CLI-based workflow.
This article outlines the methods to automate this configuration.
Use Case
You want to ensure that certain workspaces always perform Terraform runs on a local machine rather than within the HCP Terraform or Terraform Enterprise environment, and you want to configure this setting automatically as part of your infrastructure as code or scripting workflows.
Procedure
There are two primary methods to programmatically set the execution mode to local for a workspace.
Method 1: Using the TFE Provider
You can use the tfe provider to manage workspace settings, including the execution mode.
-
Set the Execution Mode: In your Terraform configuration for the
tfe_workspaceresource, set theexecution_modeargument tolocal. -
Link Local and Remote Workspaces: Because Terraform CLI workspaces are a local execution concept, you must link your local working directory to the desired remote workspace in HCP Terraform or Terraform Enterprise. You can achieve this using workspace tags.
- Add tags to your remote workspaces using the
tag_namesargument in thetfe_workspaceresource or by using the API. You can learn more about the benefits of workspace tags in the announcement blog post. - From your local command line, use the
terraform workspace selectcommand with a tag to link your local directory to the correct remote workspace. For more details on this interaction, refer to the documentation on Interactions with HCP Terraform Workspaces.
- Add tags to your remote workspaces using the
Method 2: Using the API
You can also use the HCP Terraform API to update a workspace's settings. To do this, make a PATCH request to the workspace endpoint and set the data.attributes.execution-mode attribute to local.
Additional Information
When using the tfe provider or the API, do not use the deprecated operations argument. Specifying the operations argument in the provider or the data.attributes.operations attribute in an API call may cause issues. Always use the execution-mode attribute instead.