Learn to leverage Terraform Registry modules for simplified infrastructure provisioning and reusable configurations across projects.
In this lesson, you will learn how to leverage modules from the Terraform Registry to simplify your infrastructure provisioning. Terraform modules allow you to reuse and share configurations across projects, streamlining your workflow. While a local module resides on the same machine as Terraform, modules from the Registry offer the added benefit of being easily shared with the community.
Modules in the Terraform Registry are organized by the provider they support. They come in two categories:
Verified Modules: Tested and maintained by HashiCorp, these modules are marked with a blue tick. For example, AWS security group modules often appear as verified solutions.
Community Modules: Created by users, these modules are not validated by HashiCorp.
When searching for a module—say, to create AWS security groups—you might encounter multiple options. Each module in the Registry includes detailed information such as the publisher, available versions, and helpful usage instructions with examples.
A common use case is to create a security group that permits inbound SSH access. The SSH module requires three mandatory inputs:
The name of the security group.
The VPC in which it will be created.
A list of ingress CIDR blocks allowed access for SSH.
Below is the configuration for the SSH sub-module:
Copy
Ask AI
module "security-group_ssh" { source = "terraform-aws-modules/security-group/aws/modules/ssh" version = "3.16.0" vpc_id = "vpc-7d8d215" ingress_cidr_blocks = [ "10.10.0.0/16" ] name = "ssh-access"}
It is highly recommended to specify the module version to ensure consistency and prevent unintended updates. Omitting the version may cause Terraform to download the latest module revision, potentially introducing changes that could disrupt your environment.
To use a module from the Terraform Registry, start by initializing your Terraform configuration. If the provider plugins are already downloaded, you can simply run the command to fetch the module:
Copy
Ask AI
$ terraform getDownloading terraform-aws-modules/security-group/aws 3.16.0 for security-group_ssh...- security-group_ssh in .terraform/modules/security-group_ssh/modules/ssh
Following the download, create the security group by executing:
This lesson covered how to utilize modules from the Terraform Registry, from local module examples to referencing public modules for building robust and reusable infrastructure components. Practice working with these modules to reinforce your learning and improve your Terraform configurations.For more detailed information, explore these resources: