28 August 2023
Terraform modules are one of the key features that make Terraform a powerful infrastructure as code tool. Modules allow you to encapsulate reusable Terraform configurations and share them. This enables implementing infrastructure patterns for composability and reusability.
In this beginner's guide, we'll cover the basics of Terraform modules including when and how to use them. We'll look at creating, consuming, and publishing modules to simplify infrastructure provisioning.
This material can be form on our Terraform Training Course. The perfect solution whether you are considering training as an individual or as a team.
Terraform modules are self-contained packages of Terraform configurations that define a set of infrastructure resources.
Modules encapsulate common infrastructure patterns like networks, load balancers, and databases so they can be reused. Modules expose:
Using modules makes it easier to reuse and share Terraform code. Teams can collaborate around centralized modules for consistency and compliance.
Some examples of common infrastructure that can be modularized:
Almost any Terraform configuration can be packaged as a module for reuse.
Terraform modules provide these key advantages:
Using modules makes Terraform configurations:
Overall, modules help teams scale Terraform usage by making infrastructure patterns reusable, encapsulated, and composable.
Terraform modules are created by encapsulating related infrastructure resources and exposing input and output variables.
For example, we can create a reusable module for an AWS VPC network:
# vpc.tf
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
# Other VPC properties
}
output "vpc_id" {
value = aws_vpc.main.id
}
This defines a module that creates a VPC and exposes the ID as an output.
To use variables and outputs, modules behave much like any other Terraform configuration. Input variables parameterize the module behavior while output values expose information other resources need to reference the module.
Some best practices for module development:
With a module defined, let's look at how to use modules.
To use a Terraform module, declare a module
block referencing the source location and pass in values for required input variables:
# modules.tf
module "vpc" {
source = "./vpc"
vpc_cidr = "10.123.0.0/16"
}
This consumes the VPC module by setting the input variable and making the output values available.
Additional practices when using modules:
terraform init
terraform plan
to verify changes from modulesModules enable reusable infrastructure patterns and simplify configurations by encapsulating complexity.
After creating useful Terraform modules, they can be shared and distributed easily:
Publishing modules enables discovering, using, and collaborating around infrastructure patterns. Some tips:
v1.0.0
Let's look at using published modules from the public Terraform Registry.
The Terraform Registry provides a discovery and collaboration platform for modules. It hosts thousands of reusable modules contributed by the community.
Using registry modules is easy. Reference the module source using the registry hostname:
module "aws_vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.19.0"
# ...
}
This pulls the module directly from the registry instead of locally.
Some key benefits of leveraging registry modules:
Publishing and consuming modules from the public Terraform Registry helps teams share infrastructure patterns and accelerate provisioning.
To demonstrate creating and using a real-world module, we'll walk through packaging a customizable VPC network module.
Our module will create an AWS VPC with the following:
First, we encapsulate the VPC and related resources into a Terraform configuration module:
# vpc.tf
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
# Other properties
}
# Public subnets
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
# ...
}
# Private subnets
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
#...
}
# Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
#...
}
# Route tables
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
# ...
}
# Outputs
output "vpc_id" {
value = aws_vpc.main.id
}
This module creates the VPC, subnets, internet gateway and route tables.
We can now consume the module from a root Terraform configuration:
module "vpc" {
source = "./vpc"
vpc_cidr = "10.123.0.0/16"
}
output "vpc_id" {
value = module.vpc.vpc_id
}
This declares the module usage, passes a custom VPC CIDR, and exports the VPC ID output.
By encapsulating common infrastructure in modules, we can quickly reuse these patterns for faster and more flexible provisioning. Modules allow composing together infrastructure components in a declarative and reusable way.
Terraform modules are a powerful paradigm for managing infrastructure as code. By encapsulating and parameterizing common infrastructure patterns, modules make configurations:
Modules promote reusable, declarative infrastructure definition. Mastering modules will help you and your team scale Terraform usage for managing infrastructure as code.
In today's dynamic infrastructure environment, Terraform is an invaluable tool for operations teams and developers alike. Its capabilities for declaratively defining, provisioning, and managing infrastructure as code can drive more efficient, collaborative infrastructure management.
Whether you're just starting out with infrastructure-as-code or looking to improve an existing Terraform implementation, our Terraform training courses at JBI Training can help you master this powerful platform. So why wait? Embrace the power of infrastructure-as-code and transform your infrastructure workflows today.
Start your learning journey today with the Terraform courses at JB International and unlock the potential of infrastructure-as-code at your fingertips, including:
Terraform: Learn how to use Terraform to build, change, and version infrastructure safely and efficiently.
DevOps: Gain comprehensive DevOps Skills - with AWS, Docker, Ansible and Terraform
To dive deeper into Terraform modules:
Terraform modules unlock managing infrastructure as composable building blocks. They enable sharing proven infrastructure code to accelerate provisioning across teams and the wider Terraform community.
CONTACT
+44 (0)20 8446 7555
Copyright © 2024 JBI Training. All Rights Reserved.
JB International Training Ltd - Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS
Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us