Sure! I'll explain your Terraform code step-by-step in a beginner-friendly way, clarify what each part does, why it is there, and provide some best practices along the way.
What is Terraform?
Terraform is an "Infrastructure as Code" (IaC) tool. It lets you define cloud infrastructure in code files, which you can then apply to create or manage those resources automatically. In your case, you're using Terraform to create AWS infrastructure (VPCs, subnets, EC2 instances, etc.).
Your Project Structure & Files
You have a root Terraform configuration and a module inside a directory:
terraform/
├── main.tf # root Terraform config file
└── module/
└── ec2-instance/
├── main.tf # main module config
├── variable.tf # variables used in the module
└── output.tf # outputs exported by the module
1. Root main.tf
provider "aws" {
region = "us-east-2"
}
module "ec2_instance" {
source = "/home/vagrant/terraform/module/ec2-instance" # points to module directory
ami_value = "ami-0c55b159cbfafe1f0" # AMI (Amazon Machine Image) input to module
instance_type_value = "t2.micro" # EC2 instance type input to module
}
What does this do?
- provider "aws": This tells Terraform to use the AWS provider and deploy infrastructure to AWS in the
us-east-2 (Ohio) region.
- module "ec2_instance": You are calling a module named "ec2_instance" which is defined under
/home/vagrant/terraform/module/ec2-instance.
- The
module block is like a function call: you pass inputs (variables) like the AMI ID and instance type to the module so it can create resources.
2. The ec2-instance module (module/ec2-instance/main.tf)
provider "aws" {
region = "us-east-2"
}
# Create a VPC - a private network inside AWS where all your resources run
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
# Create a subnet - sub-range inside VPC
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-2a"
map_public_ip_on_launch = true # assign public IP automatically to instances in this subnet
}
# Create an internet gateway - needed to give subnet internet access
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}
# Route table with “default route” pointing to the internet gateway
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0" # default route => internet
gateway_id = aws_internet_gateway.igw.id
}
}
# Associate the route table with the subnet
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
# EC2 Instance
resource "aws_instance" "example" {
ami = var.ami_value
instance_type = var.instance_type_value
subnet_id = aws_subnet.public.id
associate_public_ip_address = true
}
Explanation:
-
VPC (aws_vpc): This is a virtual network inside AWS. Think of it as your own private data center.
-
Subnet (aws_subnet): A subdivision of your VPC's IP address range. You created one subnet (10.0.1.0/24) inside your VPC.
-
Internet Gateway (aws_internet_gateway): Needed to connect your VPC to the internet.
-
Route Table (aws_route_table): Directs network traffic; here, all internet-bound traffic (0.0.0.0/0) goes through the internet gateway.
-
Route Table Association (aws_route_table_association): Connects the subnet to this route table so that instances in the subnet have internet access.
-
EC2 Instance (aws_instance): Creates a virtual server using the AMI and instance type you provide via variables. It is launched inside the subnet you created.
3. Module Variables (variable.tf)
variable "ami_value" {
description = "value of ami"
type = string
}
variable "instance_type_value" {
description = "value of instance type"
type = string
}
Explanation:
These define inputs to your module. The module expects two variable values:
-
ami_value: The AMI ID (like a template for your Linux/Windows image).
-
instance_type_value: EC2 instance type (t2.micro, t2.small, etc.).
These variables are passed from the root module when you call it.
4. Module Outputs (output.tf)
output "public_ip" {
value = aws_instance.example.public_ip
}
output "subnet_id" {
value = aws_subnet.public.id
}
Explanation:
-
Outputs expose values from the module back to the root so you can use or display them.
-
Here, you output the public IP address of the EC2 instance and the subnet ID.
Step-by-Step Flow When You Run Terraform:
-
Terraform reads the root main.tf.
-
It sees you want to create AWS resources using the AWS provider in us-east-2.
-
It sees you want to use the module "ec2_instance" from a local path and passes it the variables:
ami_value = "ami-0c55b159cbfafe1f0"
instance_type_value = "t2.micro"
-
Inside the module:
-
The VPC is created.
-
A public subnet is created within the VPC.
-
An internet gateway is created and attached to the VPC.
-
A route table with a route to the internet gateway is created.
-
The route table is associated with the subnet.
-
An EC2 instance is launched in the subnet with the given AMI and instance type.
-
After apply, outputs like the EC2 instance's public IP and subnet ID get printed or can be used by root or other modules.
Why use modules?
-
Reusability: You can reuse the module to spin up multiple instances or similar infrastructure with different parameters.
-
Organization: Modules help split complex configs into manageable pieces.
-
Team collaboration: Teams can maintain modules separately.
Best Practices & Tips for Beginners
-
Avoid duplicate provider blocks in modules unless necessary
-
Your module has a provider block declaring region us-east-2 — but you already set this at the root level.
-
Instead, generally declare providers in the root and do not declare them in the module, or pass the provider explicitly if needed.
-
Otherwise, you risk confusion or version conflicts.
-
Use variables for values you might want to change
-
Use locals or data sources where appropriate
- Instead of hardcoding AMI IDs, you could use AWS data sources to find the latest AMI dynamically.
-
Define outputs meaningfully
- Output values you or downstream modules need (IPs, IDs, ARNs, etc.).
-
Add comments & descriptions
- Helps you and others understand your config later.
-
Use .gitignore and store terraform state securely
-
Name your resources clearly
aws_instance.example is simple but try naming reflecting purpose, e.g., aws_instance.web_server.
-
Use tags
- Add AWS tags for easier tracking and billing.
Example:
tags = {
Name = "MyInstance"
Environment = "dev"
}
Summary / TL;DR
-
Root main.tf sets AWS provider and calls your module with required inputs.
-
Module creates basic networking (VPC, subnet, IGW, routes) and an EC2 instance inside that subnet.
-
Variables let you parameterize your EC2 AMI and instance type.
-
Outputs expose useful info (public IP, subnet ID).
-
This is a good beginner example to understand how Terraform modules work and how you can create reusable infrastructure components.
Feel free to ask questions if you'd like me to explain any part in more detail!