Terraform Cloud Series – Part 1

Reading Time: 4 minutes

What is a terraform cloud? Terraform cloud is a managed platform for teams/enterprises to create a TF stack via a managed platform. With Terraform cloud, tfstate file & stack state is stored in the Terrafrom Cloud platform. I won’t go into how the application works, but you can read up on it at the link below:

https://www.terraform.io/docs/cloud/index.html

TF Cloud is currently free for now, so go sign-up and start hands-on with it. Once you have created your account, you will see the following:

In order to create cloud resources, you need to create a workspace. Think a workspace as where you will add/modify/change cloud resources i.e. vpc, subnet, compute, etc, etc. For part 1, we will start small and work our way up to a bit more complex setup.

To create a working workspace, we will need to have the following tools:

  • TF Cloud workspace
  • TF with a remote backend
  • Backend repo control provider
  • Stack management & lifecycle management with Terraform Cloud
  • Terraform code for building stack

For this part 1 of the series, I will limit it to creating TF workspace and setup my terraform templates with remote backend. Also, If you need sample terraform templates you can get it from my git repo here.

Creating Workspace

Once signed into Terraform Cloud, click create “New workspace” you would be asked to set up the backend repository or use “no VCS connection”:

For the purpose of this lab, I will be using no VCS connection to setup my backend. Before you begin, stage local directory and download sample code from here.

TF Cloud backend

In order to use TF cloud, you need to create a remote backend. Let’s create a new file called backend.tf in a location where we have staged terraform code and copy-paste contents from below and update organization and workspace name:

terraform {
  backend "remote" {
    organization = "example-demo-org"

    workspaces {
      name = "example-demo-org-sandbox"
    }
  }
}

Note: You will need to update terraform to => .12.19 version to work with TF login.

Verify all information for backend.tf and cloud provider access key & token has been updated. After that execute the following command:

terraform login 

The above command should ask you to generate a token or if you already have token created, you can provide the token.

If everything goes as planned, you can execute the following commands next:

terrafrom init
terraform plan

If no errors are indicated, Terraform should spit out a plan with TF backend:

Wait! shouldn’t I see something in my workspace? no, not yet! with remote backend, only tfstate file & plan is stored on Terraform cloud. As soon as you apply the changes, you see the queued plan created for the stack asking for conformation.

Also, You can see my AWS account and my custom VPC is not created yet.

After confirming TF Plan – accept the changes and let’s see what happens.

Status changes from confirmation to applying.

Explore the TF Cloud stack, notice we can see the output as it is captured during the execution:

Congratulations! You have successfully created a stack using Terraform Cloud and stack statefile is managed by Terraform Cloud. In the next series, I will show you how to use Gitlab or Github with a remote repository.

If you have questions or stuck somewhere in this tutorial, please contact me or leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *