What are Terraform Modules?
Terraform modules are a way to organize and reuse Terraform configurations. They allow you to group multiple resources together and can be used to encapsulate complex infrastructure patterns. Modules can be shared and reused across different projects and teams.
Benefits of Using Modules
- Reusability: Write the configuration once and reuse it across different projects.
- Organization: Keep your configurations clean and organized.
- Abstraction: Simplify complex configurations by encapsulating details inside modules.
Directory Structure
The image shows a sample directory structure for Terraform modules:
Dir (Root Module)
├── main.tf
├── ec2
│ └── main.tf
├── s3
│ └── main.tf
└── vpc (community)
└── main.tf
- Root Module: The root module is the directory that contains your main configuration files (e.g.,
main.tf
). This is where you call other modules. - Submodules: Subdirectories within the root module directory, such as
ec2
,s3
, andvpc
, each containing their ownmain.tf
file.
Example of Using Modules
Let’s create a simple example where we have a root module that uses submodules for creating EC2 instances and S3 buckets.
Root Module (main.tf
)
The root module will call the ec2
and s3
modules.
# main.tf in the root module
module "ec2_instance" {
source = "./ec2"
}
module "s3_bucket" {
source = "./s3"
}
EC2 Module (ec2/main.tf
)
The ec2
module will create EC2 instances.
resource "aws_instance" "web" {
ami = "ami-04b70fa74e45c3917"
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
S3 Module (s3/main.tf
)
The s3
module will create an S3 bucket.
resource "aws_s3_bucket" "example" {
bucket = "my-af-test-bucket09887df"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
Using the Modules
Initialize Terraform:
terraform init
Plan the Infrastructure:
terraform plan
Apply the Configuration:
terraform apply
Summary
- Modules in Terraform: Modules allow you to group and reuse configurations.
- Root Module: The main configuration that calls submodules.
- Submodules: Encapsulated configurations within subdirectories.
- Example: The root module (
main.tf
) calls theec2
ands3
modules to create EC2 instances and S3 buckets, respectively.
Example 2 : update your directory structure to include variable files:
root-module/
├── main.tf
├── variables.tf
├── terraform.tfvars
└── instances/
├── main.tf
├── variables.tf
└── terraform.tfvars
Root Module
1. main.tf
# root-module/main.tf
provider "aws" {
region = "us-west-2"
}
module "instances" {
source = "./instances"
instance_count = var.instance_count
instance_name = var.instance_name
security_groups = var.security_groups
instance_tags = var.instance_tags
create_vm = var.create_vm
}
2. variables.tf
# root-module/variables.tf
variable "instance_count" {
description = "Number of instances to launch"
type = number
default = 1
}
variable "instance_name" {
description = "The name of the instance"
type = string
default = "Rajesh"
}
variable "security_groups" {
description = "List of security group IDs"
type = list(string)
default = ["sg-0541801a7a059ba17"]
}
variable "instance_tags" {
description = "Map of tags for the instance"
type = map(string)
default = {
Name = "my-instance4mMap-Rajesh"
}
}
variable "create_vm" {
description = "If set to true, it will create VM"
type = bool
default = true
}
3. terraform.tfvars
# root-module/terraform.tfvars
instance_count = 1
instance_name = "Rajesh"
security_groups = ["sg-0541801a7a059ba17"]
instance_tags = {
Name = "my-instance4mMap-Rajesh"
}
create_vm = true
Instances Module
1. instances/main.tf
# instances/main.tf
resource "aws_instance" "example-number" {
count = var.instance_count
ami = "ami-04b70fa74e45c3917"
instance_type = "t2.micro"
tags = {
Name = var.instance_name
}
}
resource "aws_instance" "example-list" {
ami = "ami-04b70fa74e45c3917"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}
resource "aws_instance" "example-map" {
ami = "ami-04b70fa74e45c3917"
instance_type = "t2.micro"
tags = var.instance_tags
}
resource "aws_instance" "example-bool" {
count = var.create_vm ? 1 : 0
ami = "ami-04b70fa74e45c3917"
instance_type = "t2.micro"
tags = var.instance_tags
}
2. instances/variables.tf
# instances/variables.tf
variable "instance_count" {
description = "Number of instances to create"
type = number
}
variable "instance_name" {
description = "Name of the instance"
type = string
}
variable "security_groups" {
description = "List of security group IDs"
type = list(string)
}
variable "instance_tags" {
description = "Map of tags for the instance"
type = map(string)
}
variable "create_vm" {
description = "If set to true, it will create VM"
type = bool
}
3. instances/terraform.tfvars
You usually won’t need a terraform.tfvars
file in submodules, as the variables should be passed from the root module. However, you can have a default values file if needed.
Using the Module
To use the module, navigate to the root-module
directory and run the following commands:
terraform init
terraform plan
terraform apply