HashiCorp : Terraform Cloud
Securing Variables with Terraform Cloud
Lab Solution Terraform Cloud Variables
In this lab, we’ll explore how to manage AWS credentials in Terraform Cloud using both workspace-level variables and centralized organizational variable sets. You’ll learn how to securely store sensitive data, inherit variables across workspaces, and override defaults when needed.
Table of Contents
- Workspace-Level Variables
- Organizational Variable Sets
- Terraform CLI & Cloud Integration
- Variable Precedence & Overrides
- Conclusion
- References
Workspace-Level Variables
Workspace-level variables apply only to the workspace where they’re defined. This is ideal for settings that differ per environment.
- Sign in to Terraform Cloud and select your workspace (e.g.,
devopsawsmyapp-dev
) under the Mastering Terraform Cloud organization. - Navigate to Settings → Variables.
- Add environment variables for your AWS credentials:
AWS_ACCESS_KEY_ID
(mark as sensitive)AWS_SECRET_ACCESS_KEY
(mark as sensitive)
Warning
Always mark AWS credentials as sensitive to prevent them from appearing in logs or the UI.
Organizational Variable Sets
Organizational variable sets let you define a common set of variables once and apply them to multiple workspaces.
Scope | Inheritance | Override Behavior |
---|---|---|
Workspace-level | Only that workspace | Highest precedence |
Organizational variable | Selected or all workspaces | Can be overridden at workspace |
Creating an Org Variable Set
- Go to Organization Settings → Variable Sets.
- Click Create variable set, then enter:
- Name:
AWS-Credentials
- Description:
Shared AWS keys for all dev & prod workspaces
- Name:
- Under Workspaces, choose All workspaces or pick specific ones.
- Add the following environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
(mark as sensitive)
- Save the set.
All selected workspaces now inherit these AWS credentials automatically.
Terraform CLI & Cloud Integration
To run Terraform commands against Terraform Cloud, authenticate your CLI and initialize the workspace:
# Authenticate with Terraform Cloud
terraform login
# Initialize the workspace configuration
terraform init
After initialization, your local CLI sessions will execute Terraform runs in the Cloud backend.
Variable Precedence & Overrides
By default, workspace-level variables override those from organizational sets.
Note
Order of precedence (highest → lowest):
- CLI
-var
flags - Workspace-level variables
- Organizational variable sets
- Terraform defaults
Overriding at the Workspace UI
- Open Settings → Variables in your workspace.
- Add
prefix
as an environment variable:- Key:
prefix
- Value:
dev-app
- Key:
- Save changes.
Overriding via CLI at Runtime
terraform plan -var="prefix=dev-app"
Example output:
Plan: 23 to add, 0 to change, 0 to destroy.
Changes to Outputs:
~ clumsy-bird-ip = "http://54.235.109.203:8001" -> (known after apply)
~ clumsy-bird-url = "http://ec2-54-235-109-203.compute-1.amazonaws.com:8001" -> (known after apply)
Conclusion
You have now:
- Configured AWS credentials at the workspace level.
- Created and applied an organizational variable set.
- Connected your local CLI to Terraform Cloud.
- Explored variable precedence and override methods.
This setup balances security (by marking secrets sensitive) and flexibility (via overrides), ensuring consistent credential management across environments.
References
Watch Video
Watch video content