Writing Terraform Modules
Learn how to work with Terraform modules.
Overview
Terraform modules provide a way to organize our infrastructure code into reusable components. Using a module is important when we repeatedly need to create a resource or when we need to create a resource that’s a combination of a couple of resources. A module is usually defined the same way we define our Terraform configuration files.
Let’s say that we need to create five EC2 instances at the same time or that we need to create a networking component, such as a VPC. We can employ modules for either of these scenarios.
For instance, we can first create a module that creates EC2 instances using the code snippet below:
Press + to interact
# Defining an EC2 instance named "ec2_instance" within our moduleresource "aws_instance" "ec2_instance" {instance_count = var.instance_countinstance_type = var.instance_typeami = var.ami#...other configuration items}
The code snippet above uses three variables named instance_count
...