How to Host Your React Application in AWS S3

A step-by-step guide for deploying your React app

•

Figure 1.1 Hosting React app on AWS S3

Introduction

Hosting a React app on Amazon Web Services (AWS) Simple Storage Service (S3) is a quick and inexpensive way to serve static websites. AWS S3 is a good solution for delivering your React application because of its scalability, security features, and worldwide availability. We’ll walk you through the process of hosting your React app on AWS S3, guaranteeing that your website is up and running quickly.

Prerequisites:

To follow this guide successfully, you should have the following prerequisites:

  • Basic knowledge of React.js and JavaScript
  • An active AWS account with appropriate permissions
  • An existing React app ready for deployment
  • AWS Command Line Interface (CLI) installed on your machine

Step 1: Create an S3 Bucket

  1. Log in to your AWS Management Console and navigate to the S3 service.
  2. Click on “Create Bucket” and provide a unique name for your bucket.
  3. Select the region closest to your target audience for optimal latency.
  4. Leave all other options as default and click “Create” to create the bucket.

Step 2: Configure the S3 Bucket

  1. Once your bucket is created, select it from the S3 dashboard.
  2. Go to the “Properties” tab and click on “Static website hosting.”
  3. Choose the “Use this bucket to host a website” option.
  4. Set the “Index document” to “index.html” or the entry point of your React app.
  5. Optionally, specify an “Error document” to handle 404 errors.
  6. Save the configuration.

Step 3: Prepare Your React App for Deployment

  1. Build your React app for production. Open your terminal and navigate to your project’s root directory.
  2. Run the command `npm run build` or `yarn build` to create an optimized build of your app.
  3. This process generates a “build” folder containing all the necessary static assets for deployment.

Step 4: Deploy Your React App to S3

  1. Install the AWS CLI on your machine if you haven’t already.
  2. Open your terminal and run aws configure to set up your AWS credentials. Follow the prompts and provide your access key, secret key, preferred region, and default output format.
  3. Once configured, use the following command to upload your React app’s build folder to the S3 bucket:
aws s3 sync build/ s3://your-bucket-name

Replace “your-bucket-name” with the name of your S3 bucket.

  1. Wait for the upload to finish, and you’ll see your React app’s files appear in the S3 bucket.

Step 5: Enable Public Access to Your React App

  1. Go back to your S3 bucket and select the “Permissions” tab.
  2. Click on “Bucket Policy” and paste the following policy, which allows public access to your bucket’s contents:
{
 “Version”:”2012–10–17",
 “Statement”:[{
 “Sid”:”PublicReadGetObject”,
 “Effect”:”Allow”,
 “Principal”: “*”,
 “Action”:[“s3:GetObject”],
 “Resource”:[“arn:aws:s3:::your-bucket-name/*”]
 }]
}

Again, replace “your-bucket-name” with your actual bucket name.

  1. Save the policy.

Step 6: Configure DNS (Optional)

  1. If you have a custom domain, you can configure it to point to your S3 bucket.
  2. Go to your domain provider’s website and locate the DNS management settings.
  3. Create a new CNAME record and assign it a subdomain (e.g., “www” or any other desired subdomain).
  4. Set the CNAME target to your S3 bucket’s endpoint. To find the endpoint, go to your S3 bucket’s properties and copy the “Endpoint” URL.
  5. Save the DNS settings and allow some time for the changes to propagate.

Step 7: Test Your React App

  1. After the DNS changes have propagated, you can access your React app using either the S3 bucket URL (provided in the bucket properties) or your custom domain (if configured).
  2. Open a web browser and enter the URL. You should see your React app up and running.

Conclusion

You have successfully hosted your React app on AWS S3 by following this step-by-step method. AWS S3 is a dependable and scalable infrastructure for providing static websites, making it an excellent alternative for hosting React apps. Remember to create and deploy your React app to the S3 bucket and configure public access and, if needed, DNS for a custom domain. You can benefit from AWS S3’s global availability and robust infrastructure by hosting your React app there, providing an easy and efficient user experience for your website visitors.

Continue Learning

Discover more articles on similar topics