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