Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      Handling JavaScript Event Listeners With Parameters

      July 21, 2025

      ChatGPT now has an agent mode

      July 21, 2025

      Scrum Alliance and Kanban University partner to offer new course that teaches both methodologies

      July 21, 2025

      Is ChatGPT down? You’re not alone. Here’s what OpenAI is saying

      July 21, 2025

      I found a tablet that could replace my iPad and Kindle – and it’s worth every penny

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 21, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 21, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Execute Ping Commands and Get Back Structured Data in PHP

      July 21, 2025
      Recent

      Execute Ping Commands and Get Back Structured Data in PHP

      July 21, 2025

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 21, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 21, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      I Made Kitty Terminal Even More Awesome by Using These 15 Customization Tips and Tweaks

      July 21, 2025
      Recent

      I Made Kitty Terminal Even More Awesome by Using These 15 Customization Tips and Tweaks

      July 21, 2025

      Microsoft confirms active cyberattacks on SharePoint servers

      July 21, 2025

      How to Manually Check & Install Windows 11 Updates (Best Guide)

      July 21, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Operating Systems»Linux»Modules in Terraform: Creating Reusable Infrastructure Code

    Modules in Terraform: Creating Reusable Infrastructure Code

    July 21, 2025

    Welcome to Terraform Modules, Infra Coders!

    Hey there, Infra coders! So far, you’ve mastered installing Terraform, connecting it to a cloud provider, and understanding the all-important state file. Now, let’s level up with Terraform modules. Think of modules as reusable blueprints—like a recipe you can use to cook the same dish in different kitchens without rewriting the whole thing. Modules make your Terraform code cleaner, reusable, and easier to manage, especially when your projects get bigger or involve a team.

    In this article, we’ll explore what modules are, why they’re awesome, how to create and use them, and some best practices to keep your code tidy. Let’s jump in!

    Understanding Terraform Modules
    Understanding Terraform Modules

    What Are Terraform Modules?

    A Terraform module is a set of Terraform configuration files grouped together to create a specific piece of infrastructure. Instead of writing the same code over and over for, say, an S3 bucket or a virtual machine, you bundle that code into a module. Then, you can reuse it across projects or environments (like dev, staging, or prod) with just a few tweaks.

    For example, imagine you need an S3 bucket with specific settings in multiple projects. Without modules, you’d copy-paste the same code everywhere. With a module, you write the bucket code once, store it in a folder, and call it whenever you need it. It’s like having a LEGO block you can snap into any creation!

    Why Use Modules?

    Modules are a game-changer for several reasons:

    • Reusability: Write once, use everywhere. No more copy-pasting!
    • Consistency: Ensure the same setup (like an S3 bucket or server) is identical across projects.
    • Simplicity: Break complex infrastructure into smaller, manageable pieces.
    • Team Collaboration: Share modules with your team to standardize infrastructure.
    • Less Maintenance: Update a module in one place, and all projects using it get the update.

    How Do Modules Work?

    A module is just a folder with Terraform files (like main.tf, variables.tf, or outputs.tf). You call a module from your main Terraform configuration using a module block. Here’s the basic flow:

    1. Create a Module: Write Terraform code in a folder (e.g., s3-module/) to define a resource like an S3 bucket.
    2. Use the Module: In your main project, reference the module and pass in custom settings (like the bucket name).
    3. Run Terraform: Terraform combines the module’s code with your configuration and creates the resources.

    Let’s walk through creating and using a module to see it in action.

    Step 1: Creating Your First Module

    Let’s create a simple module to set up an AWS S3 bucket with some standard settings. Create a folder structure like this:

    
    my-project/
    ├── main.tf
    └── s3-module/
        ├── main.tf
        ├── variables.tf
        └── outputs.tf
    
    

    Write the Module Code

    In the s3-module/ folder, add these files:

    In s3-module/main.tf:

    
    resource "aws_s3_bucket" "bucket" {
      bucket = var.bucket_name
      tags   = {
        Environment = var.environment
      }
    }
    
    

    In s3-module/variables.tf:

    
    variable "bucket_name" {
      description = "Name of the S3 bucket"
      type        = string
    }
    
    variable "environment" {
      description = "Environment for the bucket (e.g., dev, prod)"
      type        = string
      default     = "dev"
    }
    
    

    In s3-module/outputs.tf:

    
    output "bucket_id" {
      description = "The ID of the created S3 bucket"
      value       = aws_s3_bucket.bucket.id
    }
    
    

    This module creates an S3 bucket with a name and environment tag you specify. The outputs.tf file lets you retrieve the bucket’s ID for use elsewhere.

    Step 2: Using the Module

    Now, let’s use this module in your main project. In my-project/main.tf, add:

    
    provider "aws" {
      region = "us-east-1"
    }
    
    module "my_s3_bucket" {
      source      = "./s3-module"
      bucket_name = "my-unique-bucket-123"
      environment = "prod"
    }
    
    output "bucket_id" {
      value = module.my_s3_bucket.bucket_id
    }
    
    

    Here’s what’s happening:

    • The module block calls the s3-module folder using source = "./s3-module".
    • We pass in values for bucket_name and environment.
    • The output block grabs the bucket ID from the module to display it after creation.

    Step 3: Running the Module

    Navigate to your my-project/ folder in your terminal and run these commands:

    1. Initialize: Downloads the AWS provider and sets up the module:
      terraform init
      
    2. Plan: Checks what Terraform will create:
      terraform plan
      
    3. Apply: Creates the S3 bucket:
      terraform apply
      

      Type yes to confirm, and Terraform will create the bucket using your module. You’ll also see the bucket ID in the output.

    Terraform Modules
    Terraform Modules

    Where to Store Modules

    You can store modules in a few places:

    • Local Path: Like ./s3-module, great for testing or small projects.
    • Git Repository: Host modules on GitHub or GitLab (e.g., source = "git::https://github.com/your-repo/s3-module.git") for team sharing.
    • Terraform Registry: Use public or private modules from registry.terraform.io (e.g., source = "terraform-aws-modules/s3-bucket/aws").

    For example, to use a public module from the Terraform Registry, you could replace your module with:

    
    module "my_s3_bucket" {
      source  = "terraform-aws-modules/s3-bucket/aws"
      version = "4.1.0"
      bucket  = "my-unique-bucket-123"
    }
    
    

    This pulls a community-tested S3 module, saving you time.

    Best Practices for Terraform Modules

    To keep your modules clean and effective, follow these tips:

    • Keep Modules Focused: Each module should do one thing well (e.g., create an S3 bucket, not a bucket plus a database).
    • Use Variables: Make modules flexible with variables for names, regions, or other settings.
    • Document Everything: Add descriptions to variables and outputs to make your module user-friendly.
    • Version Modules: If sharing modules via Git or Terraform Registry, use version tags (like v1.0.0) to track changes.
    • Test Modules: Test your module in a sandbox environment before using it in production.
    • Avoid Hardcoding: Don’t put fixed values (like bucket names) in the module—use variables instead.

    Cleaning Up

    To remove the S3 bucket you created, run:

    terraform destroy
    

    Type yes to confirm, and Terraform will delete the bucket. This keeps your AWS account tidy and cost-free.

    What’s Next?

    Great job, Infra coders! You’ve just built and used your first Terraform module. You’re now ready to make your infrastructure code reusable and organized. In the next article, we’ll dive into variables and outputs to make your Terraform setups even more flexible and powerful. Keep coding, and see you soon!

    The post Modules in Terraform: Creating Reusable Infrastructure Code appeared first on TecAdmin.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleBeyond the Corporate Mold: How 21 TSI Sets the Future of Sports in Motion
    Next Article A Primer on Focus Trapping

    Related Posts

    Learning Resources

    I Made Kitty Terminal Even More Awesome by Using These 15 Customization Tips and Tweaks

    July 21, 2025
    Operating Systems

    Microsoft confirms active cyberattacks on SharePoint servers

    July 21, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    LLMs Create a New Supply Chain Threat: Code Package Hallucinations

    Development

    CVE-2025-52776 – Thanhtungtnt Video List Manager Cross-site Scripting

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-3816 – Westboy CicadasCMS OS Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Gladinet’s Triofox and CentreStack Under Active Exploitation via Critical RCE Vulnerability

    Development

    Highlights

    CVE-2025-48285 – Falang Multilanguage CSRF Vulnerability

    May 19, 2025

    CVE ID : CVE-2025-48285

    Published : May 19, 2025, 3:15 p.m. | 1 hour, 13 minutes ago

    Description : Cross-Site Request Forgery (CSRF) vulnerability in sbouey Falang multilanguage allows Cross Site Request Forgery. This issue affects Falang multilanguage: from n/a through 1.3.61.

    Severity: 4.3 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-7803 – Descreekert wx-discuz Cross-Site Scripting Vulnerability

    July 18, 2025

    Critical Golden dMSA Attack in Windows Server 2025 Enables Cross-Domain Attacks and Persistent Access

    July 16, 2025

    CVE-2025-38155 – “Qualcomm Atheros mt76 Wireless Null Pointer Dereference Vulnerability”

    July 3, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.