Introducing Terraform Cloud

Mohamed Ait Si Hamou
June 1st, 2020 · 3 min read

In this article, I will talk about Terraform Cloud and the most important things to know before exploring this amazing tool.

What is Terraform Cloud ?

Terraform Cloud is a platform that helps teams to collaborate on infrastructure configurations. It manages Terraform runs to provision infrastructure in a consistent and reliable environment. TF Cloud includes easy access to shared state and secret data, access controls and more.

Terraform Cloud offers a number of basic functionalities free of charge, as well as additional functionalities in paid levels. more details

tf_overview.

Sign up for Terraform Cloud

Prerequisites

Terraform can manage resources on many different providers and connect to many popular Version Control Systems (VCS’s), this guide requires:

  • An AWS account
  • A GitHub account

Create an account

Create a Terraform Cloud account at sign up here.

sign-up.

Confirm your email address before moving on.

Create your organization

You can create a new organization after you sign in for the first time.

Enter an organization name and an admin email address .

Note: If you’re joining an existing team of Terraform Cloud users, you have to ask the admin of the organization to add your email before you can perform any provisioning tasks.

new-organization

Set Up Workspace

After creating an account and an organization on Terraform Cloud. In this section you will learn how to create a TF Cloud workspace by connecting to a VCS repository.

1. Connect to GitHub

On the “New Workspace” page, choose GitHub from the drop-down to continue.

new-workspace

Then you have to click the green “Allow” button to connect Terraform Cloud to your GitHub account and a request will be sent to your Github organization/account admin to allow access from Terraform Cloud.

authorize-github

Note: If you are a user under a Github organisation, you need to ask the repository admin to allow the access.

2. Choose the repository

Choose the repository that hosts your Terraform source code from the list of your GitHub repositories.

3. Create the workspace

A workspace name should tell your colleagues what the workspace is used for. For example: project_name_env

workspace-settings
  • Select the working directory that Terrafrorm will use to execute the plan if any file has been changed in the folder.

  • Choose triger run planning (always trigger run or based on th paths added).

  • Select the branch which to import new version (default to master).

Finnaly, you can also create a Terrform cloud workspace using Terrform code. For example:

1resource "tfe_workspace" "prd_eu-west-1" {
2 name = "project_name_prd"
3 organization = "organization_name"
4 auto_apply = false
5 operations = true
6 queue_all_runs = true
7 terraform_version = "0.11.13"
8 file_triggers_enabled = true
9 working_directory = "providers/aws/eu-west-1/prd"
10 vcs_repo {
11 identifier = "${var.identifier}"
12 branch = "master"
13 ingress_submodules = false
14 oauth_token_id = "${tfe_oauth_client.github.oauth_token_id}"
15 }
16}
17
18resource "tfe_oauth_client" "github" {
19 organization = "organization_name"
20 api_url = "https://api.github.com"
21 http_url = "https://github.com"
22 oauth_token = "${var.github_token}"
23 service_provider = "github"
24}

4. Configure workspace variables

Within the Terraform Cloud UI, you will notice several menus and options for your workspace, including “Runs”, “States”, “Variables”, “Settings”, and the “Queue plan”.

workspace

You can edit variables as soon as you’ve created a workspace, by clicking the workspace’s “Variables” tab.

Terraform Cloud supports both Terraform Variables (as declared in your Terraform configurations) and Environment Variables. We’ll use both types in this guide.

environment-variables
  • Environment variables

Scroll down to the “Environment Variables” section, and create two variables.

Click the ”+ Add Variable” button to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Don’t forget to hide secrets using “Sensitive” checkbox. Marking a variable as sensitive prevents anybody (including you) from viewing/updating its value in Terraform Cloud’s UI or API.

  • Terraform variables

Variable values are strings by default. To enter list or map values, click the variable’s “HCL” checkbox. For example:

1{
2 public_cidr_ranges = [
3 "10.100.0.0/24"
4 ],
5
6 private_cidr_ranges = [
7 "10.110.0.0/24",
8 ]
9}

If a workspace is configured to use Terraform 0.10.0 or later, you can commit any number of *.auto.tfvars files to provide default variable values. Terraform will automatically load variables from those files.

Note: It’s not recomended to push your tfvars that holds secrets to your VCS.

Migrating your Terraform state to Terraform Cloud

Note: We recommend using Terraform v0.11.13 or newer with this backend.

If the Terraform configuration has an existing backend configuration block (s3,local…), delete it now.

Add a new backend block to the configuration:

1terraform {
2 backend "remote" {
3 hostname = "app.terraform.io"
4 organization = "my-org"
5
6 workspaces {
7 name = "my-workspace"
8 }
9 }
10}

Use the remote backend.

  • In the organization attribute, specify the name of your Terraform Cloud organization.
  • The hostname attribute is only necessary with Terraform Enterprise instances. You can omit it if you’re using the SaaS version of Terraform Cloud.
  • Specify the name of your workspace.
  • Run terraform init to migrate the workspace (answer “yes” and Terraform will migrate your state).

In order to retrieve state data from a Terraform backend don’t forget to replace your remote state with:

1data "terraform_remote_state" "vpc" {
2 backend = "remote"
3
4 config = {
5 organization = "hashicorp"
6 workspaces = {
7 name = "vpc-prod"
8 }
9 }
10}

Now you will be able to see tfstate file of your workspace, this will also be generated after each successful run.

states

Queue a run in the new workspace

Each workspace has its own queue of races that are started automatically after by pushing one or more new commits to the master branch of that repo. You can also queue plans manually with the “Queue Plan” button, usually after changing variables.

runs-confirm

By default, execution plans must be confirmed before Terraform Cloud applies them. Only users with write access can “Confirm and Apply” or “Cancel Plan” button to complete or cancel an execution. If necessary, use the “View Plan” button to get more details on the changes made to the run.

Note: You can enable automatic applies in the workspace settings.


Thanks for reading! I hope you get enough to start using Terraform Cloud.

Other recommended readings:

learn.hashicorp.com

www.terraform.io

@aitsim :)

More articles from Obytes

GraphQl In Django - An Overview

An overview of GraphQl API with Django

April 8th, 2020 · 5 min read

Autoscale Celery workers on ECS Fargate based on RabbitMQ metrics.

Autoscale Celery workers on ECS Fargate based on RabbitMQ queue depth metrics pushed to CloudWatch using a Lambda function.

April 3rd, 2020 · 2 min read

ABOUT US

Our mission and ambition is to challenge the status quo, by doing things differently we nurture our love for craft and technology allowing us to create the unexpected.