Making use of Terraform modules
Terraform is a powerful tool for managing infrastructure as code, allowing you to define and deploy your infrastructure in a repeatable and automated way. Terraform modules are a key feature of Terraform that allow you to organise and reuse your infrastructure code across multiple projects. In this blog post, we will discuss the basics of creating Terraform modules and how to use them in your infrastructure projects.
What are Terraform Modules?
Terraform modules are reusable packages of Terraform configuration that can be shared across different projects or used multiple times within the same project. They are used to encapsulate a group of resources that have a similar purpose, such as a group of EC2 instances, a VPC, or a set of IAM policies. Terraform modules allow you to create consistent, repeatable infrastructure across multiple projects, making it easier to maintain and scale your infrastructure code.
Creating a Terraform Module
To create a Terraform module, you need to define a set of resources and their configurations within a directory. The directory should contain a file called “main.tf”, which defines the resources and their configuration, and a file called “variables.tf”, which defines the input variables that can be customised by the user of the module. You can also include other files and directories for more complex modules.
Here’s an example of a simple Terraform module that creates an EC2 instance:
# main.tf
resource "aws_instance" "example" {
ami = var.ami_id
instance_type = var.instance_type
}
# variables.tf
variable "ami_id" {
type = string
}
variable "instance_type" {
type = string
}
In this example, the “aws_instance” resource is defined in “main.tf”, and two input variables are defined in “variables.tf”. The “ami_id” and “instance_type” variables can be customised by the user of the module to create an EC2 instance with a specific AMI and instance type.
Using a Terraform Module
Once you have created a Terraform module, you can use it in your infrastructure projects by referencing the module in your Terraform code. You can reference a module by specifying the path to the module directory, either locally or from a remote source like GitHub or Terraform Registry.
Here’s an example of how to use the module we created above in a Terraform project:
# main.tf
module "ec2_instance" {
source = "./modules/ec2_instance"
ami_id = "ami-1234567890abcdefg"
instance_type = "t2.micro"
}
In this example, we reference the “ec2_instance” module by specifying the local path to the module directory in the “source” parameter. We also specify the values for the input variables “ami_id” and “instance_type”. When we run “terraform apply”, Terraform will create an EC2 instance using the configuration defined in the module.
Using Modules from the Terraform Registry
The Terraform Registry is the official source for publicly available Terraform modules. It provides a central location to discover, use, and share modules for a wide range of providers and use cases. Using modules from the registry is straightforward and requires only the module’s source address.
Example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "4.0.2"
}
Hosting Modules in GitHub Repositories
You can host your Terraform modules in a GitHub repository to make them easily accessible and version-controlled. To use a module from GitHub, specify the repository URL in the source
parameter. You can also reference a specific tag, branch, or commit. This approach allows you to share modules across teams and projects, and to manage updates using Git tags or branches.
Example:
module "ec2_instance" {
source = "git::https://github.com/your-org/terraform-aws-ec2-instance.git?ref=v1.0.0"
ami_id = "ami-1234567890abcdefg"
instance_type = "t2.micro"
}
Note:
If your module is stored in a private GitHub repository, Terraform will need appropriate GitHub credentials to access it.
You can provide credentials by setting the GITHUB_TOKEN
environment variable or by configuring an SSH key that has access to the repository.
For example, you can export a personal access token before running Terraform:
export GITHUB_TOKEN=your_personal_access_token
terraform init
Alternatively, configure SSH access and use an SSH-based source URL:
module "ec2_instance" {
source = "git@github.com:your-org/terraform-aws-ec2-instance.git"
# ...other variables...
}
Hosting Modules in AWS S3 Buckets
Terraform modules can also be stored in AWS S3 buckets, which is useful for sharing modules within your organisation. To use a module from S3, specify the S3 URL in the source
parameter.
Example:
module "ec2_instance" {
source = "s3::https://s3.amazonaws.com/your-bucket/modules/ec2_instance.zip"
ami_id = "ami-1234567890abcdefg"
instance_type = "t2.micro"
}
Make sure the S3 object is a ZIP archive of your module directory. You can control access to the module using S3 bucket policies and IAM permissions.
Note:
When using modules from a private S3 bucket, ensure that your environment is configured with the necessary credentials to access those resources (via environment variables or shared credentials file) and grant access to the bucket and object containing the module.
Conclusion
Terraform modules are a powerful feature of Terraform that allow you to organise and reuse your infrastructure code across multiple projects. Creating a Terraform module is a simple process of defining resources and their configurations within a directory and specifying input variables. Once created, modules can be used in Terraform projects by referencing them in the Terraform code. Using Terraform modules can make your infrastructure code more maintainable, scalable, and reusable, and is a best practice for managing infrastructure as code.
Leave a comment