Table of contents
☑️What are modules in Terraform and why do we need modules in Terraform?
Modules in Terraform: Modules in Terraform are self-contained packages of Terraform configurations that are managed as a group. They are used to organize and reuse Terraform code, making it easier to manage and maintain infrastructure as code (IaC) projects.
Why do we need modules in Terraform?:
Reusability: Modules allow you to write reusable infrastructure components, making it easier to share and reuse code across different projects.
Modularity: Modules promote a modular approach to infrastructure design, where you can break down complex infrastructure into smaller, manageable components.
Abstraction: Modules provide an abstraction layer, hiding the complexity of the underlying infrastructure and making it easier to work with.
Scalability: With modules, you can scale your infrastructure by reusing modules and composing them to create larger, more complex systems.
Consistency: Modules help ensure consistency across your infrastructure, as you can define standard configurations and reuse them across different environments.
☑️Modular Composition and Module Versioning
Modular Composition: Modular composition refers to the practice of combining multiple modules to create complex infrastructure configurations. Terraform allows you to use output values from one module as input variables in another module, enabling you to build modular and scalable infrastructure.
Module Versioning: Module versioning in Terraform allows you to specify the version of a module to use in your configuration. This ensures that your infrastructure remains consistent and reproducible over time, even as the module evolves. Module versions are specified using version constraints, which can be defined in the required_version
field of the root module or in the module
block when calling a module.
☑️What are the ways to lock Terraform module versions?
To lock Terraform module versions, you can:
- Direct version: Specify a fixed version in your
module
block.
module "aws" {
source = "organization/module_name/version"
version = "1.2.3"
}
- Version constraints: Use a range of versions in the
version
argument.
module "aws" {
source = "organization/module_name"
version = "~> 1.2" # Selects version 1.2.x
}
- Operators: Use operators to specify more complex version ranges.
module "aws" {
source = "organization/module_name"
version = ">= 1.2, < 2.0" # Selects versions 1.2.x to 1.9.x
}
- Configuration file: Specify the provider version in the Terraform configuration file.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.62.0"
}
}
}
- Module registry: Use the
registry
argument to specify the module source and version.
module "example" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
}
These methods help keep your Terraform modules consistent across different environments and deployments.
☑️Create/Define a module in Terraform to encapsulate reusable infrastructure configuration in a modular and scalable manner.
resource "aws_instance" "my_instance" {
ami = var.ami
instance_type = var.instance_type
subnet_id = var.subnet_id
}
variable "ami" {
description = "The AMI to use for the EC2 instance"
type = string
}
variable "instance_type" {
description = "The instance type for the EC2 instance"
type = string
}
variable "name" {
description = "The name tag for the EC2 instance"
type = string
}
output "public_ip" {
value = aws_instance.new_instance.public_ip
}
- Use the module in your Terraform configuration:
module "aws_instance" {
source = "./ec2_instance"
ami = "ami-0b8b44ec9a8f90422"
instance_type = "t2.micro"
name = "new_instance"
}
With this module, you can easily create EC2 instances with different configurations by reusing the module across your Terraform configurations.
I'm confident that this blog will prove to be valuable, helping you discover new insights and learn something enriching .🙏
😊Happy Learning : )
Copyright © Pratik R. Mandge, 2024. All rights reserved.
This article and its content are protected by copyright law. Reproduction or distribution of this article without the author's permission is prohibited. For inquiries, please contact pratikmandge021@gmail.com