Terraform Project: Simple EC2 + Security Group

Published: (December 4, 2025 at 05:29 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Project Structure

terraform-project/
│── main.tf
│── variables.tf
│── outputs.tf
│── providers.tf
│── terraform.tfvars
│── modules/
│   └── ec2/
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf
└── README.md

providers.tf

Defines AWS provider + region.

terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

variables.tf

All input variables.

variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-east-2"
}

variable "instance_type" {
  description = "EC2 instance size"
  type        = string
  default     = "t2.micro"
}

variable "project_name" {
  description = "Tag for resources"
  type        = string
  default     = "tf-demo"
}

main.tf

Calls module and passes variables.

module "ec2_demo" {
  source        = "./modules/ec2"
  instance_type = var.instance_type
  project_name  = var.project_name
}

outputs.tf

output "ec2_public_ip" {
  description = "Public IP of EC2"
  value       = module.ec2_demo.public_ip
}

output "ec2_id" {
  description = "EC2 Instance ID"
  value       = module.ec2_demo.instance_id
}

terraform.tfvars (optional inputs)

aws_region    = "us-east-2"
instance_type = "t2.micro"
project_name  = "students-demo"

Module: modules/ec2/main.tf

Creates a security group and an EC2 instance with tags.

resource "aws_security_group" "demo_sg" {
  name        = "${var.project_name}-sg"
  description = "Allow SSH"

  ingress {
    description = "SSH"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "demo" {
  ami           = "ami-0c02fb55956c7d316" # Amazon Linux 2 us-east-2
  instance_type = var.instance_type
  security_groups = [aws_security_group.demo_sg.name]

  tags = {
    Name = "${var.project_name}-ec2"
  }
}

Module: modules/ec2/variables.tf

variable "instance_type" {
  type = string
}

variable "project_name" {
  type = string
}

Module: modules/ec2/outputs.tf

output "public_ip" {
  value = aws_instance.demo.public_ip
}

output "instance_id" {
  value = aws_instance.demo.id
}

How to Run (Teaching Steps)

  1. Initialize Terraform

    terraform init
  2. Validate configuration

    terraform validate
  3. Review the execution plan

    terraform plan
  4. Apply the configuration

    terraform apply -auto-approve
  5. View output values

    terraform output
  6. Destroy the infrastructure when finished

    terraform destroy -auto-approve

What Students Learn From This Project

ComponentWhat it Teaches
providers.tfProvider setup, version constraints
variables.tfVariables, types, defaults
terraform.tfvarsOverriding default values
main.tfCalling modules
modules/Real‑world production design
EC2 + SGSimple infrastructure provisioning
outputs.tfExporting values
Terraform workflow (init, plan, apply, destroy)Full lifecycle management
Back to Blog

Related posts

Read more »

Saving Terraform State in S3

Configuring S3 as a Terraform Backend Terraform can store its state in an S3 bucket. Below is a minimal configuration that sets up the S3 backend: hcl terrafor...

AWS Terraform Lifecycle Rules

Introduction Infrastructure as Code IaC is most powerful when you have full control over how resources behave during updates, replacements, and deletions. Terr...