2-how-to-deploy-a-react-application-with-serverless-architecture-on-aws.html

How to Deploy a React Application with Serverless Architecture on AWS

In today’s rapidly evolving web development landscape, building and deploying applications with serverless architecture has gained immense popularity. Serverless allows developers to focus on writing code without worrying about infrastructure management. AWS (Amazon Web Services) provides powerful tools to deploy your React applications seamlessly using a serverless architecture. This guide will walk you through deploying your React application on AWS, covering definitions, use cases, and actionable insights.

What is Serverless Architecture?

Serverless architecture is a cloud computing execution model where the cloud provider dynamically manages the allocation of machine resources. Developers only need to focus on writing code without the hassle of server setup, maintenance, and scaling. Instead of provisioning servers, you deploy functions that run in response to events.

Benefits of Serverless Architecture

  • Cost-Effective: You pay only for the compute time you consume.
  • Scalability: Automatic scaling based on demand.
  • Reduced Operational Complexity: No need to manage server infrastructure.
  • Faster Development: Developers can concentrate on building applications.

Why Choose AWS for Serverless Deployment?

AWS offers a plethora of services that make serverless deployment straightforward. Key services include:

  • AWS Lambda: To run your backend code without provisioning servers.
  • AWS S3: To host static files such as HTML, CSS, and JavaScript.
  • AWS API Gateway: To create and manage APIs for serverless applications.

Prerequisites

Before we get started, ensure you have:

  • An AWS account.
  • Node.js and npm installed on your machine.
  • A basic React application ready for deployment.

Step-by-Step Guide to Deploying a React Application on AWS

Step 1: Build Your React Application

First, build your React application for production. Navigate to your project directory and run:

npm run build

This command generates a build folder containing static files.

Step 2: Create an S3 Bucket

AWS S3 will host your static files. Follow these steps to create a bucket:

  1. Log in to the AWS Management Console.
  2. Navigate to S3.
  3. Click on "Create Bucket".
  4. Enter a unique bucket name (e.g., my-react-app-bucket).
  5. Choose a region and click "Create".

Step 3: Configure the S3 Bucket for Static Website Hosting

  1. Select your newly created bucket.
  2. Go to the "Properties" tab.
  3. Enable "Static website hosting".
  4. Specify index.html as the index document and 404.html as the error document.
  5. Save changes.

Step 4: Upload Your Build Files to S3

You can upload files manually through the console or automate it using the AWS CLI. To use the CLI, first, install it and configure it with your credentials:

aws configure

Then run the following command to sync your build folder with the S3 bucket:

aws s3 sync build/ s3://my-react-app-bucket/

Step 5: Set Bucket Policy for Public Access

To allow public access to your website, set a bucket policy:

  1. Go to the "Permissions" tab of your bucket.
  2. Click on "Bucket Policy".
  3. Insert the following policy:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-react-app-bucket/*"
    }
  ]
}
  1. Save changes.

Step 6: Access Your React Application

After the files are uploaded and the bucket policy is set, you can access your application via the URL provided under the "Static website hosting" section of the bucket properties:

http://my-react-app-bucket.s3-website-<region>.amazonaws.com

Step 7: (Optional) Set Up a Custom Domain with Route 53

To make your application accessible via a custom domain, follow these steps:

  1. Purchase a domain through AWS Route 53 or another registrar.
  2. Create a hosted zone in Route 53.
  3. Create an A record pointing to your S3 bucket.

Step 8: Deploying Backend Functions with AWS Lambda (Optional)

If your React app requires a backend, you can deploy serverless functions with AWS Lambda. Here’s a simple example:

  1. Create a new Lambda function from the AWS Management Console.
  2. Choose the "Author from scratch" option.
  3. Select a runtime (Node.js is a common choice).
  4. Write your function code. Here’s a simple example that returns a message:
exports.handler = async (event) => {
    return {
        statusCode: 200,
        body: JSON.stringify('Hello from AWS Lambda!'),
    };
};
  1. Set up an API Gateway to trigger your Lambda function.

Troubleshooting Common Issues

  • Bucket Not Public: If the website doesn't load, ensure your bucket is public.
  • CORS Issues: If you receive CORS errors, configure your S3 bucket CORS settings to allow your frontend domain.

Conclusion

Deploying a React application using serverless architecture on AWS is a robust solution for modern web development. With AWS S3, Lambda, and API Gateway, you can create scalable and cost-effective applications without the need for server management. By following the steps outlined in this guide, you can get your React application live in no time. Embrace the serverless revolution and enhance your development process today!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.