Post

Getting Started with Terraform Cloud

Getting Started with Terraform Cloud

Terraform is very good at keeping the state of the Infrastructure up to date. By default state is stored in terraform.tfstate and stored on local machine. But big downside is keeping the state up to date between the all the members of the teams of Engineers & who has the latest state. It can lead to data loss and revenue loss. If the state can be stored in a central location and synced across the engineers with latest current state.

There are multiple ways to do that. such as

  • Terraform Cloud
  • Terraform backend

Terraform Cloud is free start and has flexible pricing. Signup here for free.

Terraform backend is similar to Terraform Cloud but state can saved on multiple cloud providers such as aws, gcp, azre and etc.

Opensource selfhosted alternative to Terraform Cloud are otf, digger, terrakube.

Terraform Cloud Login

To login to the terraform cloud, run terraform login, it will open a browser window and navigate the Tokens section in User Settings. Provide Name and Expiry of the token and copy the token and enter it in the terminal.

Create Workspace

Create a workspace in Terraform Cloud and you can select Default project or create separate project. Select CLI-driven workflow

Terraform Cloud config

To initialize the terraform cloud, Add a cloud block to the directory’s Terraform configuration, to specify which organization and workspace(s) to use.

1
2
3
4
5
6
7
8
9
terraform {
  cloud {
    organization= "my-org"

    workspaces {
      name= "demo-workspace"
    }
  }
}

You can also use environment variables to configure cloud block attributes such as organization and workspace with TF_CLOUD_ORGANIZATION and TF_WORKSPACE respectively. These will come in handy when running in CI/CD pipelines.

Terraform initialization

Initialize terraform with init argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$terraform init

Initializing Terraform Cloud...
.
.
.
.
Terraform Cloud has been successfully initialized!

You may now begin working with Terraform Cloud. Try running "terraform plan" to
see any changes that are required for your infrastructure.

If you ever set or change modules or Terraform Settings, run "terraform init"
again to reinitialize your working directory.

Run the config

On the first run, if the state is stored locally, terraform will prompt to migrate the state to the cloud. Upon on selecting yes, state will be moved to the cloud and can be access by anyone in the team by simply providing the access the project and workspace.

Conclusion

Terraform Cloud is easy to manage and will be on the lighter side on the changes too. Makes sense for smaller teams. But your organisation wants to selfhost the terraform cloud, Terraform Enterprise is a self hosted Terraform Cloud with SSO and other features. But needs hefty check and on role engineers to managing and administration.

As mentioned above, there are plenty of opensource alternatives. My personal favourite is otf. But does not come with any support, you manager might not like that. Any how, keep building Au Revoir.

This post is licensed under MIT by the author.