#
Bonus Hands-On Exercise: Creating a Configuration with Variables and Outputs
#
Exercise Objective
Participants will enhance their Terraform configuration by adding variables to make resource definitions more dynamic and outputs to capture key information about the resources provisioned (e.g., the name of the Resource Group or its location).
#
Steps
#
Step 1: Define Variables for Resource Configuration
Create a
variables.tf
File:- If you haven’t already, create a
variables.tf
file in your Terraform project directory.
- If you haven’t already, create a
Define Variables in
variables.tf
:- Add variable definitions for the Resource Group name and location. This will allow you to change these values dynamically without modifying the resource configuration directly.
- Example:
# Define a variable for the Resource Group name variable "resource_group_name" { description = "The name of the Azure Resource Group" type = string default = "myDynamicResourceGroup" } # Define a variable for the Azure region (location) variable "location" { description = "The Azure region where the Resource Group will be created" type = string default = "East US" }
Save the
variables.tf
File:- Save the file to apply the variable definitions to your Terraform configuration.
#
Step 2: Update main.tf
to Use Variables
Open the
main.tf
File:- Open your existing
main.tf
file where the Azure provider and Resource Group resource are defined.
- Open your existing
Modify the Resource Group to Use Variables:
- Update the
azurerm_resource_group
resource block to use the variables defined invariables.tf
. - Example:
# Configure the Azure provider provider "azurerm" { features {} } # Create an Azure Resource Group using variables resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.location }
- Here,
var.resource_group_name
andvar.location
reference the values of the variables defined invariables.tf
, making the configuration more flexible.
- Update the
Save the
main.tf
File:- Save the changes in
main.tf
.
- Save the changes in
#
Step 3: Define Outputs to Capture Resource Information
Create an
outputs.tf
File:- In your project directory, create a new file named
outputs.tf
. This file will contain the output definitions.
- In your project directory, create a new file named
Define Outputs in
outputs.tf
:- Add output definitions to capture information about the provisioned Resource Group, such as its name and location.
- Example:
# Output the Resource Group name output "resource_group_name" { description = "The name of the resource group" value = azurerm_resource_group.example.name } # Output the Resource Group location output "resource_group_location" { description = "The location of the resource group" value = azurerm_resource_group.example.location }
Save the
outputs.tf
File:- Save the
outputs.tf
file to apply the output definitions to your Terraform configuration.
- Save the
#
Step 4: Initialize and Run terraform plan
to Preview Changes
Initialize the Project (if not already initialized):
- In the terminal, navigate to the project directory and run:
terraform init
- This command downloads necessary provider plugins and prepares the working directory for Terraform operations.
- In the terminal, navigate to the project directory and run:
Run
terraform plan
:- Run the following command to preview the changes Terraform will make:
terraform plan
- The plan output should show that Terraform will create a Resource Group with the specified name and location.
- Run the following command to preview the changes Terraform will make:
Review the Plan Output:
- Verify that the plan output confirms Terraform will create the specified Resource Group.
- The output should include information about the planned creation of the Resource Group.
#
Step 5: Apply the Configuration to Provision the Resource Group
Run
terraform apply
:- Run the following command to apply the configuration and create the Resource Group:
terraform apply
- When prompted, type "yes" to confirm the apply operation.
- Run the following command to apply the configuration and create the Resource Group:
Review the Output Values:
- After Terraform completes the apply, check the terminal output for the values of
resource_group_name
andresource_group_location
. - Example output:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: resource_group_name = "myDynamicResourceGroup" resource_group_location = "East US"
- After Terraform completes the apply, check the terminal output for the values of
Verify the Resource in the Azure Portal (optional):
- Go to the Azure Portal and navigate to Resource Groups to confirm that the Resource Group has been created with the correct name and location.
#
Verification
Confirm Variable Use:
- Check that the Resource Group was created with the name and location specified in the variables.
Confirm Outputs:
- Verify that the output values for
resource_group_name
andresource_group_location
are displayed after runningterraform apply
.
- Verify that the output values for
Troubleshoot Issues (if any):
- If any errors occur, check that the variables are correctly referenced in
main.tf
and thatoutputs.tf
is properly defined.
- If any errors occur, check that the variables are correctly referenced in