Table of Contents

Working with Terraform modules

Abhilash

Dec 30, 2024

2 min readLast Updated Dec 30, 2024

Terraform is a powerful tool for creating and managing infrastructure as code. One of the key features of Terraform is the ability to use modules to encapsulate and reuse infrastructure configuration. In this blog post, we'll explore how to work with Terraform modules, including how to create and publish your own modules.

First, let's define what a Terraform module is. A module is a collection of Terraform files that define a set of resources. A module can be thought of as a reusable piece of infrastructure configuration that can be easily shared and reused across different projects.

Creating a Terraform module is relatively straightforward. You'll need to create a directory that contains all of the Terraform files that make up the module and then use the module block in your Terraform configuration to reference that directory. For example, let's say you have a module that creates an S3 bucket. Your directory structure might look something like this:

modules/
├─ s3/
│  ├─ s3.tf
│  ├─ vars.tf
│  ├─ output.tf

In your Terraform configuration, you would then reference the modules/s3-bucket directory to use the module:

module "s3-bucket" {
  source         = "./modules/s3"
  name           = "GalleryApp"
  environment    = "Dev"
  s3_bucket_name = "mygalleryapptrt"
}
dev/main.tf

Table of Contents

Let's create a Terraform Module for a sample demo app in which we will deploy the same applications in different environments.

Our terraform module will be related to Three tier Application which consists of a React js Frontend, a Nodejs backend and a database where the database will be in a private subnet and React and Nodejs app in a public subnet

Three Tier Architecture

Here in the above diagram, we are using S3, VPC, EC2 and RDS.
And now we will start creating terraform modules for the above infra.

Assuming you have already gone through the earlier blog i.e Getting started with terraform

Let us start with S3 terraform module.

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

resource "aws_s3_bucket_website_configuration" "react_website" {
  bucket = aws_s3_bucket.react_website.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "404.html"
  }
}
main.tf
  • Create  S3 bucket to host a static website
  • Set index.html as the entry point whenever the website is loaded and 404.html page if something goes wrong
variable "name" {
  description = "the name of your stack, e.g. \"demo\""
}

variable "environment" {
  description = "the name of your environment, e.g. \"prod\""
}

variable "bucket_name" {
  description = "name for the s3 bucket"
}
vars.tf
  • Create variables for terraform stack, here name and environment is used to identify the stack
  • bucket_name is used to create bucket names in the s3
output "react_website_endpoint" {
    value = aws_s3_bucket_website_configuration.react_website.website_endpoint
}
output.tf
  • Terraform outputs are a means of exporting resource information in a structured format. This data can then be utilized to automate the configuration of other components within your infrastructure or as a source of information.

As shown earlier how to import a module created, let's use the same code and create a dev environment.

dev/
├─ main.tf
├─ output.tf
├─ vars.tf
modules/
├─ s3/
│  ├─ main.tf
│  ├─ output.tf
│  ├─ vars.tf
Folder structure

Let's plan and apply terraform s3 module for the DEV environment. once everything runs successfully. you will able to see the output.

Projects Completed till now.

Discover how we can help your business grow.

"Third Rock Techkno's work integrates complex frameworks and features to offer everything researchers need. They are open-minded and worked smoothly with the academic subject matter."

- Dr Daniel T. Michaels, NINS

Related Resources

Our Services

You May Also Like