Terraform Deployment in AWS using Terraform CLI Commands (workspace and state)

Infra as Code (IaC) | AWS Hero | Teacher | Mentor | Youtuber | -> Way to Cloud & MLOPS
Search for a command to run...

Infra as Code (IaC) | AWS Hero | Teacher | Mentor | Youtuber | -> Way to Cloud & MLOPS
No comments yet. Be the first to comment.
API (Application Programming Interface) security is critical for protecting sensitive data and maintaining the integrity of systems and applications. APIs are used to connect different systems and applications, and as a result, they can provide a gat...

Well after months of deep slumber, away from social media networks, I had to return to Pune and adapt to hybrid working. I was browsing through meetup.com and was intrigued by one event from AWS user group Pune. It is AWS Reinvent 2022 -Recap on 21st...

Episode 2

Simplify Access Control and Enhance Transparency Over Your ML Projects

Episode 1

In this hands-on lab, we will create two individual 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.

Clone Terraform Code and Switch to the Proper Directory
Create a New Workspace
Deploy Infrastructure in the Test Workspace and Confirm Deployment via the AWS Management Console
Deploy Infrastructure in the Default Workspace and Confirm Deployment via AWS Management Console
Destroy Resources in the Workspaces and Delete the Test Workspace
Let us log in to the lab server.
In a web browser, log in to the AWS Management Console using your credentials.
git clone https://github.com/samtechlab/content-hashicorp-certified-terraform-associate-foundations.git
cd content-hashicorp-certified-terraform-associate-foundations/section4-lesson3/
ls
The files in the directory should include main.tf and network.tf. These files basically use the ${terraform.workspace} variable to create parallel environments and decide which region the deployment occurs in, depending on the workspace you're in.
terraform workspace list
The output should only show the default workspace. This workspace cannot be deleted.
Note: When you use the terraform workspace list command to view the existing workspaces, the workspace which you are currently inside will be prepended with an asterisk (*) in front of the workspace name.
terraform workspace new test
You will be automatically switched into the newly created test workspace upon successful completion. However, you can confirm this using the terraform workspace list command if you'd like.
terraform init
cat main.tf
Note the configurations in the main.tf code, particularly:
AWS is the selected provider.
If the code is deployed on the default workspace, the resources will be deployed in the us-east-1 region.
If the code is deployed on any other workspace, the resources will be deployed in the us-west-2 region.
In the code creating the EC2 virtual machine, we have embedded the $terraform.workspace variable in the Name attribute, so we can easily distinguish those resources when they are created within their respective workspaces by their name:
< workspace_name >-ec2.
cat network.tf
In the code creating the security group resource, we have embedded the $terraform.workspace variable in the Name attribute, so we can easily distinguish those resources when they are created within their respective workspaces by their name:
< workspace name >-securitygroup.
terraform apply --auto-approve
Note: The --auto-approve flag will prevent Terraform from prompting you to enter yes explicitly before it deploys the code.
terraform state list
There should be a number of resources being tracked, including the resources spun up by the code just deployed.
terraform workspace select default
terraform state list
The return output should say that No state file was found! for this workspace.
Navigate to the AWS Management Console in your browser.
Click on N. Virginia (the us-east-1 region) at the top-right to engage the Region drop-down, and select US West (Oregon), or us-west-2.
Expand the Services drop-down and select EC2.
On the Resources page, click Instances.
Verify that the test-ec2 instance appears in the list.
In the menu on the left, click Security Groups.
Verify that the test-securitygroup resource appears in the list.
terraform workspace list
Again, the asterisk (*) prepended to the name confirms you are in the default workspace.
terraform apply --auto-approve
terraform state list
There should now be a number of resources being tracked, including the resources spun up by the code just deployed.
Navigate to the AWS Management Console in your browser.
Click on Oregon (the us-west-2 region) at the top-right to engage the Region drop-down, and select US East (N. Virginia), or us-east-1.
As you are already on the Security Groups page, verify that the default-securitygroup resource appears in the list.
In the menu on the left, click Instances.
Verify that the default-ec2 instance appears in the list.
terraform workspace select test
terraform destroy --auto-approve
Navigate to the AWS Management Console in your browser.
Click on N. Virginia (the us-east-1 region) at the top-right to engage the Region drop-down, and select US West (Oregon), or us-west-2 . As you are already on the Instances page, verify that the test-ec2 instance is shutting down or may have already been terminated.
In the menu on the left, click Security Groups.
Verify that the test-securitygroup resource no longer appears in the list.
Note: It may take some time for the resources to be terminated in the AWS Management Console, and you may need to refresh the browser a few times to confirm that the resources have been destroyed.
terraform workspace select default
terraform workspace delete test
terraform destroy --auto-approve
Congratulations — you've completed this hands-on lab!