Skip to main content
Automate your Terraform Cloud workflows—workspaces, variables, team access, and more—by codifying everything you’d normally click through in the Terraform Cloud UI. In this lab, we’ll use the official hashicorp/tfe provider to:
  • Initialize the TFC provider
  • Query and inspect existing workspaces
  • Create new workspaces
  • Manage workspace variables
  • Specify Terraform versions
  • Dynamically generate multiple workspaces
  • Assign team permissions
  • Clean up resources

Prerequisites

  1. Terraform CLI v1.x installed
  2. A Terraform Cloud account
  3. A Terraform API token with appropriate permissions
Keep your TFE_TOKEN secure. Do not commit API tokens to version control.
Set your Terraform Cloud API token in the shell:

1. Initialize the Terraform Cloud Provider

Create a directory named workspace_automation and inside it, add a main.tf file:
Initialize the provider:

2. Query an Existing Workspace

Add a data source block to main.tf to fetch a workspace by name:
Apply the configuration:
Sample output:
Inspect the state:

3. Create a New Workspace

Extend main.tf with a resource block to provision a workspace:
Format and apply:
Preview plan:
After successful apply, verify the new workspace in Terraform Cloud.
The image shows a Terraform Cloud interface with a focus on the "Workspaces" section, displaying options to filter and sort workspaces, but no workspaces are currently listed.

4. Manage Workspace Variables

Use tfe_variable resources to define standard, sensitive, and HCL variables:
Apply the changes:
Confirm in the Terraform Cloud UI under Variables that all variables appear correctly.
The image shows a Terraform Cloud interface displaying the "Variables" section for a workspace named "webserver-aws-stage." It includes details about sensitive variables and workspace variables.

5. Specify Terraform Version per Workspace

Control the Terraform version used by the workspace:
Reapply:
Verify the Terraform version update in the workspace settings.

6. Dynamically Create Multiple Workspaces

Define apps and environments, then loop to create all combinations:
Apply all at once:
You’ll see six workspaces created (appA/appB × sandbox/development/production).

7. Assign Team Access to Workspaces

Ensure your TFC plan supports Teams & Governance. Create a teams.tf:
Apply:
Verify in the Terraform Cloud UI under Organization → Teams → classmates and in each workspace’s Team Access page.
The image shows a user interface for managing organization access settings, with options to manage policies, workspaces, and other administrative tasks. A success message indicates that a team named "classmates" has been created.
Browse to any workspace’s Team Access settings to confirm the “classmates” team has read privileges.
The image shows a "Team Access" page from a web application, listing different teams and their access privileges, such as "default," "read," and "plan." The sidebar includes various workspace settings options.

8. Cleanup

When you’re done, destroy all created resources:

Resource Overview


Watch Video

Practice Lab