This blog post is to explain how I did to automatically change Terraform Cloud workspace from Github Actions. As explained in the documentation remote workspace, you can use different remote workspace by specifying the prefix of you workspace in the Terraform backend configuration.
backend "remote" {
organization = "netmemo"
workspaces {
prefix = "netmemo-"
}
}
After that, you only need to select the proper workspace by entering the terraform workspace select [suffix] command. The issue comes if you want to do it in a fully automated environment like with Github Actions. You need an extra step which is to set the TF_WORKSPACE variable.
Below are the 2 steps that are needed to select a specific environment. In this blog the workspace will be netmemo-dev where netmemo- is the prefix configured in the main.tf and dev the suffix configured in the .yml file of the Github Actions script.
This step initializes Terraform and set the TF_WORKSPACE variable to indicate that we want to use the dev environment suffix before the initialization.
- name: Terraform Init
id: init
run: terraform init
env:
TF_WORKSPACE: "dev"
If we are not setting the TF_WORKSPACE, the init command will try to get the default workspace that doesn’t exist and you will have the following error.
The currently selected workspace (default) does not exist.
This is expected behavior when the selected workspace did not have an
existing non-empty state. Please enter a number to select a workspace:
1. dev
2. prod
Even after setting the TF_WORKSPACE variable, we still need to enter the terraform workspace select command to provide the suffix of the workspace.
- name: Terraform Workspace
id: workspace
run: terraform workspace select dev
If we forgot to enter the command to select workspace, the Terraform configuration section will try to load a workspace with only the prefix and trigger an error.
Error: error starting operation: The configured "remote" backend encountered an unexpected error:
invalid value for workspace
You can find a full example of where we need to change workspace automatically in this blog post Github Actions with Terraform Cloud for CI/CD of NSX-T