Solution Review: Create a Project That is Aware of the Workspace

In this lesson, we'll discuss the solution to the Terraform project we discussed in the last lesson.

Problem statement#

Part#1#

Create a project that is aware of the workspace. The project creates an sqs queue and s3 bucket and uses the workspace name in the name of the sqs queue and s3 bucket. Apply your project to a workspace called dev and a workspace called prod.

Part#2#

Remove the sqs queue and apply it to both workspaces so that the queue is removed.

Part#3#

Destroy all of the resources you have created.

Solution#

Let’s discuss the solution of each part one by one and how you can run them.

Part #1#

Let’s implement the solution of project part 1:

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
/
main.tf
Terraform solution project part1 implementation

For part 1, we configure an AWS provider by setting the region property to the region that we want to create our AWS infrastructure in, which is eu-west-2 in this case. We create an SQS queue using an aws_sqs_queue resource, and for the name, we use the special ${terraform.workspace} value to get the name of the current workspace. We repeat this with the name of the S3 bucket in the aws_s3_bucket resource.

To create a new workspace called "dev", we have to run terraform workspace new dev. This command creates the workspace “dev” and switches to it so that it becomes active.

Next, we run terraform init and terraform apply to create the infrastructure. We want to create the same infrastructure again, but this time, we will use a workspace called "prod". To create this workspace, we run terraform workspace new prod.

Next, we run terraform init and terraform apply. Terraform will replace the variable ${terraform.workspace} anywhere it has been used with our current workspace name ("prod").

Running the project#

Using the following commands, you can run the project:

Terraform commands used to run the project part 1

📝Note: When the command terraform workspace new dev is run, terraform will create a new workspace called dev and switch to it all in one command.

Part#2#

Let’s implement the solution of the project part 2:

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
/
main.tf
Terraform solution project part2 implementation

To solve part 2, we first delete the aws_sqs_queue resource block entirely. Next, to physically delete the queue from AWS, we have to run terraform apply. As we are still currently using the "prod" workspace from when we finished part 1, the queue "prod-queue" will be deleted.

To delete the queue from the dev workspace, we first have to switch to that workspace by running terraform workspace select dev. Next, we have to apply Terraform using terraform apply. This will delete the "dev-queue".

Running the project#

Using the following commands, you can run the project:

Terraform commands used to run the project part 2

Part#3#

To delete all resources you have created, use the following commands:

Terraform commands used to run the project part 3

For part 3, we run terraform destroy to destroy everything we have created. This will destroy the my-awesome-bucket-dev S3 bucket as we currently have the dev workspace selected from part 2. Once the S3 bucket is destroyed, we need to destroy the prod one. To do this, we have to first switch to the prod workspace using the command terraform workspace select prod. Once the prod workspace is active, we can destroy the prod S3 bucket using the terraform destroy command.

Project: Create a Project That is Aware of the Workspace
Final Remarks
Mark as Completed
Report an Issue