This guide will walk you through the process of deploying a static website using Amazon Web Services (AWS). We'll cover everything from purchasing a domain to setting up automated deployment using AWS CLI.
- Prerequisites
- Purchase Domain from Namecheap
- Set Up AWS S3 Bucket
- Configure AWS CloudFront
- Set Up AWS Certificate Manager
- Configure AWS Route 53 7.Generate AWS Access Keys
- Set Up AWS CLI
- Create Deployment Script
- An AWS account
- Basic knowledge of command-line interface
- Your static website files ready for deployment
Go to Namecheap and search for your desired domain name. Add the domain to your cart and complete the purchase process.
-
Log in to your AWS Management Console.
-
Navigate to S3 and click "Create bucket".
-
Configure bucket settings:
- Uncheck "Block all public access"
- Enable versioning
- Leave other settings as default
-
Create the bucket.
-
Go to the bucket's "Permissions" tab and add the following bucket policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
Replace your-bucket-name with your actual bucket name.
-
Go to CloudFront in the AWS Console and click "Create Distribution".
-
Choose "Web" as the delivery method.
-
Set "Compress Objects Automatically" to "Yes".
-
Under "Default Cache Behavior Settings":
-
Viewer Protocol Policy: Redirect HTTP to HTTPS
-
Allowed HTTP Methods: GET, HEAD
-
Cache Policy and Origin Request Policy: Use the defaults
- Under "Distribution Settings":
- Price Class: Choose your preferred option
- Alternate Domain Names (CNAMEs): Enter your domain name
- SSL Certificate: We'll set this up in the next step
- Create the distribution.
Warning! AWS Certificate Manager must be created in the US-EAST-1 Region Warning! AWS Certificate Manager must be created in the US-EAST-1 Region Warning! AWS Certificate Manager must be created in the US-EAST-1 Region
-
Go to AWS Certificate Manager.
-
Click "Request a certificate".
-
Choose "Request a public certificate" and click "Next".
-
Enter your domain name and any subdomains (e.g., www.yourdomain.com).
-
Choose "DNS validation" and click "Request". You'll see validation records. Keep this page open for the next step.
-
Go to Route 53 in the AWS Console.
-
Click "Create Hosted Zone".
-
Enter your domain name and create the zone.
-
Go back to Namecheap, find your domain, and update the nameservers with the ones from Route 53.
-
In Route 53, create a new record set:
- Name: leave blank (or enter www if you want a www subdomain)
- Type: A - IPv4 address
- Alias: Yes
- Alias Target: Select your CloudFront distribution
- Add the CNAME records for SSL certificate validation (from the Certificate Manager step).
- Go to IAM (Identity and Access Management) in the AWS Console.
- Click on your user name.
- Go to the "Security credentials" tab.
- Under "Access keys", click "Create access key".
- Download the key file and keep it secure.
- Install AWS CLI following the official guide.
- Open your terminal and run:
aws configure
- Enter your AWS Access Key ID and Secret Access Key when prompted.
- For default region, enter your preferred region.
- For default output format, you can leave it blank or enter json.
Create a file named deploy.sh in your project root with the following content:
#!/bin/bash
# Variables
BUCKET_NAME="your-bucket-name"
DISTRIBUTION_ID="your-cloudfront-distribution-id"
# Remove existing files in the bucket
aws s3 rm s3://$BUCKET_NAME --recursive
# Upload new files to the bucket
aws s3 sync . s3://$BUCKET_NAME --exclude "*.sh" --exclude ".git/*" --exclude "README.md"
# Invalidate CloudFront cache
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*"
echo "Deployment complete!"
Replace your-bucket-name and your-cloudfront-distribution-id with your actual values. To use this script:
- Make it executable: chmod +x deploy.sh
- Run it from your project directory: ./deploy.sh
This script will delete all existing files in your S3 bucket, upload your new files, and invalidate the CloudFront cache to ensure your changes are immediately visible.