0
0
TerraformHow-ToBeginner · 4 min read

How to Use Terraform Cloud: Setup and Workflow Guide

To use 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.

hcl
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.

hcl
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"
}
Output
Initializing the backend... Successfully configured the backend "remote"! Terraform will perform remote operations using Terraform Cloud. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
⚠️

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 apply locally 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.

bash
### 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 remote backend.
  • Run terraform login to authenticate your CLI.
  • Run terraform init to initialize the backend.
  • Use terraform plan and terraform apply to manage infrastructure remotely.

Key Takeaways

Configure the Terraform backend to 'remote' with your Terraform Cloud organization and workspace.
Always authenticate your CLI with 'terraform login' before initializing Terraform Cloud backend.
Terraform Cloud stores state and runs plans remotely, enabling team collaboration and automation.
Run 'terraform init' after backend setup to connect your local Terraform to Terraform Cloud.
Avoid common errors by verifying workspace names and backend configuration before applying.