#
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.tf
File:- 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.tf
File.
#
Step 2: Define Variables in variables.tf
Create a
variables.tf
File:- 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_name
andlocation
variables: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.tf
File.
#
Step 3: Create Separate Variable Files for Development and Production
Create a
dev.tfvars
File:- 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.tfvars
File:- 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.tf
File:- 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.tf
File.
#
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 plan
for the Development Environment:- Use
terraform plan
with the-var-file
flag 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-group
in the "East US" region.
- Use
Run
terraform apply
for the Development Environment:- Apply the configuration using the
dev.tfvars
file to create the Resource Group for development:terraform apply -var-file="dev.tfvars"
- Type
yes
when prompted to confirm the apply operation. - After the apply completes, note the output values for
resource_group_name
andresource_group_location
, which should reflect the development environment settings.
- Apply the configuration using the
Repeat for the Production Environment:
- Run
terraform plan
andterraform apply
using theprod.tfvars
file: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-group
and 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-group
andprod-resource-group
have been created in the respective regions specified indev.tfvars
andprod.tfvars
.
- Check that both
#
Verification
Confirm Environment-Specific Resources:
- Ensure that the Resource Groups in Azure match the names and locations specified in
dev.tfvars
andprod.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 apply
reflect the correct resource group name and location for each environment.
- Verify that the output values displayed in the terminal during