#
Bonus Hands-On Exercise: Using Remote State with Workspaces
#
Exercise Objective
Participants will extend their remote state setup by using Terraform workspaces. This will allow them to manage different environments (e.g., dev, staging, prod) using the same remote state backend in Azure Blob Storage.
#
Prerequisites
- Azure Storage Account and Blob Container: Ensure that you have already set up Azure Blob Storage for remote state, as outlined in the previous exercise.
- Terraform Backend Configured: Make sure that your
main.tf
file includes the backend block pointing to Azure Blob Storage.
#
Steps
#
Step 1: Modify the Backend Configuration to Use Workspaces
Open Your
main.tf
File:- Open your
main.tf
file where the Azure provider and backend configuration are defined.
- Open your
Update the Backend Block for Workspace Support:
- Modify the
key
parameter in the backend block to use theterraform.workspace
interpolation syntax, which will dynamically set the state file path based on the current workspace. - Example:
terraform { backend "azurerm" { resource_group_name = "<your-resource-group>" storage_account_name = "<your-storage-account-name>" container_name = "<your-container-name>" key = "${terraform.workspace}/terraform.tfstate" } }
- Here,
${terraform.workspace}
will dynamically create a separate state file for each workspace (e.g.,dev/terraform.tfstate
,staging/terraform.tfstate
, etc.).
- Modify the
Save the
main.tf
File:- Save your changes in
main.tf
.
- Save your changes in
#
Step 2: Initialize the Backend and Workspaces
Run
terraform init
to Reinitialize the Backend:- In the terminal, run the following command to reinitialize the backend with the updated configuration:
terraform init
- Terraform will detect the change in backend configuration and ask for confirmation to initialize with the new settings.
- In the terminal, run the following command to reinitialize the backend with the updated configuration:
Confirm Backend Initialization:
- Follow the prompts in the terminal and confirm the initialization.
#
Step 3: Create and Switch Between Workspaces
Create a New Workspace for the Development Environment:
- Use the following command to create a new workspace called
dev
:terraform workspace new dev
- Terraform will switch to the
dev
workspace after creating it. This workspace will now have its own state file in Azure Blob Storage (dev/terraform.tfstate
).
- Use the following command to create a new workspace called
Create a Workspace for Staging:
- Create another workspace called
staging
:terraform workspace new staging
- Terraform will automatically switch to the
staging
workspace after creating it. The state file for this workspace will be stored instaging/terraform.tfstate
.
- Create another workspace called
Create a Workspace for Production:
- Similarly, create a
prod
workspace for the production environment:terraform workspace new prod
- The state file for this workspace will be stored in
prod/terraform.tfstate
.
- Similarly, create a
Switch Between Workspaces:
- You can switch between the workspaces using the
select
command:terraform workspace select dev
- This command switches the current workspace back to
dev
, allowing you to manage resources specific to that environment.
- You can switch between the workspaces using the
#
Step 4: Apply the Configuration in Each Workspace
Run
terraform apply
in the Dev Workspace:- Make sure you are in the
dev
workspace by running:terraform workspace select dev
- Run
terraform apply
to apply the configuration in thedev
environment:terraform apply
- Type
yes
when prompted to confirm.
- Make sure you are in the
Repeat for Staging and Production Workspaces:
- Switch to the
staging
workspace and apply the configuration:terraform workspace select staging terraform apply
- Switch to the
prod
workspace and apply the configuration:terraform workspace select prod terraform apply
- Each environment will have its own state file stored in Azure Blob Storage under the respective workspace directory (e.g.,
dev/terraform.tfstate
,staging/terraform.tfstate
,prod/terraform.tfstate
).
- Switch to the
#
Step 5: Verify State Files in Azure Blob Storage
Log in to the Azure Portal:
- Go to the Azure Portal and navigate to the resource group containing the storage account.
Navigate to the Storage Account and Container:
- Open the storage account.
- In the left-hand menu, select Containers and then open the container where you stored the Terraform state files.
Verify the State Files:
- You should see separate directories or state files for each workspace (e.g.,
dev/terraform.tfstate
,staging/terraform.tfstate
, andprod/terraform.tfstate
). - Each state file will contain the specific resources applied in that workspace, allowing you to manage separate environments.
- You should see separate directories or state files for each workspace (e.g.,
#
Verification
Confirm Workspace Creation:
- Use
terraform workspace list
to view all created workspaces and verify thatdev
,staging
, andprod
are present.
- Use
Check Azure Blob Storage:
- Verify that each workspace has its own state file in Azure Blob Storage, confirming that remote state is correctly isolated by environment.
Troubleshoot Issues:
- If state files are not appearing as expected, ensure that the
key
parameter in the backend configuration uses${terraform.workspace}
syntax correctly.
- If state files are not appearing as expected, ensure that the