How to Use Terraform Cloud: Setup and Workflow Guide
Terraform Cloud, first create an account and workspace on the Terraform Cloud website. Then configure your local terraform CLI to connect to this workspace by setting the backend to remote. This lets you run Terraform commands that store state and run plans remotely, enabling collaboration and automation.Syntax
Terraform Cloud uses a remote backend configuration in your Terraform files to connect your local setup to the cloud workspace. The key parts are:
hostname: The Terraform Cloud server URL.organization: Your Terraform Cloud organization name.workspaces: The workspace name where your state is stored.
This configuration tells Terraform to store state and run plans in Terraform Cloud instead of locally.
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "your-org-name"
workspaces {
name = "your-workspace-name"
}
}
}Example
This example shows how to configure Terraform to use Terraform Cloud for remote state and run management. Replace your-org-name and your-workspace-name with your actual Terraform Cloud details.
After this setup, running terraform init connects your local Terraform to Terraform Cloud. Running terraform apply will execute the plan remotely and store the state in Terraform Cloud.
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "your-org-name"
workspaces {
name = "your-workspace-name"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "example" {
bucket = "my-terraform-cloud-example"
acl = "private"
}Common Pitfalls
Common mistakes when using Terraform Cloud include:
- Not setting the backend before running
terraform init, causing Terraform to use local state. - Using incorrect organization or workspace names, which leads to connection errors.
- Forgetting to authenticate with Terraform Cloud CLI (
terraform login), so Terraform cannot access the workspace. - Trying to run
terraform applylocally without remote execution enabled, which defeats the purpose of Terraform Cloud.
Always run terraform login first to authenticate, then configure the backend, and finally run terraform init to connect.
### Wrong: No backend configured terraform init ### Right: Backend configured and logged in terraform login terraform init
Quick Reference
Key steps to use Terraform Cloud:
- Create a Terraform Cloud account and organization.
- Create a workspace for your project.
- Configure your Terraform files with the
remotebackend. - Run
terraform loginto authenticate your CLI. - Run
terraform initto initialize the backend. - Use
terraform planandterraform applyto manage infrastructure remotely.