Migrate an Existing Terraform Project into Terraform Cloud

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

In this hands-on lab, we will take an existing Terraform configuration and convert it to run the code using Terraform Cloud. We will use the CLI-driven method to deploy our resources to AWS with Terraform Cloud doing the bulk of the work.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Prepare the Environment
  1. Log in to the lab instance.

  2. Clone the lab repo to your lab machine:

    git clone https://github.com/ACloudGuru-Resources/content-introduction-terraform-cloud-lab.git
  3. Create the terraform-guru organization.

  4. Select CLI-driven workflow.

  5. Create the terraform-cloud-guru workspace.

  6. Create a variable set by going to your workspace settings and then Variable sets. Click Create variable set.

  7. Use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

  8. In the AWS Management Console, create an access key for the cloud_user and secret access key, and use those values for the variable set.

  9. In the user settings, create a token for the CLI to use with Terraform Cloud called terraform-cloud. You must copy the key, run terraform login in your terminal, and follow the prompts.

Migrate the Existing Terraform Project into Terraform Cloud
  1. Modify the main.tf file, and add the Terraform Cloud config.
  2. Add the Terraform variables in Terraform Cloud for your workspace for the ami, instance_count,instance_type, and subnet

    Note: You will need to look this up from the console or CLI. All other variables will be in the variables.tf file.

  3. Modify the variables so they use the Terraform Cloud variables.
Deploy the EC2 Instances with Terraform Cloud
  1. Initialize your working directory.
  2. Validate your code.
  3. Apply your configuration, and view it in your terminal and Terraform Cloud Runs section.
  4. Check the AWS Management Console to verify the instances were created.
Create a Terraform Module from Existing Code
  1. Create a GitHub repo called terraform-as-ec2-psacg.
  2. Add the Terraform Cloud application, and configure it.
  3. Add the module configuration to your repo called main.tf.
  4. Add a tag to the repo of 1.0.0.
  5. Add the module to Terraform Cloud Registry.
Modify the Terraform Configuration to use the Module and Redeploy
  1. Edit the main Terraform configuration to use the module.
  2. Initialize the Terraform working directory.
  3. Validate the syntax.
  4. Apply your new configuration.
  5. Confirm the changes.

Additional Resources

In this hands-on lab, you will clone the lab Git repo that will contain the existing Terraform config you will need to modify for use with your Terraform Cloud account. You will need to set up your Terraform Cloud workspace called terraform-cloud-guru in the terraform-guru organization, as well as create some variables for some of the resources in the the Terraform config file. Once you have added the cloud config and variables, you will then apply the config that will deploy two EC2 instances called TERRAFORM-GURU-0 and TERRAFORM-GURU-1 to AWS. From there, you will create a module with your configuration and store it in GitHub, which will then be used by Terraform Cloud as a module repository. You will then use that module to deploy those same two EC2 instances to AWS to complete the lab.

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?