DEV Community

Cover image for Modules in Terraform day 6
Avesh
Avesh

Posted on

Modules in Terraform day 6

Working with Terraform Modules

Terraform modules are building blocks that enhance reusability, simplify configuration, and ensure consistency in infrastructure as code (IaC). Whether creating custom modules or leveraging existing ones, understanding how to work effectively with Terraform modules is crucial for efficient infrastructure management. This article explores key aspects of Terraform modules, including module creation, usage, versioning, composition, and the differences between public and private modules, with detailed examples.


Module Creation

Modules encapsulate reusable Terraform configurations, enabling you to define, manage, and share infrastructure components consistently.

Steps to Create a Module:

  1. Define Resources: Create a main.tf file containing the resources.
  2. Parameterize with Variables: Use variables.tf to make the module flexible.
  3. Output Results: Include outputs.tf for returning values from the module.

Scenario:

Creating a module to deploy an AWS S3 bucket:

Module Structure:

modules/
  ├── s3_bucket/
      ├── main.tf
      ├── variables.tf
      └── outputs.tf
Enter fullscreen mode Exit fullscreen mode

main.tf:

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  acl    = var.acl
}
Enter fullscreen mode Exit fullscreen mode

variables.tf:

variable "bucket_name" {}
variable "acl" {
  default = "private"
}
Enter fullscreen mode Exit fullscreen mode

outputs.tf:

output "bucket_arn" {
  value = aws_s3_bucket.bucket.arn
}
Enter fullscreen mode Exit fullscreen mode

Module Usage

Modules are referenced in Terraform configurations to provision infrastructure.

Usage Steps:

  1. Source Declaration: Specify the module path in the configuration.
  2. Pass Inputs: Assign values to module variables.
  3. Retrieve Outputs: Use module outputs in the main configuration.

Scenario:

Using the S3 bucket module in a project:

Main Configuration:

module "s3" {
  source      = "./modules/s3_bucket"
  bucket_name = "example-bucket"
  acl         = "public-read"
}

output "bucket_arn" {
  value = module.s3.bucket_arn
}
Enter fullscreen mode Exit fullscreen mode

Module Versioning

Versioning ensures consistency and avoids breaking changes when modules are updated. Modules stored in versioned repositories like Terraform Registry or Git can specify versions.

Scenario:

Referencing a specific version of a module from Terraform Registry:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.14.0"

  cidr = "10.0.0.0/16"
  azs  = ["us-east-1a", "us-east-1b"]
}
Enter fullscreen mode Exit fullscreen mode

When upgrading versions, use the terraform plan command to preview changes.


Module Composition

Modules can be composed by combining multiple modules to build complex infrastructure setups. This approach promotes modularity and reduces duplication.

Scenario:

Composing modules for a web application:

Structure:

modules/
  ├── network/
  ├── compute/
  └── storage/
Enter fullscreen mode Exit fullscreen mode

Main Configuration:

module "network" {
  source = "./modules/network"
}

module "compute" {
  source = "./modules/compute"
  vpc_id = module.network.vpc_id
}

module "storage" {
  source      = "./modules/storage"
  bucket_name = "app-data"
}
Enter fullscreen mode Exit fullscreen mode

This structure allows the application’s components to be managed independently while being interconnected.


Public vs Private Modules

Public Modules:

  • Hosted on Terraform Registry: Accessible to everyone.
  • Use Cases: Common infrastructure patterns like VPCs, EC2, or S3.
  • Example:
  module "vpc" {
    source  = "terraform-aws-modules/vpc/aws"
    version = "3.14.0"
  }
Enter fullscreen mode Exit fullscreen mode

Private Modules:

  • Stored in Private Repositories: Used within an organization.
  • Use Cases: Organization-specific configurations or sensitive infrastructure setups.
  • Example:
  module "internal_network" {
    source = "git::ssh://git@github.com/org/private-modules.git//network"
  }
Enter fullscreen mode Exit fullscreen mode

Public modules offer ready-to-use solutions, while private modules provide tailored functionality and enhanced security.


Image description

Conclusion

Terraform modules simplify infrastructure management by promoting reuse, consistency, and scalability. Whether creating custom modules, managing versions, or composing them for complex setups, understanding these practices is vital. Balancing public and private modules ensures a secure and efficient infrastructure tailored to your needs.

Top comments (3)

Collapse
 
tythos profile image
Brian Kirkpatrick

Nice walkthrough! One trick I find very useful is, especially when you are using Terraform to moderate an organized orchestration technology like Kubernetes, enforcing an isomorphism between modules/subfolders and namespaces (or namespace-like constructs). It's not necessasry, but it's a useful way to organize around clear dependencies between various applications and other cluster resources.

Collapse
 
i_am_vesh profile image
Avesh

Great thinking I'll look up this 👌

Collapse
 
dexterhale profile image
Dexter Hale

It's always great to learn something new from your posts.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.