☑️Day 65: Terraform Variables and Configuration Management🚀

🔹Table of Contents :
Introduction
Step-by-Step Guide
Task 1: Setting Up a Simple Terraform Project
Task 2: Using
variables.tffor ConfigurationTask 3: Dynamic Configuration with Environment Variables
Command Summary
Real-Time Scenarios
✅Learning Objectives
Today, I delved deeper into Terraform variables and learned how to manage configurations effectively without modifying main.tf directly. This approach is particularly essential in DevOps to maintain stability, avoid errors, and promote best practices for infrastructure management. Here’s a breakdown of each task I completed, along with detailed commands and explanations.
✅Why Configuration Files Matter in DevOps
In DevOps, changing file contents or names directly in a primary configuration file (main.tf) is discouraged. This approach ensures that configurations are isolated and can be dynamically adjusted without risking the integrity of the core setup. Instead, we use separate files like variables.tf to define and manage variables centrally.
✅Step-by-Step Guide and Commands
✅Task 1: Setting Up a Simple Terraform Project with Variable Management
Create and Navigate to Directory
This directory will house the Terraform configuration files.
mkdir terraform-variables cd terraform-variables pwd
Create
main.tfDefine a basic resource using the
local_fileresource to generate a file with specified content.Command:
vim main.tfContent:
resource "local_file" "devops" { filename = "${pwd}/devops_test.txt" content = "This is a DevOps test file" }
Initialize and Apply
Initialize Terraform: This command sets up the required files and downloads necessary plugins.
terraform initApply Configuration: This creates the specified file in your directory.
terraform apply
✅Task 2: Using a Separate variables.tf File for Configuration
Create
variables.tfWe’ll define variables here, isolating these values from
main.tfso they can be adjusted independently.Command:
vim variables.tfContent:
variable "filename" { default = "${pwd}/devops-automated.txt" } variable "content" { default = "This is auto-generated from a variable" }
Modify
main.tfto Use VariablesWe now access our variables using the
varkeyword, which referencesvariables.tf.Command:
vim main.tfUpdated Content:
resource "local_file" "devops-var" { filename = var.filename content = var.content }
Apply the Changes
Plan: Check if Terraform recognizes the new configuration.
terraform planApply: Apply the updated configuration to generate
devops-automated.txt.terraform applyVerify the File:
ls cat devops-automated.txt
✅Task 3: Setting and Updating Environment Variables for Dynamic Values
Add a New Variable to
variables.tfDefine an additional variable,
devops_op_trainer, without setting a default value. This will allow for dynamic assignment.Command:
vim variables.tfUpdated Content:
variable "devops_op_trainer" {}
Set the Environment Variable
Command: Use
exportto setTF_VAR_devops_op_trainer, allowing Terraform to reference it.export TF_VAR_devops_op_trainer="TrainWithSL"
Reference Variable in
main.tfUpdate
main.tfto include an output block that displays the value ofdevops_op_trainer.Command:
vim main.tfUpdated Content:
output "devops_op_trainer" { value = var.devops_op_trainer }
Apply and Change Variable Dynamically
Apply: Check that the output shows
TrainWithSLas thedevops_op_trainer.terraform applyUpdate Environment Variable:
export TF_VAR_devops_op_trainer="Shubham_Londe"Reapply Changes: Run
applyagain to see the updated value.terraform apply
✅Commands Summary
mkdir terraform-variables: Creates a new project directory.cd terraform-variables: Navigate to the project directory.vimmain.tf: Create and define the main resource file.terraform init: Initialize the configuration.terraform apply: Apply the configuration changes.vimvariables.tf: Create and define the variables configuration file.terraform plan: Preview changes before applying.export TF_VAR_variable_name=value: Set environment variables for dynamic values.terraform apply(reapply): Apply configuration after changing environment variables.
✅Real-Time Scenarios
Environment-Specific Configurations: Defining variables in a separate file makes it easy to customize environments (development, testing, production) without editing core configuration files.
Dynamic Adjustments: Setting environment variables lets you change specific configurations, such as the trainer name, without directly editing files.
Output Variables: Useful for displaying dynamic information, such as the name of the deployment environment or the responsible DevOps engineer.
This method of using variables in separate files enhances the modularity and flexibility of Terraform projects. By adhering to best practices, we ensure that our configurations remain efficient, adaptable, and easy to manage across different environments.
Stay tuned for more hands-on tasks and in-depth learning!🚀
🚀Thanks for joining me on Day 65! Let’s keep learning and growing together!
Happy Learning! 😊
#90DaysOfDevOps




