https://github.com/atulkamble/terraform-webserver-setup
This project provides a step-by-step guide for launching, connecting to, and installing a webserver on an EC2 instance using Terraform.
https://github.com/atulkamble/terraform-webserver-setup
aws cli ec2 git iac infrastructure-as-code terraform
Last synced: about 1 month ago
JSON representation
This project provides a step-by-step guide for launching, connecting to, and installing a webserver on an EC2 instance using Terraform.
- Host: GitHub
- URL: https://github.com/atulkamble/terraform-webserver-setup
- Owner: atulkamble
- Created: 2024-03-21T08:24:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-08T05:35:23.000Z (about 1 year ago)
- Last Synced: 2025-07-03T13:28:07.487Z (12 months ago)
- Topics: aws, cli, ec2, git, iac, infrastructure-as-code, terraform
- Language: HCL
- Homepage: http://linkedin.com/in/atuljkamble
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Terraform Web Server Setup (AWS EC2)
This project uses **Terraform** to provision an **Apache HTTP web server** on an **AWS EC2 instance** inside the default VPC. It also generates a secure SSH key pair, configures security groups, and automatically installs and starts Apache on the EC2 instance.
## ๐ Features
- Deploys EC2 instance in AWS (Amazon Linux 2)
- Installs and starts Apache HTTPD server
- Generates and saves RSA private key securely
- Opens port **80 (HTTP)** and **22 (SSH)** via Security Group
- Automatically fetches the public IP of the instance
- Lightweight deployment using `t3.medium` instance
- Cross-platform `deploy.sh` automation script
## ๐ Project Structure
```bash
.
โโโ aws # Directory to store private key
โโโ deploy.sh # Shell script to deploy infrastructure
โโโ instructions.md # Step-by-step usage instructions
โโโ main.tf # Terraform config for AWS EC2 setup
โโโ outputs.tf # Outputs the EC2 instance's public IP
โโโ README.md # Project overview and usage
โโโ terraform.tfstate # Terraform state file (auto-generated)
โโโ terraform.tfstate.backup
````
## ๐ Quick Start
```bash
# 1. Clone this repo
git clone https://github.com/yourusername/terraform-webserver-setup.git
cd terraform-webserver-setup
# 2. Run the deployment script
chmod +x deploy.sh
./deploy.sh
```
## ๐ Access Web Server
After deployment, the script will output and (on supported OS) open the web server in your default browser:
```
http://
```
## ๐ SSH Access
```bash
chmod 400 aws/mywebserver.pem
ssh -i aws/mywebserver.pem ec2-user@
```
## ๐งน Destroy Infrastructure
To destroy the created infrastructure:
```bash
terraform destroy -auto-approve
```
## ๐ Requirements
* AWS CLI configured with access to create EC2 resources
* Terraform >= 1.5.0
* Bash shell (macOS/Linux)
# ๐ Instructions
```markdown
# Instructions: Deploy EC2 Web Server with Terraform
This guide walks you through deploying a basic Apache web server on AWS EC2 using Terraform.
---
## Prerequisites
- AWS account with EC2 access
- Terraform installed (`terraform -v`)
- AWS credentials configured via `aws configure` or environment variables
---
## Steps
### 1. Clone the Repository
```bash
git clone https://github.com/yourusername/terraform-webserver-setup.git
cd terraform-webserver-setup
````
---
### 2. Make Script Executable
```bash
chmod +x deploy.sh
```
---
### 3. Deploy Infrastructure
```bash
./deploy.sh
```
What this script does:
* Removes any existing key file (`aws/mywebserver.pem`)
* Initializes Terraform
* Applies the infrastructure
* Fetches the public IP
* Opens the web page in your browser
---
### 4. Access Web Server
Once deployed, visit:
```
http://
```
You should see a message like:
```
Welcome to Webserver ip-172-31-xx-xx.ec2.internal
```
---
### 5. SSH into Instance (Optional)
```bash
chmod 400 aws/mywebserver.pem
ssh -i aws/mywebserver.pem ec2-user@
```
---
### 6. Destroy Infrastructure (Optional)
```bash
terraform destroy -auto-approve
```
---
## Notes
* The RSA key pair is generated dynamically during deployment.
* The private key is saved to `aws/mywebserver.pem`
* Apache is installed and enabled on boot.
* The AMI used is Amazon Linux 2 (`ami-0cbbe2c6a1bb2ad63`) in `us-east-1`.
---
## Troubleshooting
* **Permission denied (publickey)**: Make sure `.pem` file has `chmod 400` and is the correct key.
* **Instance not accessible**: Ensure security group allows inbound SSH (22) and HTTP (80) traffic.
* **Key file not found**: Ensure `aws/` directory exists or re-run `deploy.sh`.
---