Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS

πŸš€ Deploy a Secure EC2 Web Server with Terraform, VPC, and Remote State on AWS

Are you looking to automate your AWS infrastructure and deploy a web server with best practices? In this guide, you’ll learn how to use Terraform to:

Provision a…


This content originally appeared on DEV Community and was authored by Tej Tandel

πŸš€ Deploy a Secure EC2 Web Server with Terraform, VPC, and Remote State on AWS

Are you looking to automate your AWS infrastructure and deploy a web server with best practices? In this guide, you'll learn how to use Terraform to:

  • Provision a secure VPC with public/private subnets
  • Launch an EC2 instance running Apache with a sample website
  • Manage SSH keys and security groups
  • Store your Terraform state remotely in S3 for safety and collaboration

Let's get started!

πŸ—‚οΈ Project Structure

.
β”œβ”€β”€ backend.tf
β”œβ”€β”€ Instance.tf
β”œβ”€β”€ KeyPair.tf
β”œβ”€β”€ Provider.tf
β”œβ”€β”€ README.md
β”œβ”€β”€ SecurityGroup.tf
β”œβ”€β”€ vars.tf
β”œβ”€β”€ vpc.tf
β”œβ”€β”€ web.sh
β”œβ”€β”€ .gitignore
β”œβ”€β”€ terraform-vpc-ssh-key
β”œβ”€β”€ terraform-vpc-ssh-key.pub
└── .terraform/

πŸ“¦ What Does This Project Do?

  • Creates a VPC with public and private subnets across multiple Availability Zones
  • Sets up an Internet Gateway and routing for public subnets
  • Launches an EC2 instance in a public subnet
  • Configures a Security Group for SSH and HTTP access
  • Manages SSH Key Pairs for secure access
  • Provisions Apache Web Server with a sample website using a shell script
  • Stores Terraform state remotely in an S3 bucket for collaboration and safety

πŸ› οΈ Prerequisites

  • Terraform installed
  • AWS CLI installed and configured (aws configure)
  • An AWS account with permissions to create EC2, VPC, and S3 resources

πŸ“ File Overview

  • Provider.tf: AWS provider configuration
  • vpc.tf: VPC, subnets, internet gateway, and route tables
  • Instance.tf: EC2 instance definition and provisioning
  • KeyPair.tf: SSH key pair resource
  • SecurityGroup.tf: Security group for SSH/HTTP access
  • vars.tf: Variables for region, AMI, zones, etc.
  • backend.tf: Remote state backend (S3)
  • web.sh: Script to install Apache and deploy a sample website
  • .gitignore: Ignore sensitive files and Terraform state
  • terraform-vpc-ssh-key / .pub: SSH keys (do not commit private key!)

🚦 Quick Start

# 1. Clone the repo
git clone https://github.com/tej6667/terraform-aws-vpc-ec2-webserver.git

# 2. Initialize Terraform
terraform init

# 3. Review the plan
terraform plan

# 4. Apply the configuration
terraform apply
# Type 'yes' when prompted

# 5. Destroy resources when done
terraform destroy
# Type 'yes' when prompted

βš™οΈ Customization

  • Region & Zones: Edit vars.tf for your preferred AWS region and availability zones.
  • AMI: Update the amiID map in vars.tf for your region.
  • Security Group: Change allowed IPs in SecurityGroup.tf.
  • S3 Backend: Set your S3 bucket name in backend.tf.
  • SSH Key: Replace with your own key in KeyPair.tf and upload your public key.

πŸ“ Notes & Best Practices

  • Never commit AWS credentials or private keys.
  • Review security group rules before applying.
  • Remote state in S3 is critical for team collaboration and disaster recovery.
  • Remember to destroy resources to avoid AWS charges.

🧩 Learn More

πŸ’¬ Questions?

Drop a comment below or check out the GitHub repo for more details!

Happy automating! πŸš€


This content originally appeared on DEV Community and was authored by Tej Tandel


Print Share Comment Cite Upload Translate Updates
APA

Tej Tandel | Sciencx (2025-06-17T21:55:41+00:00) Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS. Retrieved from https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/

MLA
" » Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS." Tej Tandel | Sciencx - Tuesday June 17, 2025, https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/
HARVARD
Tej Tandel | Sciencx Tuesday June 17, 2025 » Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS., viewed ,<https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/>
VANCOUVER
Tej Tandel | Sciencx - » Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/
CHICAGO
" » Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS." Tej Tandel | Sciencx - Accessed . https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/
IEEE
" » Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS." Tej Tandel | Sciencx [Online]. Available: https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/. [Accessed: ]
rf:citation
» Deploy a Secure EC2 Web Server with Terraform, Multi-AZ, and Remote State on AWS | Tej Tandel | Sciencx | https://www.scien.cx/2025/06/17/deploy-a-secure-ec2-web-server-with-terraform-multi-az-and-remote-state-on-aws/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.