DEV Community

Siva Sakthii U S
Siva Sakthii U S

Posted on

Deploying a React App with Vite on AWS S3 and CloudFront: A Step-by-Step Guide

Introduction

In this blog, we’ll walk you through deploying a React app, built with Vite, to AWS S3 with CloudFront for fast and secure delivery. By the end, you’ll have your static React site live and ready to scale globally.

Image description


Step 1: Create a React Application

  • Download and install Node.js (LTS version recommended) from Node.js Official Website.

  • Check the version of node and npm:
    Image description

  • Create a Vite Project:
    Image description

  • Install dependencies:
    Image description

  • Execute npm run dev :
    Image description

  • Change the UI for your needs

  • Build the React App:
    (Generate the static files for deployment)

npm run build
Enter fullscreen mode Exit fullscreen mode

The output will be in the dist/ folder.


Step 2: Set Up an S3 Bucket

Create an S3 Bucket:

  • Go to the AWS Management Console > S3.
  • Click Create bucket.
  • Provide a unique bucket name (e.g., my-static-react-site).

Image description

  • Disable Block all public access.

Image description

  • Bucket versioning

Image description

  • Being a professional add Tags

Image description

  • Create the bucket.

Image description

Upload React Build Files:

  • Navigate to your S3 bucket.

Image description

  • Click Upload > Add files.

Image description

  • Upload all files from the dist/ folder.

Image description

Set Public Read Access:

  • Go to Permissions.

  • Edit Bucket Policy:
    Image description

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-static-react-site/*"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Enable Static Website Hosting:

  • In the Properties tab, enable Static website hosting.

Image description

Image description

  • Set the Index document to index.html.

Image description

  • Note the Bucket website endpoint.

Step 3: Configure CloudFront for CDN

Create a CloudFront Distribution:

  • Go to CloudFront in the AWS Console.

Image description

  • Click Create Distribution.
  • In the Origin Domain Name, select your S3 bucket (or paste the static website URL).

Image description

  • Select "Do not enable security protections" Image description
  • Click Create Distribution

Deploy the CloudFront Distribution:

  • Note the CloudFront domain name after deployment.

Step 5: Verify Deployment

  • Open your CloudFront distribution URL in a browser.
  • Ensure the React site loads correctly.

Conclusion

With these steps, you’ve successfully deployed your Vite-based React app on AWS S3 with CloudFront. This setup ensures your app is highly available, fast, and scalable.

Top comments (0)