Simplifying Terraform Modules: A Hands-On Guide

Simplifying Terraform Modules: A Hands-On Guide

What Are Terraform Modules?

Terraform modules are reusable building blocks that help manage your infrastructure code efficiently. They allow you to encapsulate Terraform configurations into smaller, more manageable units, enabling modular and organized deployment of resources. Modules can be stored locally, in a version control repository, or even in public registries like the Terraform Module Registry.

Why Use Terraform Modules?

Modules help solve several problems:

  1. Reusability: Write the code once, reuse it across multiple projects or environments.

  2. Simplicity: Break down complex infrastructure into manageable pieces.

  3. Maintainability: Centralize resource definitions, making updates easier.

  4. Collaboration: Share common modules across teams to ensure consistency.


Creating an EC2 Instance with Terraform Modules

In this project, I created an EC2 instance using a Terraform module. Here's how I structured the project and executed it.


Step 1: Create the Module Directory

I created a folder named module/ec2_instance to define the reusable module. Inside this folder, I added the following files:

main.tf:

resource "aws_instance" "example" {
    ami = var.ami_value
    instance_type = var.instance_type_value
    subnet_id = var.subnet_id_value
}

This file defines the EC2 instance resource using variables for flexibility.

variables.tf:

variable "ami_value" {
  description = "value for the ami"
}

variable "instance_type_value" {
  description = "value for instance type"
}

variable "subnet_id_value" {
  description = "value for the subnet_id"
}

Here, I declared variables to make the module dynamic and reusable.

outputs.tf:

output "public-ip-address" {
  value = aws_instance.example.public_ip
}

This file outputs the public IP address of the created EC2 instance.


Step 2: Main File to Call the Module

I created another main.tf file outside the module directory to invoke the module and pass the required values:

provider "aws" {
    region = "ap-south-1"
}

module "ec2_instance" {
  source = "./module/ec2_instance"
  ami_value = "ami-0dee22c13ea7a9a67"
  instance_type_value = "t2.micro"
  subnet_id_value = "subnet-0d595fdf7d4273d6d"
}

output "ec2_public_ip" {
  value = module.ec2_instance.public_ip
}

Step 3: Execution

  1. Initialize Terraform: Run terraform init to set up the working directory and download provider plugins.

  2. Plan the Execution: Use terraform plan to preview the changes Terraform will make to your infrastructure.

  3. Apply the Configuration: Run terraform apply to create the EC2 instance.


Key Benefits of Using a Module

  • Consistency: The same module can be used across environments (e.g., dev, staging, production).

  • Efficiency: It saves time by avoiding code duplication.

  • Scalability: Easy to scale infrastructure without rewriting code.


Terraform modules are a great way to keep your infrastructure-as-code organized, reusable, and scalable. This simple hands-on project highlights how modules can streamline resource creation while ensuring flexibility.