Skip to content

pathnex/pathnex_terraform_for_ec2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Terraform AWS EC2 Deployment This project provisions an Amazon EC2 instance on AWS using Terraform, installs Apache (httpd), and serves a simple web page.

It uses: Remote backend in S3 for Terraform state DynamoDB for state locking Default VPC and subnet Security Group allowing HTTP (port 80)

πŸ“ Project Structure

β”œβ”€β”€ backend.tf # Remote backend (S3 + DynamoDB) β”œβ”€β”€ main.tf # Core infrastructure (EC2, SG, AMI, VPC) β”œβ”€β”€ variables.tf # Input variables β”œβ”€β”€ outputs.tf # Terraform outputs β”œβ”€β”€ versions.tf # Terraform & provider version constraints └── readme.md # Project documentation

πŸ—οΈ Infrastructure Created This Terraform configuration provisions: βœ… 1 EC2 instance (Amazon Linux 2023) βœ… Security Group (HTTP only – port 80 open to public) βœ… Apache Web Server installed via user_data βœ… Public IP assigned automatically βœ… Remote state stored in S3 βœ… State locking using DynamoDB

βš™οΈ Backend Configuration Remote state is stored in: S3 Bucket: pathnex-feb-2026-batch Key: pathnex/terraform.tfstate Region: us-east-1 DynamoDB Table: terraform-locks Encryption: Enabled

⚠️ Ensure the S3 bucket and DynamoDB table exist before running terraform init.

πŸ”§ Requirements Terraform >= 1.6.0 AWS Provider ~> 5.0 AWS CLI configured AWS profile configured

Example: export AWS_PROFILE=pathnex

πŸ“Œ Variables Variable Description Default aws_region AWS region us-east-1 instance_type EC2 instance type t3.micro name_prefix Name prefix for tags Pathnex-Feb-2026 tags Common resource tags { Project = "terraform-class" }

πŸš€ How to Deploy 1️⃣ Format Code (Best Practice) Always format Terraform code before deployment: terraform fmt -recursive 2️⃣ Initialize Terraform terraform init 3️⃣ Validate Configuration terraform validate 4️⃣ Plan Deployment terraform plan 5️⃣ Apply Infrastructure terraform apply Type yes when prompted.

🌐 Access the Web Server After successful deployment, Terraform will output: instance_id public_ip web_url

Example: web_url = http://

Open the URL in your browser to see: Hello All, Today is 18 Feb 2026 and ec2 creation with the help of terraform is successful # you can change it in "user_data" section in main.tf

πŸ” Security Group Rules Inbound Port 80 (HTTP) β†’ 0.0.0.0/0 Outbound All traffic allowed

⚠️ Important Production Security Notes This setup is created for learning/demo purposes only. In real production environments: ❌ We do NOT open ports like 80 or 22 to 0.0.0.0/0 ❌ We do NOT directly attach public IPs to backend application servers ❌ We do NOT expose EC2 instances directly to the internet

🏭 How This Can Be Improved for Production In production, the following improvements should be implemented:

πŸ” Networking & Security Place EC2 instances in private subnets Use an Application Load Balancer (ALB) for public traffic Allow traffic only from ALB Security Group β†’ EC2 Security Group Enable HTTPS (port 443) with SSL certificates Restrict Security Group CIDR ranges Use NACLs for additional network control Enable VPC Flow Logs

πŸ”‘ Identity & Access Use IAM Roles (no static credentials) Follow least-privilege IAM policies Enable MFA for AWS users

πŸ“Š Monitoring & Logging Enable CloudWatch Logs Enable CloudWatch Alarms Enable AWS Config Enable GuardDuty

πŸ’Ύ Data Protection Enable EBS encryption Enable S3 versioning on backend bucket Enable S3 bucket access logging

πŸ“ˆ High Availability & Scaling Use Auto Scaling Group (ASG) Use Launch Templates Deploy across multiple Availability Zones Add Health Checks

πŸ§ͺ DevOps Best Practices Use separate environments (dev/stage/prod) Use remote variable files Integrate with CI/CD (GitHub Actions / Terraform Cloud) Add security scanning (tfsec, checkov) Use pre-commit hooks Implement modular Terraform structure

🧹 Destroy Infrastructure To remove all created resources: terraform destroy

🏷️ Tags Used Resources are tagged with: Project = terraform-class Name = pathnex-feb-tf-instance batch = Pathnex-Feb-2026

πŸ“š Learning Objectives Covered Terraform remote backend configuration State locking using DynamoDB EC2 provisioning Security Groups Data sources (VPC, Subnets, AMI) User data scripting Outputs Terraform formatting (terraform fmt) Infrastructure as Code best practices

πŸ‘¨β€πŸ’» Author www.youtube.com/@pathnex

About

This project provisions an Amazon EC2 instance on AWS using Terraform, installs Apache (httpd), and serves a simple web page.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages