Deploying a React App on AWS S3 with CloudFront

Deploying a React App on AWS S3 with CloudFront

Publish Date: Jan 23
1 0

Deploying a React App to AWS S3 with CloudFront

1️⃣ Log in to AWS Console

Log in to your AWS console using your credentials.

2️⃣ Check if npm and Node.js are installed

Run the following commands to check if npm and Node.js are installed:

npm -v
node -v
Enter fullscreen mode Exit fullscreen mode

Checking npm and Node.js

3️⃣ Create a React App

Run the following commands to set up a React project using Vite:

npm create vite@latest
cd Dharshinisrig
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

Creating React App

4️⃣ View Your React App

Follow the link displayed in the terminal to open the React app in the browser.

React App Running

The React app should display like this:
React App Output

5️⃣ Modify App.tsx

Open the App.tsx file and make necessary edits.
Editing App.tsx

Modified React App

6️⃣ Build the React App

Run the following command to build the project:

npm run build
Enter fullscreen mode Exit fullscreen mode

Building React App

7️⃣ Create an S3 Bucket in AWS

Go to AWS S3 and create a new bucket.
Creating S3 Bucket

7.1 Remove Blocked Public Access

Disable "Block all public access."
Public Access Settings

7.2 Enable Bucket Versioning and Add Tags

Enable Bucket Versioning

8️⃣ Enable Static Website Hosting

Enable Static Website Hosting from the bucket properties.
Static Hosting

9️⃣ Configure Bucket Permissions

Edit the bucket policy to allow public access.
Edit Bucket Policy

Copy the Bucket ARN:
Copy Bucket ARN

Generate the Bucket Policy and save the changes:
Bucket Policy

🔟 Upload React Build Files

Upload all files from the dist folder to the S3 bucket.
Upload Files

1️⃣1️⃣ Access Your Website

Copy the index.html URL:
Copy Index.html URL

Or copy the Bucket Endpoint:

http://714022205024-dharshinisrig.s3-website-us-east-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Website Running

1️⃣2️⃣ Set Up CloudFront for Faster Access

Create a CloudFront distribution for the S3 bucket.
Create CloudFront Distribution

12.1 Disable WAF

Disable WAF

12.2 Deploy CloudFront

After deploying, copy the CloudFront URL:

https://d2yop5uofve2kk.cloudfront.net
Enter fullscreen mode Exit fullscreen mode

CloudFront Deployed

Visit the CloudFront URL to see your React app.
React App via CloudFront

1️⃣3️⃣ Clean Up AWS Resources

13.1 Delete CloudFront Distribution

Delete CloudFront

13.2 Delete S3 Objects

Delete Objects in S3

13.3 Delete the S3 Bucket

Delete S3 Bucket

🎉 Conclusion

You have successfully deployed a React app on AWS S3 and served it via CloudFront! 🚀

Comments 0 total

    Add comment