Terraform Cloud is a managed service by HashiCorp that provides:
- Remote state management with built-in locking
- Secure variable storage for sensitive data
- Collaborative workflows with team access controls
- Policy as code with Sentinel and OPA
- Cost estimation and infrastructure insights
- Private module registry for reusable infrastructure code
- Integration with VCS (GitHub, GitLab, Bitbucket)
Before setting up Terraform Cloud, ensure you have:
- A valid email address for sign-up
- Terraform installed locally (version 1.1.0 or later)
- A version control system account (GitHub, GitLab, or Bitbucket)
- Cloud provider credentials (AWS, Azure, GCP, etc.)
- Navigate to Terraform Cloud
- Choose your plan:
- Free tier: Up to 5 users, 500 runs/month, basic features
- Team tier: More runs, SSO, advanced features
- Enterprise tier: Unlimited runs, custom policies, dedicated support
- Complete the registration with your email and password
- Verify your email address
- After login, click on "Create an organization"
- Enter organization details:
- Organization name (e.g.,
my-company-infrastructure) - Email address
- Organization name (e.g.,
- Choose your email preference for notifications
- Click "Create organization"
- Navigate to your organization in Terraform Cloud
- Click "New workspace"
- Choose workspace type:
- CLI-driven: For manual runs from local CLI
- VCS-driven: For automatic runs on VCS changes
- Configure workspace settings:
- Workspace name (e.g.,
production-networking) - Description
- Execution mode (local, remote, or agent)
- Terraform version
- Workspace name (e.g.,
- Click "Create workspace"
- Select "Connect to a version control provider"
- Choose your VCS provider (GitHub, GitLab, or Bitbucket)
- Authorize Terraform Cloud to access your repositories
- Select the repository containing your Terraform code
- Configure VCS settings:
- Branch to track (e.g.,
main) - Terraform working directory
- Trigger conditions (push, PR, tags)
- Branch to track (e.g.,
- Navigate to your workspace → "Variables"
- Click "Add variable"
- Configure variable settings:
- Key: Variable name (e.g.,
AWS_REGION) - Value: Variable value
- Category: Environment variable or Terraform variable
- Sensitive: Mark as sensitive for secrets
- HCL: Enable for complex values
- Key: Variable name (e.g.,
Example variables:
AWS_ACCESS_KEY_ID (sensitive)
AWS_SECRET_ACCESS_KEY (sensitive)
AWS_REGION = us-west-2
ENVIRONMENT = production
- Create a
terraform.tfvarsfile locally for reference - Add variables to Terraform Cloud workspace:
- Key: Variable name (matches your Terraform variables)
- Value: Variable value
- Category: Terraform variable
Example:
instance_type = t3.large
vpc_cidr = 10.0.0.0/16
enable_monitoring = true
Follow the installation guide in install_terraform.md
Option 1: Interactive Login
terraform loginThis will open a browser window for authentication.
Option 2: API Token
- Navigate to User Settings → Tokens
- Generate a new token with appropriate permissions
- Set the token as environment variable:
export TF_API_TOKEN="your-api-token-here"Create or update backend.tf:
terraform {
cloud {
organization = "your-organization"
workspaces {
name = "your-workspace"
}
}
}terraform init# Plan changes
terraform plan
# Apply changes
terraform apply- Push changes to your connected branch
- Terraform Cloud automatically triggers a run
- Monitor the run in the Terraform Cloud dashboard
- Approve the run if required (based on workspace settings)
- Navigate to your workspace in Terraform Cloud
- Click "Queue plan"
- Review the plan output
- Click "Confirm & apply" if changes look correct
- Navigate to Organization Settings → "Sentinel"
- Ensure Sentinel is enabled (available on paid plans)
- Go to Organization → "Policy Sets"
- Click "Create a new policy set"
- Configure policy set:
- Name (e.g.,
security-policies) - Description
- Workspace scope (specific workspaces or all)
- Enforcement level (advisory, soft-mandatory, hard-mandatory)
- Name (e.g.,
- Create policy files with
.sentinelextension:
Example policy:
# restrict-s3-public-access.sentinel
import "tfplan/v2" as tfplan
# Restrict S3 bucket public access
s3_buckets_public = rule {
all tfplan.resource_changes as rc {
rc.type is "aws_s3_bucket" and
rc.change.after.acl is "private"
}
}
main = rule {
s3_buckets_public
}
- Upload policies to your policy set
- Assign policies to workspaces
- Navigate to workspace → "Notifications"
- Add notification integrations:
- Slack: Connect to your Slack workspace
- Email: Configure email recipients
- Microsoft Teams: Add webhook URL
- Generic webhook: Custom webhook endpoint
- View run history in workspace dashboard
- Check run logs for detailed execution information
- Review state changes in the state view
- Track cost estimates for infrastructure changes
- Navigate to Organization → "Teams"
- Create teams (e.g.,
developers,operators,auditors) - Add members to teams via email
- Assign permissions to teams
Workspace-level permissions:
- Read: View runs and state
- Plan: Trigger plans
- Write: Trigger applies
- Admin: Full workspace control
Organization-level permissions:
- Owner: Full organization control
- Admin: Manage teams and policies
- Member: Basic access to assigned workspaces
- Download the agent from Terraform Cloud
- Install on your infrastructure (on-premises or private cloud)
Docker installation:
docker pull hashicorp/tfc-agent:latest
docker run -d \
-e TFC_AGENT_TOKEN="your-agent-token" \
hashicorp/tfc-agent:latestBinary installation:
wget https://releases.hashicorp.com/tfc-agent/<version>/tfc-agent_<version>_linux_amd64.zip
unzip tfc-agent_<version>_linux_amd64.zip
./tfc-agent- Navigate to Organization → "Agent Pools"
- Create an agent pool
- Assign workspaces to the agent pool
- Agents will execute runs in your private network
- Navigate to Organization → "Registry"
- Click "Publish a module"
- Connect to VCS repository containing the module
- Configure module details:
- Module name
- Provider (AWS, Azure, GCP, etc.)
- Versioning strategy
Reference private modules in your Terraform code:
module "vpc" {
source = "app.terraform.io/your-organization/vpc/aws"
version = "1.0.0"
cidr = "10.0.0.0/16"
}cp terraform.tfstate terraform.tfstate.backupReplace local backend with cloud backend in backend.tf:
terraform {
cloud {
organization = "your-organization"
workspaces {
name = "your-workspace"
}
}
}terraform initTerraform will prompt to migrate state to Terraform Cloud.
- Check state in Terraform Cloud dashboard
- Run
terraform planto verify state integrity - Remove local state file after verification
- Always mark sensitive variables as sensitive
- Use least privilege IAM roles for cloud providers
- Enable MFA for Terraform Cloud accounts
- Rotate API tokens regularly
- Use Sentinel policies for compliance
- Use VCS-driven workspaces for team projects
- Require approval for production applies
- Set up notifications for run status
- Use workspace naming conventions (e.g.,
env-service) - Document variable requirements in README files
- Use remote execution for large infrastructure
- Implement state refresh optimization
- Use module caching for faster runs
- Enable cost estimation to prevent overspending
- 📖 Official Terraform Cloud Documentation
- 🎓 HashiCorp Learn - Terraform Cloud
- 📖 Sentinel Policy Language
- 📖 Terraform Cloud API
You are now ready to use Terraform Cloud for your infrastructure management! 🚀
Note
Join Our Telegram Community // Follow me for more DevOps & Cloud content.
