> ## Documentation Index
> Fetch the complete documentation index at: https://www.latitude.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Terraform Cloud best practices

> Collaborate on infrastructure as code with team workflows and security guardrails

When working with multiple people, managing your infrastructure with Terraform becomes challenging. When that happens, adding visibility and guardrails to your Terraform plans becomes important.

[Terraform Cloud](https://app.terraform.io) is a great option for collaborating with other developers. Some of its most useful features are the ability to run plans remotely, shared state files, and add security features to avoid human error messing up your Latitude.sh infrastructure.

This is an opinionated setup for Latitude.sh on Terraform Cloud based on best practices we created when using Terraform with Latitude.sh ourselves.

## Set up

After signing up for Terraform Cloud, set up your workspace. We recommend using a Git repository. We use *GitHub*, but Terraform Cloud also allows *GitLab*, *Bitbucket* and *Azure DevOps* to be connected.

Create one directory on your git repo and one workspace in Terraform Cloud for each of your Latitude.sh projects. So, if like us, you have a *Development* and a \_Production \_ project on your Latitude.sh account that you want to manage with Terraform, this is what the root of your repo should look like.

<img src="https://mintcdn.com/latitudesh/_9x7641lGNJX3WXB/images/guides/terraform-cloud/537b364-github-repository.png?fit=max&auto=format&n=_9x7641lGNJX3WXB&q=85&s=89b5cd87615cc6cf69e06bceabab35bc" alt="Latitude.sh projects are represented by one directory each." width="898" height="153" data-path="images/guides/terraform-cloud/537b364-github-repository.png" />

Now go back to the workspace setup in Terraform Cloud. In the Configure settings section, click on **Advanced options**. In the **Terraform Working Directory** input, type the directory name you set up previously.

<img src="https://mintcdn.com/latitudesh/_9x7641lGNJX3WXB/images/guides/terraform-cloud/de488e1-advanced-options.png?fit=max&auto=format&n=_9x7641lGNJX3WXB&q=85&s=c8e0307a6570bc28b1a33df547bef279" alt="Terraform Cloud workspace setup advanced options" width="821" height="676" data-path="images/guides/terraform-cloud/de488e1-advanced-options.png" />

In the **Apply method** select, choose **Auto Apply** for environments you don't care about breaking, such as *development*, and **Manual apply** for critical environments, like *production*.

We strongly recommend leaving **Automatic speculative plans** selected.

This is a good setup to allow your team to move quickly when developing while keeping critical environments less error-prone.

Here's what your workspace should look like now

<img src="https://mintcdn.com/latitudesh/_9x7641lGNJX3WXB/images/guides/terraform-cloud/55ff6b5-terraform-cloud-workspaces.png?fit=max&auto=format&n=_9x7641lGNJX3WXB&q=85&s=b1c9d48874def0b3f687c2f53df627ce" alt="Terraform Cloud workspaces" width="1161" height="400" data-path="images/guides/terraform-cloud/55ff6b5-terraform-cloud-workspaces.png" />

## Variables

Select a workspace and go to **Variables**. Add a new sensitive variable named `LATITUDESH_AUTH_TOKEN` with your Latitude.sh [API Key](https://www.latitude.sh/docs/api-reference/get-api-keys) as value.

<img src="https://mintcdn.com/latitudesh/_9x7641lGNJX3WXB/images/guides/terraform-cloud/dd7195f-workspace-variables.png?fit=max&auto=format&n=_9x7641lGNJX3WXB&q=85&s=55226d90b46582902bd9fb8222a858c7" alt="Terraform Cloud workspace variables" width="911" height="247" data-path="images/guides/terraform-cloud/dd7195f-workspace-variables.png" />

Do this for all workspaces and create one API key for each.

## Additional recommendations

With this setup, whenever you push changes to the `main` branch

* Terraform Cloud will plan and apply your plan for the *development* workspace.
* Terraform Cloud will plan and wait for confirmation to apply your plan for the *production* workspace. You have to go to Terraform Cloud's dashboard and approve the changes.

Because Terraform has the potential for highly destructive actions like reimaging an instance or completely destroying it, it's important to harden your git repo. Here's what we recommend:

1. Restrict repository access to developers working on your infrastructure.
2. Create **branch protection rules**, the most important being protecting your `main` branch.
