☑️Day 61: Creating My First Terraform Configuration with HCL🚀

🔹Table of Contents :

  • Introduction

  • Creating the First Terraform Configuration File

  • Important Terraform Commands

  • Running the First Terraform Script: Step-by-Step Guide

  • Real-World Scenarios for Using local_file in DevOps

  • Key Takeaways


1. Introduction to HCL (HashiCorp Configuration Language)

  • What is HCL?
    HCL is a human-readable configuration language developed by HashiCorp. It’s used to define resources and configurations in Terraform.

  • Why HCL?
    HCL’s syntax is designed for clarity and simplicity, making it easy for DevOps engineers to understand infrastructure code and collaborate effectively.

  • Writing HCL
    HCL syntax is structured and declarative, meaning you specify the desired end state, and Terraform takes care of provisioning or updating infrastructure to meet that state.


2. Creating the First Terraform Configuration File

  • File Setup
    Open a terminal and create a new file named local.tf using Vim or your preferred text editor:

      vim local.tf
    
  • Writing the Configuration in local.tf

    • Define a simple local file resource:

        resource "local_file" "devops" {
          filename = "/home/ubuntu/terraform-course/terraform-local/devops_automated.txt"
          content  = "I want to become a DevOps Engineer who knows Terraform"
        }
      

Explanation of the Configuration
Here, we’re creating a local_file resource named devops. This configuration:

  • Sets the file path to /home/ubuntu/terraform-course/terraform-local/devops_automated.txt.

  • Adds the content: “I want to become a DevOps Engineer who knows Terraform” to the file.

  • Real-World Example
    Local file resources can be used to log results, create configuration files dynamically, or document infrastructure changes.


3. Important Terraform Commands

  • terraform init

    • Purpose: Initializes your working directory and downloads any required provider plugins.

    • Usage: Run this command before other Terraform commands.

    • Command:

        terraform init
      
  • terraform validate

    • Purpose: Checks if your configuration is syntactically correct.

    • Usage: Ensures there are no syntax errors in your .tf files.

    • Command:

        terraform validate
      
  • terraform plan

    • Purpose: Creates a detailed execution plan, showing the changes Terraform will apply.

    • Usage: Helps you review changes before applying them to ensure accuracy.

    • Command:

        terraform plan
      
  • terraform apply

    • Purpose: Applies the changes defined in your configuration file, creating or updating resources.

    • Usage: Executes the specified configuration and provisions the infrastructure.

    • Command:

        terraform apply
      

4. Running the First Terraform Script: Step-by-Step

  • Step 1: Initialize the working directory:

      terraform init
    
  • Step 2: Validate the configuration:

      terraform validate
    
  • Step 3: Create a plan and review changes:

      terraform plan
    
  • Step 4: Apply the plan to create the file:

      terraform apply
    

5. Real-World Scenarios for Using local_file in DevOps

  • Configuration Documentation: Use local files to document the configurations created by Terraform.

  • Logging and Debugging: Track outputs and resource states during testing.

  • Automated File Creation: Generate dynamic files or logs for other applications or monitoring tools.


Creating this first Terraform file and understanding HCL syntax was a huge step forward in my Infrastructure as Code journey! By mastering these basics, I’m building a strong foundation for automating and managing complex infrastructure setups.

Stay tuned for more hands-on tasks and in-depth learning!🚀

🚀Thanks for joining me on Day 60! 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