☑️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.tf for Configuration

  • Task 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

  1. Create and Navigate to Directory

    • This directory will house the Terraform configuration files.

        mkdir terraform-variables
        cd terraform-variables
        pwd
      
  2. Create main.tf

    • Define a basic resource using the local_file resource to generate a file with specified content.

    • Command:

        vim main.tf
      
    • Content:

        resource "local_file" "devops" {
          filename = "${pwd}/devops_test.txt"
          content  = "This is a DevOps test file"
        }
      
  3. Initialize and Apply

    • Initialize Terraform: This command sets up the required files and downloads necessary plugins.

        terraform init
      
    • Apply Configuration: This creates the specified file in your directory.

        terraform apply
      

Task 2: Using a Separate variables.tf File for Configuration

  1. Create variables.tf

    • We’ll define variables here, isolating these values from main.tf so they can be adjusted independently.

    • Command:

        vim variables.tf
      
    • Content:

        variable "filename" {
          default = "${pwd}/devops-automated.txt"
        }
        variable "content" {
          default = "This is auto-generated from a variable"
        }
      
  2. Modify main.tf to Use Variables

    • We now access our variables using the var keyword, which references variables.tf.

    • Command:

        vim main.tf
      
    • Updated Content:

        resource "local_file" "devops-var" {
          filename = var.filename
          content  = var.content
        }
      
  3. Apply the Changes

    • Plan: Check if Terraform recognizes the new configuration.

        terraform plan
      
    • Apply: Apply the updated configuration to generate devops-automated.txt.

        terraform apply
      
    • Verify the File:

        ls
        cat devops-automated.txt
      

Task 3: Setting and Updating Environment Variables for Dynamic Values

  1. Add a New Variable to variables.tf

    • Define an additional variable, devops_op_trainer, without setting a default value. This will allow for dynamic assignment.

    • Command:

        vim variables.tf
      
    • Updated Content:

        variable "devops_op_trainer" {}
      
  2. Set the Environment Variable

    • Command: Use export to set TF_VAR_devops_op_trainer, allowing Terraform to reference it.

        export TF_VAR_devops_op_trainer="TrainWithSL"
      
  3. Reference Variable in main.tf

    • Update main.tf to include an output block that displays the value of devops_op_trainer.

    • Command:

        vim main.tf
      
    • Updated Content:

        output "devops_op_trainer" {
          value = var.devops_op_trainer
        }
      
  4. Apply and Change Variable Dynamically

    • Apply: Check that the output shows TrainWithSL as the devops_op_trainer.

        terraform apply
      
    • Update Environment Variable:

        export TF_VAR_devops_op_trainer="Shubham_Londe"
      
    • Reapply Changes: Run apply again 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.

  • vim main.tf: Create and define the main resource file.

  • terraform init: Initialize the configuration.

  • terraform apply: Apply the configuration changes.

  • vim variables.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

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
10
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti