How to Deploy a React App to AWS: A Step-by-Step Guide
Deploying a React app to AWS is a reliable way to serve your frontend at scale. AWS offers multiple options—Amazon S3 with CloudFront is a cost-effective, low-maintenance choice. This guide walks you through the essential steps to get your app live quickly.
First, ensure you have a production build of your React app. Run npm run build in your project folder. This creates a build directory with optimized static files (HTML, JS, CSS). These files are all you need to host on AWS.
1. Create an S3 Bucket
Log into your AWS Console and go to S3. Click “Create bucket”, choose a globally unique name (e.g., my-react-app-2024), and select your region. Under “Block Public Access settings”, uncheck “Block all public access” and acknowledge the warning. Enable “Static website hosting” in the Properties tab, enter index.html as the Index document.
2. Upload Your Build
Inside the bucket, click “Upload” and add all files from your build folder. Do not upload the folder itself—just its contents. After upload, go to the Permissions tab and add this bucket policy to make files publicly readable:
- Open “Bucket Policy” and paste:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::your-bucket-name/*" } ] } - Replace
your-bucket-namewith your actual bucket name.
3. Add CloudFront for HTTPS and Speed
Navigate to CloudFront and click “Create Distribution”. Under Origin, select your S3 bucket (choose “Use website endpoint” if prompted). Set “Viewer protocol policy” to Redirect HTTP to HTTPS. In the ���Default root object” field, type index.html. Leave other settings default and create the distribution. Wait 5–10 minutes for it to deploy.
4. Configure Error Pages for SPA Routing
If your React app uses client-side routing (e.g., React Router), you must handle 404 errors. In your S3 bucket, go to Properties -> Static website hosting -> Edit. Under “Error document”, enter index.html. This ensures any route like /about serves your app entry point.
Your React app is now live on AWS. Use the CloudFront domain name (e.g., d123.cloudfront.net) to access it. For a custom domain, add an alternate domain name in CloudFront and set up a Route 53 record. Always test thoroughly and consider enabling logging for production apps.