Deploy a Static Website to AWS S3 or Apache Web Server

Objective

Deploy a simple static website using AWS S3 (for serverless hosting) or Apache (for traditional hosting on an EC2 instance). Automate the deployment process using a script.

Project Scope

Architecture

Option 1: AWS S3 Hosting

  1. AWS S3 Bucket (Public or Private with CloudFront)
  2. IAM Policies for Secure Access
  3. CloudFront (Optional for Performance & HTTPS)
  4. Route 53 (Optional for Custom Domain)

Option 2: Apache Web Server

  1. AWS EC2 (Amazon Linux/Ubuntu)
  2. Apache Web Server
  3. Security Groups & IAM Roles
  4. Route 53 (Optional for Custom Domain)

Implementation Steps

Step 1: Prerequisites

Step 2: Setup AWS S3 for Hosting (Option 1)

1. Create an S3 Bucket

aws s3 mb s3://my-static-website --region us-east-1

2. Enable Static Website Hosting

aws s3 website s3://my-static-website --index-document index.html

3. Upload Website Files

aws s3 cp ./website/ s3://my-static-website/ --recursive

4. Configure Public Read Access (If Needed)

cat > policy.json <
    

5. (Optional) Configure CloudFront for HTTPS

aws cloudfront create-distribution --origin-domain-name my-static-website.s3.amazonaws.com

Step 3: Setup Apache Web Server on EC2 (Option 2)

1. Launch EC2 Instance

aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t2.micro --key-name my-key --security-groups my-security-group

2. Install Apache & Deploy Website

ssh -i my-key.pem ec2-user@<EC2-PUBLIC-IP> << 'EOF'
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
echo "<h1>Welcome to My Website</h1>" | sudo tee /var/www/html/index.html
EOF
    

3. Configure Security Group to Allow HTTP/HTTPS

aws ec2 authorize-security-group-ingress --group-name my-security-group --protocol tcp --port 80 --cidr 0.0.0.0/0

Step 4: Automate Deployment with a Shell Script

Create a script to handle deployments.

deploy.sh

#!/bin/bash

S3_BUCKET="my-static-website"
EC2_USER="ec2-user"
EC2_IP="your-ec2-public-ip"
DEPLOY_PATH="/var/www/html"

echo "Choose Deployment Option:"
echo "1 - Deploy to AWS S3"
echo "2 - Deploy to Apache on EC2"
read -p "Enter your choice: " choice

if [ "$choice" -eq 1 ]; then
  echo "Deploying to S3..."
  aws s3 cp ./website/ s3://$S3_BUCKET/ --recursive
  echo "Deployment to S3 completed!"
elif [ "$choice" -eq 2 ]; then
  echo "Deploying to Apache Web Server..."
  scp -r ./website/* $EC2_USER@$EC2_IP:$DEPLOY_PATH
  ssh $EC2_USER@$EC2_IP "sudo systemctl restart httpd"
  echo "Deployment to Apache completed!"
else
  echo "Invalid option, exiting."
  exit 1
fi
    

Run the script:

chmod +x deploy.sh
./deploy.sh

Step 5: Post-Deployment Validation

Step 6: (Optional) CI/CD Pipeline

For automated deployments, integrate the script into GitHub Actions or Jenkins.

GitHub Actions Workflow Example

Create .github/workflows/deploy.yml

name: Deploy to AWS

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Install AWS CLI
        run: sudo apt-get install -y awscli

      - name: Deploy to S3
        run: aws s3 cp ./website/ s3://my-static-website/ --recursive
    

Conclusion

This project provides a simple deployment workflow for hosting a static website on AWS S3 or an Apache Web Server using manual setup, automated scripts, and CI/CD pipelines.