#
Bonus Hands-On Exercise: Using Variables and Outputs Across Multiple Environments
#
Exercise Objective
Participants will create separate variable files for development and production environments, allowing them to provision the same infrastructure with different parameters and outputs. This exercise will help participants understand how to manage multiple environments with Terraform using variable files.
#
Steps
#
Step 1: Set Up the Project Directory and Base Configuration
Create a New Project Directory (if not already created):
- Open a terminal and create a new directory for the project, then navigate into it:
mkdir multi-environment-project cd multi-environment-project
- Open a terminal and create a new directory for the project, then navigate into it:
Create a
main.tfFile:- In your project directory, create a file named
main.tf. - Define the Azure provider and a Resource Group resource in
main.tf:provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.location } - This configuration uses variables to set the name and location of the Resource Group, allowing the values to be customized by environment.
- In your project directory, create a file named
Save the
main.tfFile.
#
Step 2: Define Variables in variables.tf
Create a
variables.tfFile:- In the same project directory, create a new file named
variables.tf.
- In the same project directory, create a new file named
Define Variables for Resource Group Name and Location:
- In
variables.tf, add the following code to define theresource_group_nameandlocationvariables:variable "resource_group_name" { description = "The name of the resource group" type = string } variable "location" { description = "The Azure region to deploy resources" type = string } - These variables will be set differently for each environment using separate variable files.
- In
Save the
variables.tfFile.
#
Step 3: Create Separate Variable Files for Development and Production
Create a
dev.tfvarsFile:- In your project directory, create a file named
dev.tfvars. - Add the following variable values specific to the development environment:
resource_group_name = "dev-resource-group" location = "East US"
- In your project directory, create a file named
Create a
prod.tfvarsFile:- In your project directory, create another file named
prod.tfvars. - Add the following variable values specific to the production environment:
resource_group_name = "prod-resource-group" location = "West US"
- In your project directory, create another file named
Save Both Variable Files.
#
Step 4: Define Outputs in outputs.tf
Create an
outputs.tfFile:- In your project directory, create a new file named
outputs.tf.
- In your project directory, create a new file named
Define Outputs for Resource Group Name and Location:
- In
outputs.tf, add the following output definitions to display the Resource Group’s name and location:output "resource_group_name" { description = "The name of the resource group" value = azurerm_resource_group.example.name } output "resource_group_location" { description = "The location of the resource group" value = azurerm_resource_group.example.location }
- In
Save the
outputs.tfFile.
#
Step 5: Run Terraform Commands for Each Environment
Initialize the Project:
- In the terminal, run the following command to initialize the project and download provider plugins:
terraform init
- In the terminal, run the following command to initialize the project and download provider plugins:
Run
terraform planfor the Development Environment:- Use
terraform planwith the-var-fileflag to preview the changes for the development environment:terraform plan -var-file="dev.tfvars" - Review the output to ensure that Terraform will create a Resource Group with the name
dev-resource-groupin the "East US" region.
- Use
Run
terraform applyfor the Development Environment:- Apply the configuration using the
dev.tfvarsfile to create the Resource Group for development:terraform apply -var-file="dev.tfvars" - Type
yeswhen prompted to confirm the apply operation. - After the apply completes, note the output values for
resource_group_nameandresource_group_location, which should reflect the development environment settings.
- Apply the configuration using the
Repeat for the Production Environment:
- Run
terraform planandterraform applyusing theprod.tfvarsfile:terraform plan -var-file="prod.tfvars" terraform apply -var-file="prod.tfvars" - Confirm the creation of the Resource Group for the production environment, which should have the name
prod-resource-groupand location "West US".
- Run
#
Step 6: Verify Resource Groups in Azure Portal
Log in to the Azure Portal:
- Go to the Azure Portal.
Navigate to Resource Groups:
- Check that both
dev-resource-groupandprod-resource-grouphave been created in the respective regions specified indev.tfvarsandprod.tfvars.
- Check that both
#
Verification
Confirm Environment-Specific Resources:
- Ensure that the Resource Groups in Azure match the names and locations specified in
dev.tfvarsandprod.tfvars.
- Ensure that the Resource Groups in Azure match the names and locations specified in
Confirm Output Values:
- Verify that the output values displayed in the terminal during
terraform applyreflect the correct resource group name and location for each environment.
- Verify that the output values displayed in the terminal during