Terraform Modules
What Is a Terraform Module?
In Terraform, Modules are groups of .tf files that are kept in a different directory from the configuration as a whole. A module’s scope encompasses all of its resources. So, if the user needs information about the resources that a module creates, the module must be explicitly stated. To do this, declare an output on the module that exposes the necessary data and permits references to that output from outside the module.
A typical module would resemble this:
.
├── LICENSE
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
Types of Terraform Modules
With Terraform, you are most likely to come across one of three main categories of modules:
Root Module
Child Module
Published Modules
Benefits of Module:
Organise Configuration: Easier to navigate, understand, and update your configuration
Encapsulate Configuration: Helps prevent unintended consequences
Re-use Configuration: Share modules that you have written with your team or the general public
Consistency: help to provide consistency in your configurations
e.g.for EC2 instance creation by using module
Check status by command tree:
Below file will create two instances by using below configuration file if need more modules it can be declared under module in same file.
Module Composition:
Composition is a collection of infrastructure modules, which can span across several logically separated areas (e.g.., AWS Regions, several AWS accounts). Composition is used to describe the complete infrastructure required for the whole organization or project.
A composition consists of infrastructure modules, which consist of resources modules, which implement individual resources.
Simple infrastructure composition
module "s3_bucket" {
source = "../s3_bucket"
bucket_name = var.bucket_name
acl = "public-read"
}
resource "aws_s3_bucket_policy" "this" {
bucket = module.s3_bucket.this.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["s3:GetObject"]
Effect = "Allow"
Resource = "${module.s3_bucket.this.arn}/*"
Principal = "*"
}
]
})
}
variable "bucket_name" {
description = "The name of the S3 bucket for the static website"
type = string
}
Module Versioning
Terraform modules support versioning, allowing you to track changes to your modules over time and roll back to previous versions if needed. To version your modules, you can use a version control system like Git and tag your module releases with semantic versioning.
To reference a specific version of a module in your Terraform configuration, you can use the version argument in the module block:
- Create a
versions.tf
file in the module directory and specify the version constraints:
# vpc_module/versions.tf
terraform {
required_version = ">= 1.0"
}
module "vpc_module" {
source = "git::https://github.com/example/vpc_module.git"
version = "1.2.0"
}
linkdin:linkedin.com/in/gajanan-barure-7351a4140
Happy Learning :)
Thank you for reading!! Hope you find this helpful.
Terraweekday05#challenge90daysofdevops