Using Terraform CLI Commands (workspace and state) to Manipulate a Terraform Deployment

1.5 hours
  • 5 Learning Objectives

About this Hands-on Lab

In this hands-on lab, we will create two distinct, parallel environments against the same Terraform code using the `terraform workspace` command. We will also use the `terraform state` command to see what resources are being tracked in the state files of the different workspaces.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Clone Terraform Code and Switch to the Proper Directory
  1. Clone the code required for the lab from the provided GitHub repository.
  2. Switch to the appropriate directory where the code required for this lab resides.
  3. List the files in the directory.
Create a New Workspace
  1. Check that no workspace other than the default one exists using the terraform workspace list command.
  2. Create a new workspace named test using the terraform workspace new command.
Deploy Infrastructure in the Test Workspace and Confirm Deployment via the AWS Management Console
  1. In the test workspace, initialize the working directory and download the required providers.
  2. Examine the code in the main.tf and network.tf files to become familiar with the resources that will be created upon deployment.
  3. Deploy the code with the terraform apply --auto-approve command.
  4. Confirm that Terraform is tracking resources via a state file in the test workspace.
  5. Confirm that Terraform is not tracking resources via a state file in the default workspace.
  6. Verify that the resources were created as intended in the test workspace via the AWS Management Console.
Deploy Infrastructure in the Default Workspace and Confirm Deployment via AWS Management Console
  1. Deploy the code in the default workspace with the terraform apply --auto-approve command.
  2. Confirm that Terraform is now tracking resources via a state file in the default workspace.
  3. Verify that the resources were created as intended in the default workspace via the AWS Management Console.
Destroy Resources in the Workspaces and Delete the Test Workspace
  1. In the test workspace, tear down the infrastructure using the terraform destroy --auto-approve command.
  2. Verify that the resources were destroyed via the AWS Management Console.
  3. In the default workspace, delete the test workspace you created using the terraform workspace delete command.
  4. In the default workspace, tear down the infrastructure using the terraform destroy --auto-approve command.

Additional Resources

Link to the GitHub respository containing Terraform code for this lab:

https://github.com/linuxacademy/content-hashicorp-certified-terraform-associate-foundations.git

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?