Skip to content

Modules

Terraform modules are a powerful feature that allows you to reuse code and create more efficient infrastructure as code. Modules are a collection of resources that are grouped together and can be reused across different projects.

In this section, we will explore the concept of modules and show you how to use internal modules and ones imported from registries!

Section titled “In this section, we will explore the concept of modules and show you how to use internal modules and ones imported from registries!”

Modules can be downloaded or referenced from Terraform Public Registry, Private Registry, Git repositories or relative from within your project.

You should use Terraform modules when you need to organize, reuse, and manage infrastructure code effectively, especially as your infrastructure grows in complexity.

Modules allow you to encapsulate a specific piece of functionality (e.g., creating an EC2 instance, VPC, or S3 bucket) into a reusable block of code, which can then be called multiple times across different environments or projects.

As you will remember from the Project Structure Module - in your Terraform project, the root of your project is itself considered a module. This root module is where you define your main resources, variables, outputs, and other configurations.

So just like when you create your project, when creating a module - you should follow the same standard structure with files like main.tf, variables.tf, and outputs.tf.

Internal modules are modules that are those that defined within your Terraform project. You can create an internal module by creating a new directory that contains the module code.

Here’s an example of how you can use an internal module into your project:

module "ec2_instance" {
source = "./modules/ec2-instance"
instance_type = "t2.micro"
ami = "ami-0c55b159cbfafe1f0"
}

In this example, we define a module called ec2_instance that is sourced from the ./modules/ec2-instance directory. We also pass in two variables instance_type and ami, which are used to create an EC2 instance. The module code is defined in the ./modules/ec2-instance directory, and it contains the Terraform resources and variables required to create an EC2 instance.

External modules are those that are downloaded from external registries, such as the Terraform Module Registry or other third-party registries or storage locations (such as a Git repo). Use external modules to reuse code, version modules and to take advantage of existing Terraform code that has been created by others.

Here’s an example of how you can use an imported module in Terraform:

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
}

In this example, we use an imported module called terraform-aws-modules/vpc/aws from the Terraform Module Registry, and as such, you can specify which version of the module you want to use.

Terraform allows you to leverage modules hosted in Git repositories, providing a powerful way to reuse and share infrastructure configurations. By referencing modules directly from Git, you can easily incorporate community-maintained modules or modules developed within your organization.

To use a module from a Git repository, you need to specify the module source as the Git URL and provide any required authentication details if the repository is private.

module "example" {
source = "git::https://github.com/example-org/example-module.git?ref=v1.2.0"
instance_type = "t2.micro"
ami = "ami-0c55b159cbfafe1f0"
}

Terraform modules are a powerful feature that allows you to reuse code and create more efficient infrastructure as code. You can create internal modules within your Terraform code or use imported modules from external registries.

With modules, you can simplify your code, reduce duplication, and create more modular and scalable infrastructure. By doing so you can increase your productivity and create more robust infrastructure solutions!