Terraform Module: Day 2

Terraform Modules: Streamlining Infrastructure Management

Terraform Module: Day 2

Introduction

Terraform modules have the potential for reusability. They contain a set of related resources, configurations, and logic into a reusable and shareable unit. Modules enable you to create shareable and organized infrastructure components.

Example

Consider a project operating under a monolithic architecture, where the entirety of the code is contained within a single application. In this scenario, making adjustments or testing individual parts of the code becomes cumbersome. Similarly, if all Terraform code were consolidated into a single application, modifying specific components would pose a challenge. For instance, altering an EC2 instance would necessitate combing through the entire codebase to locate and update the relevant sections. However, Terraform's module concept offers a solution by breaking down the code into distinct, reusable blocks. This division allows for increased reusability and modularity, streamlining the process of creating and modifying infrastructure configurations.

Separate modules of Terraform can be used to create one Terraform file.

Advantages

Demo of Modules

How to create S3_bucket module

Today, we will set up a module for S3 bucket on AWS instance.

  1. Create a folder called modules.

  2. Inside modules folder, create s3_bucket folder.

  3. Create these files inside the folder: main.tf, terraform.tfvars, variables.tf, output.tf, provider.tf.

Lets add some code in your .tf files and set up the module.

# main.tf

# Define the S3 bucket module
resource "aws_s3_bucket" "s3_bucket" {
  bucket = var.bucket_name

  tags = {
    Name = var.bucket_name
  }
}
# terraform.tfvars

# Define input variable values for the S3 bucket module
bucket_name = "my-s3-bucket"  # Specify the desired S3 bucket name
# variables.tf

# Define input variables for the S3 bucket module
variable "bucket_name" {
  description = "The name of the S3 bucket to create"
}
# output.tf

# Output the S3 bucket name
output "s3_bucket_name" {
  value = aws_s3_bucket.s3_bucket.bucket
}
# provider.tf

#Defines the provider for S3 module
provider "aws" {
    region = "us-east-1"
}

Using all the files above inside module/s3_bucket folder, S3_bucket module is created. This module can be utilized just by creating a new main.tf files as per the requirements in below format. Just be creating main.tf as per requirements, above s3_module can reutilized and modularized.

# main.tf

# Initialize the S3 bucket module
module "s3_bucket" {

  source = "./modules/s3_bucket". # location where s3_bucket module is present
  bucket_name = "my-example-bucket".  # replace this with actual bucket name
}

Enter below commands after adding above files:

terraform init

terraform plan

terraform apply

This will automatically create s3 bucket in your AWs account.

Conclusion

In today's blog, we learned about modules, advantages of modules and how by utilizing the S3 bucket module in our Terraform configuration, we've streamlined the creation of S3 buckets. This modular approach enhances code organization and reusability, simplifying infrastructure management. With clear input parameters, our configuration remains flexible, facilitating easy customization for different projects or environments. Through this implementation, Terraform ensures consistent and efficient provisioning of S3 storage resources.

Hope you enjoyed the demo by doing it yourself!