Skip to main content
A module in OpenTofu (a Terraform fork) is any directory that contains configuration files. When you run OpenTofu commands inside that directory, it becomes the root module, orchestrating resources defined within.

Root Module Example

Suppose your workspace looks like this:
main.tf
variables.tf
Running tofu init, tofu plan, or tofu apply inside aws-instance treats it as the root module.

Calling Child Modules

To avoid duplicating infrastructure code, package a directory as a child module and invoke it:
Create a main.tf in development:
  • module "dev-webserver" assigns a logical name.
  • source = "../aws-instance" points to the child module’s path.
Now development is the root module, calling the ../aws-instance child module.

Building a Reusable Payroll App Module

FlexIT Consulting needs the same payroll stack in multiple regions. The architecture uses:
  • One EC2 instance (custom AMI)
  • One DynamoDB table
  • One S3 bucket
All resources live in the default VPC:
The image is a diagram of a simplified AWS architecture for FlexIT Consulting's payroll software, showing components like an AWS instance, S3 bucket, and DynamoDB table within a default VPC. It highlights aspects such as no IAM role considerations and default VPC and subnet usage.

Define the Module

Organize reusable code under modules/payroll-app:

app_server.tf

s3_bucket.tf

dynamodb_table.tf

variables.tf

  • Hardcoded: instance type, DynamoDB table name, and hash key.
  • Configurable: AMI, region, bucket via variables.

Deploy in US East (us-east-1)

Create a root module for the US deployment:
provider.tf
main.tf
Initialize and apply:
You’ll see:
The S3 bucket name combines the region prefix with the default bucket variable.

Deploy in London (eu-west-2)

Repeat for the UK region:
provider.tf
main.tf
Resources provisioned under:

OpenTofu can source community or verified modules from the registry, just like Terraform. For example, to provision a security group:
The image shows a search interface from the OpenTofu Registry, displaying results for "security-group" modules, including details about a Terraform module for creating EC2-VPC security groups on AWS.
Always pin the version to prevent unexpected module changes. Use tofu get or tofu init to fetch registry modules.

The image is an infographic titled "OpenTofu Module" highlighting the benefits of using modules, including simpler configuration files, lower risk, and reusability.

Watch Video