Skip to main content

Command Palette

Search for a command to run...

Day70 ---> 90DaysOfDevOps Challenge @TWS

Published
5 min read
Day70 ---> 90DaysOfDevOps Challenge @TWS
C

Aspiring DevOps Engineer, Certified AWS Solutions Architect - Associate

Terraform Modules

  • Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory

  • A module can call other modules, which lets you include the child module's resources in the configuration concisely.

  • Modules can also be called multiple times, either within the same configuration or in separate configurations, allowing resource configurations to be packaged and re-used.

What are the different types of Terraform modules?

Terraform modules are an essential component of Terraform's design that allows you to organize and reuse your infrastructure code. They provide a way to encapsulate and package resources, configurations, and other related components, making it easier to manage complex infrastructure setups and promote code reusability.

Different types of modules available in Terraform:

  1. Root Modules: The root module refers to the main configuration file that serves as the entry point for your Terraform project. It typically contains top-level resources and acts as the foundation for your infrastructure. Root modules can include child modules or directly define resources.
module "networking" {
  source = "./modules/networking"
}

module "webserver" {
  source = "./modules/webserver"
  vpc_id = module.networking.vpc_id
}
  1. Child Modules: Child modules are modules called and used within other modules or the root module. They allow you to encapsulate specific configurations or sets of resources, promoting modularity and code organization.
resource "aws_vpc" "main" {
  # ...
}

output "vpc_id" {
  value = aws_vpc.main.id 
}
  1. Provider Modules: Provider modules are specialized modules that focus on configuring and managing specific infrastructure providers, such as AWS, Azure, Google Cloud, or Docker. They abstract the complexities of provider-specific APIs and enable consistent management of resources across different providers.
provider "aws" {
  region = "us-east-1"
}

module "ec2" {
  source = "github.com/terraform-aws-modules/terraform-aws-ec2-instance"
}
  1. Community Modules: Community modules are pre-built modules created by the Terraform community and shared through platforms like the Terraform Registry, GitHub, or other online repositories.
module "s3_bucket" {
  source = "terraform-aws-modules/s3-bucket/aws"
  # ... 
}
  1. Composite Modules: Composite modules, also known as composition modules, are modules that are built by combining and reusing other modules. They provide a way to compose complex infrastructure setups by assembling and configuring multiple modules together.
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
}

module "ec2" {
  source = "terraform-aws-modules/ec2-instance/aws"

  vpc_security_group_ids = [module.vpc.default_security_group_id]
}
  1. Data Modules: Data modules in Terraform are used to retrieve and access external data or resources that are not directly managed by Terraform itself. These modules facilitate integration with external systems, such as querying APIs, fetching data from databases, or reading from external configuration files. Data modules enable Terraform to interact with external information dynamically during the planning and execution phases.
data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] 
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  # ...
}

Remember to leverage community modules and the Terraform Registry to benefit from pre-built modules and collaborate with the wider Terraform community.

Difference between Root Module and Child Module.

Root ModuleChild Module
DefinitionThe main configuration file acts as the entry point for your Terraform code.A module that is imported and used within another module (the root module or another child module).
ContainsTop-level resources and configuration.A specific set of resources or configurations to promote modularity and reusability.
ImportsChild modulesN/A
FilenameUsually main.tf or root.tfHas variables.tf, outputs.tf and main.tf files
HierarchyTop of the module hierarchyNested below the root module
RoleDefines the overall infrastructureImplements reusable functionality
Isolated ChangesChanges to a child module do not affect the root moduleChanges to the root module may affect child modules

Example:

main.tf (root module)

module "networking" {
  source = "./modules/networking"
}

module "webserver" {   
  source = "./modules/webserver"
  vpc_id = module.networking.vpc_id
}

main.tf (networking module)(child module)

resource "aws_vpc" "main" {
  # ... 
}

Are modules and Namespaces are same? Justify your answer for both Yes/No

Modules vs Namespaces

ModulesNamespaces
Physical vs LogicalPhysical groups of codeLogical groupings of code
File StructureHave their own filesCode grouped using declarations
ChangesChanges are isolatedChanges can affect other code
ReusabilityAllow reusing code by importingAllow reusing names within the codebase
Method of code storagephysical isolation and encapsulationlogical grouping within the same codebase
EncapsulationEncapsulate related codeGroup-related code logically

Modules and namespaces are different concepts. Modules are actual reusable code units, while namespaces are logical groupings of code within the same codebase.

Let's use the module to create resources on the AWS environment--->

ec2_module/main.tf

# Create an AWS EC2 Instance

resource "aws_instance" "server-instance" {
  count = var.number_of_instances

  ami                    = var.ami
  instance_type          = var.instance_type        
  subnet_id              = var.subnet_id
  vpc_security_group_ids = var.security_group       

  tags = {
    Name = "${var.instance_name}-${count.index + 1}"
  }
}

output "instance_id" {
  description = "Instance ID"
  value       = aws_instance.server-instance[*].id
}

ec2_module/variables.tf

# Server Module Variables
variable "number_of_instances" {
  description = "Number of Instances to Create"
  type        = number
  default     = 1
}

variable "instance_name" {
  description = "Instance Name"
  type        = string
  default     = "My_Ec2_Test"
}

variable "ami" {
  description = "AMI ID"
  type        = string
  default     = "ami-053b0d53c279acc90"
}

variable "instance_type" {
  description = "Instance Type"
  type        = string
  default     = "t2.micro"
}

variable "subnet_id" {
  description = "Subnet ID"
  type        = string
  default     = "subnet-0fb1b21f41c1408e2"
}

variable "security_group" {
  description = "Security Group"
  type        = list(string)
  default     = ["sg-0277e2f4d375c8965"]
}

main.tf

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

module "launch_ec2_instance" {
  source = "./ec2_module"
}

output "instance_id" {
  description = "Instance ID"
  value       = module.launch_ec2_instance.instance_id       
}

image

image

image

image

image

image

image

Happy learning :)

Day 70 task is complete!

90DaysOfDevOps Tasks👇

github.com/Chaitannyaa/90DaysOfDevOps.git

Chaitannyaa Gaikwad | LinkedIn

More from this blog

90DaysOfDevOpsChallenge-team

81 posts