Migrating Terraform State to Terraform Cloud

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

Hey there, Gurus! Welcome to the lab! In this lab, you will migrate your local Terraform state to Terraform Cloud for better collaboration between you and your trusty team. You will create and configure a Terraform Cloud workspace, where you will store your state for your EC2 instance remotely. Then, you will add a backend configuration to your Terraform configuration file and apply the updated configuration.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up the Environment

1) In the terminal, once logged into your instance, change to the lab-migrate-state directory.

2) Apply the Terraform configuration.

3) In the AWS Management Console, navigate to the cloud_user in IAM and generate your Access key ID and Secret access key values.

4) Copy or download your key to use during the Terraform Cloud setup.

Set Up Your Terraform Cloud Workspace

1) Navigate to Terraform Cloud at https://app.terraform.io/session and create a free account (or log in if you have an existing account).

2) Create an organization named ACG-Terraform-Labs.

3) Create a workspace named labs-migrate-state using the CLI-driven workflowoption.

4) Navigate to the Variables tab and add two environment variables: one named AWS_ACCESS_KEY_ID with a value of your Access key ID and one named AWS_SECRET_ACCESS_KEY with a value of your Secret access key.

5) Navigate to User Settings and then Tokens, and create an API token called terraform_login.

6) Copy your API token.

Add the Backend Configuration

1) Log in to Terraform Cloud with the API token.

2) Add the backend block provided to the configuration file.

3) Initialize your working directory.

4) Delete your terraform.tfstate file.

Apply the Updated Configuration and Confirm the State Was Saved to Terraform Cloud

1) Apply your updated configuration.

2) Confirm your state is saved to Terraform Cloud.

Additional Resources

Lab Scenario

You are an admin who supports one of the largest online comic book retailers in the world. You are one of a handful of Terraform admins who have been assigned to use Terraform to manage the company's cloud deployments. You have started working on some pretty important configurations for the company's infrastructure and would like to start utilizing Terraform Cloud to store your state remotely for backup and auditing purposes. To accomplish this, you will configure your local Terraform configuration to use Terraform Cloud to store your state remotely.

Lab Setup

To access the Terraform configuration files used in this lab, go to the ZIP file in the lab's GitHub repository.

To access Terraform, go to the Terraform Cloud login page.

Note: If you perform this lab more than once, be sure to delete the ACG-Terraform-Labs organization you created. If not, you will get an error message stating it already exists while setting up your Terraform Cloud workspace. When creating a workspace named labs-migrate-state, use the CLI-driven workflowoption.

Backend Configuration Code

To add the remote backend to the configuration file, use the following, replacing <ORG_NAME> with ACG-Terraform-Labs and Example-Workspace with lab-migrate-state:

```
terraform {
   backend "remote" {
     organization = "<ORG_NAME>"
     workspaces {
       name = "Example-Workspace"
     }
   }

   required_providers {
      aws = {
        source  = "hashicorp/aws"
        version = "~> 3.27"
      }
   }
}
```

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?