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 invars.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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.