Making use of Terraform modules

2 minute read

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.

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.

Updated: