What is Terraform Modules? Explained with Examples

What is Terraform Modules? Explained with Examples

This post will help you to understand what is terraform modules and explained terraform modules with examples. In the previous posts, we have covered the below topics to create AWS VPC using Terraform Resources. So we can create such infrastructure using terraform resources or terraform modules based on our tasks.

What is Terraform Modules?

Terraform modules are reusable units that are very well organized collections of Terraform configurations with infrastructure resources. They really help us to manage any complex infrastructure by grouping related resources, allowing for scalability and maintainence easily. By using modules, we can simplify our code, promote best practices and share the configurations across any different projects. Think of them like building blocks for your cloud infrastructure.

Lets take an example of creating AWS VPC using Terraform Modules.

In the last post, we have created AWS VPC using individual terraform resources. For creating such AWS VPC, We identified around 9 components form VPC architecture. Also we created 1 security group and deployed 2 AWS EC2 on Public Subnet and Private Subnet to test it.

1. AWS VPC (Virtual Private Cloud)
2. AWS Internet Gateway (IGW)
3. AWS Public Subnet 1
4. AWS Private Subnet 1
5. AWS Public Route table 1
6. AWS Private Route table 1
7. AWS Public Route table 1 association with Public Subnet 1
8. AWS Private Route table 1 association with Private Subnet 1
9. AWS Security Group 1
10. AWS EC2 - Web1 (Public Subnet 1)
11. AWS EC2 - DB1 (Private Subnet 1)

For all these list of components, We have found respective terraform resources from terraform registry and created resource section on vpc.tf. You can refer the code here.

If we use terraform modules for the same AWS VPC architecture. we can create the same setup with less than 10 lines.

You can find all terraform modules from here -  https://registry.terraform.io/browse/modules     

We need to identify the correct terraform modules that will suit for our tasks. So I will be using this terraform provided aws modules called vpc - https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest 

Here is my vpc.tf where i have copied examples from terraform modules and updated as per my requirement.

1. VPC
2. 2 Public Subnets
3. 2 Private Subnets
4. Security group
5. AWS EC2

With less number of lines in code, it creates a entire AWS VPC. Here you go.

vpc.tf


provider "aws" {
  region = "us-east-1"
}

module "vpc1" {
  source = "terraform-aws-modules/vpc/aws"

  name = "flipkart-vpc-dev"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1a", "us-east-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  tags = {
    Terraform   = "true"
    Environment = "dev"
  }
}

resource "aws_security_group" "sg1" {
  name = "flipkart-vpc-sg1"
  #   description = "Allow TLS inbound traffic and all outbound traffic"
  vpc_id = module.vpc1.vpc_id

  ingress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["115.99.14.198/32"]

  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]

  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name       = "flipkart-sg1"
    Managed_by = "terraform"
  }
}

resource "aws_instance" "web1" {
  ami                         = "ami-0e86e20dae9224db8"
  instance_type               = "t2.micro"
  subnet_id                   = module.vpc1.public_subnets[0]
  key_name                    = "demov1"
  associate_public_ip_address = "true"
  vpc_security_group_ids      = [aws_security_group.sg1.id]

  tags = {
    Name = "Web1"
  }
}

output "my_web1_public_ip" {
  value = aws_instance.web1.public_ip

}

output "my_web1_private_ip" {
  value = aws_instance.web1.private_ip

}



After making all required changes to vpc.tf manifest files. we have to initialize the directory and apply it.
terraform init
terraform apply --auto-approve

Simply by copying the same modules multiple times, we can create multiple AWS VPC easily for different purposes.

That's it for this post. Keep practicing and have fun. Leave your comments if any.

Post a Comment

0 Comments