Introduction & Use-Case
When working with Terraform Enterprise/Cloud, there are times where you'd like to set your TFE/C Workspace's execution type to "Local". You can set it easily on the UI as explained here [a], but there are times when you'd like to set the Local execution mode by default either programmatically or through the CLI workflow -- that's what this article walks you through.
Procedure
At the time of this writing, there are currently 2 ways to select the local
execution mode by default:
- when using the TFE provider [b] OR
- through the data.attributes.execution-mode
API setting [c].
The API method is pretty self-explanatory so I'll focus on how to do this using the TFE provider.
Because local workspaces in Terraform (via the terraform workspace
command) is a local execution concept only and different from TFE/C workspaces, we have to link them up for a programmatic/CLI-based workflow.
Here are the steps to do so:
-
Add tags to your TFE workspaces using the TFE provider [d] or the API [e]. To learn more about workspace tags, please see [f].
-
Link your working directory to multiple TFE workspaces using workspace tags - via the
workspace select
command [g] to select which remote workspace to use. The explanation of the principle behind this can be found here [h].
P.S.
If you proceed to use the TFE provider or API to specify local execution modes, please do NOT use/specify the now deprecated operations
argument [i] or data.attributes.operations
[c] to avoid running into issues.
Link References
[b] https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace#execution_mode
[c] https://www.terraform.io/cloud-docs/api-docs/workspaces
[d] https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace#tag_names
[e] https://www.terraform.io/cloud-docs/api-docs/workspaces#add-tags-to-a-workspace
[f] https://www.hashicorp.com/blog/new-workspace-tags-for-terraform-cloud
[g] https://www.terraform.io/cli/commands/workspace/select
[h] https://www.terraform.io/cli/workspaces#interactions-with-terraform-cloud-workspaces
[i] https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace#operations