Creating a Reusable Terraform Module to Launch EC2 Ubuntu
Instances
Terraform is an Infrastructure as Code (IaC) tool that
enables you to define cloud resources in a declarative configuration. Instead
of creating multiple separate Terraform configurations, we can build reusable
modules to simplify resource creation, especially for recurring tasks like
launching EC2 instances.
In this tutorial, we’ll create a reusable module to launch
EC2 instances running Ubuntu. This module will be reusable, meaning you can
easily use it in different projects or environments by providing different
variable values.
What You’ll Learn
- What
is a Terraform Module?
- How
to Create a Reusable Module to Launch EC2 Instances
- How
to Use the Module with Different Configurations
- Step-by-Step
Instructions for Beginners
1. What is a Terraform Module?
A Terraform module is a container for multiple resources
that can be reused across projects. For example:
- Instead
of writing the code to launch an EC2 instance every time, you can write it
once in a module.
- The
module allows you to pass variable values (like instance type, subnet,
etc.) to customize the resource.
Benefits of Modules:
- Reusability:
Write once, use everywhere.
- Maintainability:
Centralize resource definitions.
- Consistency:
Avoid errors by using pre-tested code.
2. Project Structure for the EC2 Module
Here is how to organize your files:
terraform/
├──
ec2_module/ # The reusable
module
│
├──
main.tf # Contains resource
definitions
│
├──
variables.tf # Defines variables
for customization
│
├──
outputs.tf # Exports information
like instance ID
├──
main.tf # Root module to
use the reusable module
├──
terraform.tfvars # Provide values
for the variables
3. Writing the Reusable Module
Step 1: Create the Module Directory
In the terraform directory, create a folder called ec2_module.
Step 2: Define the Resources in the Module
ec2_module/main.tf
This file contains the definition for the EC2 instance.
resource
"aws_instance" "ubuntu_instance" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
key_name = var.key_name
iam_instance_profile = var.iam_instance_profile
security_groups = [var.security_group]
associate_public_ip_address =
var.associate_public_ip
tags = {
Name = var.instance_name
}
root_block_device {
volume_size = var.volume_size
volume_type = var.volume_type
}
}
Step 3: Define Variables for Flexibility
ec2_module/variables.tf
This file allows you to define input variables for the
module. Users can pass different values when using the module.
variable
"ami_id" {
description = "AMI ID for the Ubuntu
instance"
type
= string
}
variable
"instance_type" {
description = "EC2 instance type"
type
= string
}
variable
"subnet_id" {
description = "Subnet ID where the
instance will be launched"
type
= string
}
variable
"key_name" {
description = "Key pair for SSH
access"
type
= string
}
variable
"iam_instance_profile" {
description = "IAM instance profile
name"
type
= string
}
variable
"security_group" {
description = "Security group ID"
type
= string
}
variable
"instance_name" {
description = "Name of the
instance"
type
= string
}
variable
"volume_size" {
description = "Root volume size in
GB"
type
= number
default
= 8
}
variable
"volume_type" {
description = "Root volume type"
type
= string
default
= "gp2"
}
variable
"associate_public_ip" {
description = "Whether to associate a
public IP address with the instance"
type
= bool
default
= true
}
Step 4: Define Outputs
Outputs allow you to get useful information like the
instance ID or public IP after creating the EC2 instance.
ec2_module/outputs.tf
output
"instance_id" {
description = "The ID of the created EC2
instance"
value
= aws_instance.ubuntu_instance.id
}
output
"public_ip" {
description = "The public IP of the EC2
instance"
value
= aws_instance.ubuntu_instance.public_ip
}
output
"private_ip" {
description = "The private IP of the EC2
instance"
value
= aws_instance.ubuntu_instance.private_ip
}
4. Using the Module
Step 1: Define the Root Module
In the root directory, create a main.tf file to use the
reusable ec2_module.
main.tf
provider "aws"
{
region = "ap-south-1"
}
module
"ubuntu_instance" {
source = "./ec2_module"
ami_id =
"ami-0a91cd140a1fc148a" # Replace with the latest Ubuntu AMI ID
instance_type = "t2.micro"
subnet_id =
"subnet-0fca57504fda7891b" # Replace with your subnet ID
key_name = "your-key-pair-name"
iam_instance_profile =
"your-iam-role-name"
security_group = "your-security-group-id"
instance_name = "MyUbuntuInstance"
volume_size = 16
volume_type = "gp2"
}
Step 2: Provide Variable Values
terraform.tfvars
Create a terraform.tfvars file to store variable values:
key_name = "your-key-pair-name"
subnet_id = "subnet-0fca57504fda7891b"
ami_id = "ami-0a91cd140a1fc148a"
security_group =
"sg-xxxxxxxxxxxxxx"
5. Running the Terraform Configuration
Step 1: Initialize Terraform
Run the following command to initialize the project:
terraform init
Step 2: Plan the Resources
Review what Terraform will create:
terraform plan
Step 3: Apply the Configuration
Launch the EC2 instance:
terraform apply
6. Reusing the Module
To launch another EC2 instance, simply add another module
block in the main.tf file with different values:
module
"another_instance" {
source = "./ec2_module"
ami_id =
"ami-0a91cd140a1fc148a"
instance_type = "t2.small"
subnet_id = "subnet-0xxxxxxx"
key_name = "your-key-pair-name"
iam_instance_profile =
"another-iam-role"
security_group = "another-security-group"
instance_name = "AnotherUbuntuInstance"
volume_size = 20
volume_type = "gp2"
}
7. Conclusion
This guide walks you through creating a reusable Terraform
module to simplify the process of provisioning EC2 Ubuntu instances. By using
modules, you ensure consistency, reusability, and easier maintenance across
your infrastructure. Try it out, and let me know if you have any questions or
feedback!
0 Comments