Infrastructure as Code: An In-Depth Look at Terraform Modules and Versioning

Balkaran Brar
Cloud Prodigy
Published in
2 min readMar 26, 2023

Introduction

Infrastructure as Code (IaC) has become an essential part of modern DevOps practices. By using tools like Terraform, you can manage and automate your cloud infrastructure in a more efficient and scalable way. In this blog post, we will take a closer look at Terraform modules and versioning, two powerful features that can help you streamline your IaC workflows.

Understanding Terraform Modules

A Terraform module is a reusable and shareable unit of Terraform configurations that are organized in a structured way. Modules enable you to encapsulate a set of resources and configurations that can be used across multiple projects or environments.

Using modules in your Terraform configurations helps with:

  • Code reusability
  • Simplifying complex configurations
  • Promoting best practices
  • Enforcing consistency across environments

Creating a Terraform Module

To create a module, start by organizing your Terraform configuration files in a folder. The folder should contain a main.tf, variables.tf, and outputs.tf file, which will define the resources, input variables, and outputs, respectively.

module1/
| — main.tf
| — variables.tf
| — outputs.tf

Example (main.tf):

resource “aws_security_group” “this” {
name = var.security_group_name
description = “Security group example”

ingress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
}
}

Versioning Your Terraform Modulesr

Versioning is crucial for maintaining and evolving your Terraform modules. With proper versioning, you can ensure that different environments or projects can use different versions of a module without causing conflicts.

Using Git for Module Versioning:

One way to version your modules is by using a Git repository. You can tag your module releases with semantic versioning (e.g., v1.0.0, v1.1.0, v2.0.0), making it easier to track changes and maintain compatibility.

Example: Using a specific version of a module in your Terraform configuration:

module “security_group” {
source = “git::https://example.com/terraform-modules/aws-security-group.git?ref=v1.0.0"
security_group_name = “my-security-group”
allowed_cidr_blocks = [“10.0.0.0/8”]
}

Using a Terraform Registry for Module Versioning

Another option for versioning modules is by using a Terraform Registry, such as the public Terraform Registry or a private registry. This provides a centralized location for storing and distributing your modules with versioning support.

Example: Using a specific version of a module from the public Terraform Registry:

module “vpc” {
source = “cloudprodigy/vpc/aws”
version = “2.0.3”
name = “${local.app_name}-${local.environment}”
cidr = lookup(lookup(local.vpc_config, local.environment), “cidr”)
azs = [“${local.region}a”, “${local.region}b”]
private_subnets = lookup(lookup(local.vpc_config, local.environment), “private_subnets”)
public_subnets = lookup(lookup(local.vpc_config, local.environment), “public_subnets”)
tag_application = local.app_name
tag_team = local.team
environment = local.environment
}

Conclusion

Terraform modules and versioning are powerful features that can help you manage and scale your Infrastructure as Code effectively. By creating reusable modules and leveraging versioning best practices, you can ensure consistent and controlled infrastructure deployments.

--

--

Balkaran Brar
Cloud Prodigy

Cloud Architect | DevOps Professional | Kubernetes | Data Analytics