How to use modules from the Terraform Registry
The Terraform Registry is a place where one can find different providers and modules (re-usable Terraform configurations) and use them with Terraform. It includes contributors as HashiCorp, third-party vendors and the Terraform community. This registry is aimed at providing plugins for handling various infrastructure API, making available existing modules for swiftly building commonly used infrastructure components and demonstrating ways to develop efficiently written Terraform code.
What will we cover?
In this tutorial, we will see how to use modules from Terraform Registry. We will see this by demonstrating an example of creating an EC2 instance on AWS.
Pre-Flight Check
For this tutorial we need the following prerequisites:
- Terraform to be installed on the local system.
- Basic knowledge of Terraform modules.
- AWS account.
Using Modules from the Terraform Registry
Modules for different providers are maintained by the Terraform registry. Modules can be searched from the registry page. Also note that while making a search only verified modules are listed in search results. These modules are checked by HashiCorp to confirm their compatibility and stability. Unverified modules can also be listed using the search filtering option.
Modules published in Terraform Registry can be referred in a typical Terraform code using the syntax:
<NAMESPACE>/<Name of module>/<Name of Provider>
For e.g., look at the below usage of module from Terraform registry:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
}
The above example uses a VPC module from Terraform registry for AWS. Running the ‘terraform init’ command will retrieve and store modules referred to by a Terraform code.
Terraform Registry has provision for utilizing both public and private modules. The public Terraform Registry openly allows publishing and consuming providers and modules. Private modules can be published using a private registry.
Exploring the Terraform Registry
The module page displays some brief details about the module such as the description of the module, date of publishing, url of the source code, download stats etc. You calso see here various tabs for Readme, Inputs, Outputs, Dependency and Resources. We can refer to these sections for selecting the variables to impart in our code.
There are also examples demonstrating the usage of the module, for example a module for creating an EC2 instance is used in this guide. Before we dive into this example, let us first look inside the source code of this module. Open the source code url. On the github page, there are some folders and some files for this module. For now we are just focusing on the three terraform files: main.tf, outputs.tf and variables.tf. The main.tf file contains many lines describing an EC2 instance. Similarly the variables.tf and outputs.tf have a number of input and output value declarations.
Launching an EC2 instance using a Module
Let us now see how we can use a module from the Terraform registry. We will create an EC2 instance using a module from ‘terraform-aws-modules’.
Step 1. Open up the Terraform Registry webpage and search for aws module for EC2:
Step 2. Click on the ‘terraform-aws-modules/ec2-instance’ search result as shown above. This module is used for launching an AWS EC2 instance in our case.
Now, look at the below configuration file which we have created locally on our system for working with this module. In this file we have included the above module using the ‘source’ argument. We have used only few declarations from the actual module as described here:
$ nano myinstance.tf
provider "aws" {
region = "us-east-1"
}
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 3.0"
name = "single-instance"
ami = "ami-0022f774911c1d690"
instance_type = "t2.micro"
availability_zone ="us-east-1a"
key_name = "Your-Key-pair-name"
monitoring = true
vpc_security_group_ids = ["Security-Group-ID"] putin_khuylo = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
In this configuration, there are two arguments: source and version.
- source: This argument is needed when a Terraform module is used. In the above ‘example’ configuration, Terraform will look into the Terraform registry for the given quoted module address. Local modules, URLs, and other sources can also be used here.
- version: It is an optional argument but it is recommended to use when working with a Terraform module. When included, it loads the specified version of the Terraform module for supported sources. If not used then the latest available module version will be loaded.
To start with, first, initialize the directory containing our local file ‘myinstance.tf’. Run the command:
Now run the ‘terraform plan’ or use the ‘terraform apply’ command to directly put the changes into work:
$ terraform apply
Enter ‘yes’ when prompted. This will start the process of applying the changes.
This time we do not need to explicitly create variables.tf or output.tf files, instead we need to only reference the repository variables we want to use. From the management console, open the EC2 dashboard to check if the instance is created or not:
Concluding up…
In this guide, we have seen how Terraform registry modules can be used. In the same way try to configure a VPC using a Terraform module.
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment