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.
aws configure
)aws s3 mb s3://my-static-website --region us-east-1
aws s3 website s3://my-static-website --index-document index.html
aws s3 cp ./website/ s3://my-static-website/ --recursive
cat > policy.json <5. (Optional) Configure CloudFront for HTTPS
aws cloudfront create-distribution --origin-domain-name my-static-website.s3.amazonaws.comStep 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-group2. 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 EOF3. 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/0Step 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 fiRun the script:
chmod +x deploy.sh ./deploy.shStep 5: Post-Deployment Validation
- For S3: Open
http://my-static-website.s3-website-us-east-1.amazonaws.com
- For EC2: Open
http://<EC2-PUBLIC-IP>
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/ --recursiveConclusion
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.