Day 5: Terraform module
Unlocking Efficiency with Terraform's Modular Approach
Table of contents
- ๐ถ TerraWeek Day 5
- ๐ถ Task 1-a: What are modules in Terraform and why do we need modules in Terraform?
- ๐ถ Task 1-b: What are the benefits of using modules in Terraform?
- ๐ถ Task 2: Create/Define a module in Terraform to encapsulate reusable infrastructure configuration in a modular and scalable manner. E.g. AWS EC2 Instance.
- ๐ถ Task 3: Dig into modular composition and module versioning.
- <mark>Modular Composition:</mark>
- <mark>Module Versioning:</mark>
- ๐ถ Task 4: What are the ways to lock Terraform module versions? Explain with code snippets.
๐ถ TerraWeek Day 5
๐ถ Task 1-a: What are modules in Terraform and why do we need modules in Terraform?
Terraform modules are reusable units of Terraform configuration that allow you to encapsulate and organize your infrastructure code into manageable and shareable components.
We need modules in Terraform for the following key reasons:
Modularity: Modules help break down infrastructure code into smaller, manageable pieces.
Reusability: They enable the reuse of infrastructure components in multiple projects.
Abstraction: Modules abstract complex configurations, simplifying their usage.
Parameterization: Modules can be customized using variables, reducing code repetition.
Encapsulation: They hide implementation details, improving code readability.
Versioning: Modules can be versioned and shared via registries for consistency.
Scalability: They aid in managing complexity as infrastructure grows.
Maintainability: Changes to modules can be propagated for consistent updates.
Collaboration: Modules support collaboration among teams or individuals.
Testing: They can be tested in isolation to catch issues early.
Documentation: Modules can include documentation for easy understanding.
Code Review: Smaller modules make code reviews more focused and manageable.
Security: Modules facilitate the consistent application of security practices.
Overall, modules enhance the organization, reusability, and maintainability of Terraform infrastructure code.
๐ถ Task 1-b: What are the benefits of using modules in Terraform?
Modules in Terraform serve several important purposes:
Reusability: Modules allow you to write infrastructure code once and reuse it across different parts of your project or in multiple projects. This promotes code reuse and reduces duplication.
Abstraction: Modules provide an abstraction layer, allowing you to hide complex infrastructure details behind a simplified interface. This abstraction makes it easier to consume and work with infrastructure code.
Organization: Modules help organize your Terraform codebase into logical, self-contained components. This modular structure improves code maintainability and readability.
Collaboration: Modules can be shared within your organization or with the broader Terraform community. This facilitates collaboration by enabling others to leverage your infrastructure code.
Versioning: Modules can be versioned, ensuring that changes are deliberate and can be tracked over time. This helps in managing infrastructure updates and rollbacks.
Parameterization: Modules can accept input variables, allowing you to customize their behavior without modifying their underlying code. This parameterization makes modules flexible and adaptable to different use cases.
Testing: Modules can be individually tested and validated, which contributes to the reliability and quality of your infrastructure code.
Isolation: Modules provide a level of isolation, preventing unintended side effects when changes are made. This isolation ensures that modifications to one module don't impact others.
Scalability: As your infrastructure grows, modules help you maintain a structured and scalable codebase. You can add or update modules as needed to accommodate changes.
Terraform modules are a fundamental concept that promotes code reuse, abstraction, organization, collaboration, versioning, parameterization, testing, isolation, and scalability in your infrastructure as code projects. They are essential for efficient and maintainable infrastructure provisioning and management.
๐ถ Task 2: Create/Define a module in Terraform to encapsulate reusable infrastructure configuration in a modular and scalable manner. E.g. AWS EC2 Instance.
Create all the files
dynamodb.tf
ec2.tf
providers.tf
s3.tf
terraform.tf
variables.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1" # Replace with your desired AWS region
}
resource "aws_instance" "terra_instance" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}
resource "aws_s3_bucket" "my-terra-bucket" {
bucket = var.bucket_name
}
resource "aws_dynamodb_table" "my-terra-table" {
name = var.table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "d5terraID"
attribute {
name = "d5terraID"
type = "S"
}
}
variable "ami_id" {
default = "ami-053b0d53c279acc90"
}
variable "instance_type" {
default = "t2.micro"
}
variable "instance_name" {
default = "terraweek-d5-instance"
}
variable "bucket_name" {
default = "terraweek-d5-bucket"
}
variable "table_name" {
default = "terraweek-d5-table"
}
After terraform apply.
Now Create a directory for modules and copy all the required templates.
Now we have created terra-app template modules.
#variable.tf
variable "ami_id" {
description = "This is AMI ID based on modules"
type = string
}
variable "instance_type" {
description = "This is instance based on env"
type = string
}
variable "instance_name" {
type = string
}
variable "bucket_name" {
type = string
}
variable "table_name" {
type = string
}
variable "env_name" {
type = string
}
#s3.tf
resource "aws_s3_bucket" "my-terra-bucket" {
bucket = "${var.env_name}-${var.bucket_name}"
}
#ec2.tf
resource "aws_instance" "terra_instance" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "${var.env_name}-${var.instance_name}"
}
}
#dynamodb.tf
resource "aws_dynamodb_table" "my-terra-table" {
name = "${var.env_name}-${var.table_name}"
billing_mode = "PAY_PER_REQUEST"
hash_key = "d5terraID"
attribute {
name = "d5terraID"
type = "S"
}
}
Module is Block which allows you to use templates to create a new infrastructure.
Define the EC2 instance configuration in the main.tf:
#main.tf
# Dev
module "dev-terra-app" {
source = "./modules/terra-app"
env_name = "dev"
instance_type = "t2.micro"
ami_id = "ami-053b0d53c279acc90"
instance_name = "terraweek-d5-instance"
bucket_name = "terraweek-d5-bucket"
table_name = "terraweek-d5-table"
}
#QA
module "qa-terra-app" {
source = "./modules/terra-app"
env_name = "qa"
instance_type = "t2.small"
ami_id = "ami-01c647eace872fc02"
instance_name = "terraweek-d5-instance"
bucket_name = "terraweek-d5-bucket"
table_name = "terraweek-d5-table"
}
#Production
module "prd-terra-app" {
source = "./modules/terra-app"
env_name = "prd"
instance_type = "t2.micro"
ami_id = "ami-026ebd4cfe2c043b2"
instance_name = "terraweek-d5-instance"
bucket_name = "terraweek-d5-bucket"
table_name = "terraweek-d5-table"
}
Run terraform apply:
All instances are created with module templates.
All S3 buckets will be created.
All DynamoDB tables are created.
๐ถ Task 3: Dig into modular composition and module versioning.
Modular composition and module versioning are essential aspects of Terraform that help you manage infrastructure as code (IaC) projects efficiently, promote reusability, and ensure consistency across different environments and teams.
Modular Composition:
Modular composition in Terraform refers to the practice of building complex infrastructure by combining smaller, reusable modules.
Module Versioning:
Module versioning is the practice of assigning unique versions to your Terraform modules. This is crucial for maintaining stability and ensuring that changes to modules do not unintentionally break existing infrastructure.
๐ถ Task 4: What are the ways to lock Terraform module versions? Explain with code snippets.
Locking Terraform module versions is important to ensure that your infrastructure remains stable and doesn't break due to unexpected changes in module versions.
When referencing a module in your Terraform configuration, we can specify a version constraint using the version argument. This allows us to ensure that only compatible module versions are used.
You can create a .tf file dedicated to module versions and constraints. For example, you can create a file named module-versions.tf
and define module versions and constraints there
#module-version.tf
module "CKP-app" {
source = "hashicorp/example/aws"
version = ">= 1.0, < 2.0"
# Version constraint, any version in the 1.x range
}
In this example, Terraform will only use module versions that are greater than or equal to 1.0.0 but less than 2.0.0.
In conclusion, Terraform modules are a powerful and essential feature for managing infrastructure as code (IaC) effectively. They offer a way to encapsulate, reuse, and share infrastructure configurations in a modular and scalable manner. Here's a summary of the key points regarding Terraform modules:
#Terraweek Repo: https://github.com/Chandreshpatle28/TerraWeek.git
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform