Deploying a React Application on AWS Amplify: A Beginner's Guide
Learn how to deploy a React application using AWS Amplify, connect it to a Git repository, and automatically deploy future changes with a simple CI/CD workflow.
In this tutorial, you will learn how to deploy a React application using AWS Amplify and configure automatic deployments whenever changes are pushed to your repository.
Prerequisites
Before starting, make sure you have:
- A React application
- A GitHub repository containing your project
- A Git provider account, such as GitHub
- An AWS account
- Node.js and npm installed locally
Your React project should contain the required configuration files, such as
package.json .Step 1: Prepare Your React Application
Before deploying, make sure your application works correctly on your local machine.
Install the project dependencies:
npm installThen run the development server:
npm run devOpen the local URL shown in your terminal and verify that the application works as expected.
It is also a good idea to test the production build locally:
npm run buildFor a typical Vite-based React application, this creates a
dist directory containing the production files.Once everything is working, commit and push your project to your Git repository.
Step 2: Open AWS Amplify
Sign in to the AWS Management Console and open AWS Amplify.
Choose the option to create a new application and select the option for hosting a web application.
Amplify supports connecting your application to supported Git providers, allowing AWS to retrieve your source code and automatically build your application.
Step 3: Connect Your Repository
Connect your Git provider account and select the repository containing your React application.
Choose the branch you want to deploy.
For example:
Repository: my-react-projectBranch: mainAmplify will detect your project and use the repository configuration to help configure the build process.
Review the detected settings before continuing.
Step 4: Configure the Build
Amplify needs to know how to install your dependencies and create the production version of your application.
For a typical Vite React project, a configuration can look like this:
version: 1frontend: phases: preBuild: commands: - npm ci build: commands: - npm run build artifacts: baseDirectory: dist files: - '**/*' cache: paths: - node_modules/**/*The important parts are:
-
npm ciinstalls the dependencies from the lock file.
-
npm run buildcreates the production build.
-
baseDirectory: disttells Amplify where the generated files are located.
- The cache configuration can help speed up subsequent builds.
The exact configuration may differ depending on the React framework, build tool, and project structure.
Step 5: Deploy the Application
After reviewing your repository and build configuration, start the deployment.
Amplify will perform the deployment process automatically:
- Clone the repository.
- Install the project dependencies.
- Run the configured build command.
- Collect the generated production files.
- Deploy the application.
Once the deployment succeeds, Amplify provides a URL that you can use to access your application.
Open the URL and verify that the deployed application works correctly.
Step 6: Enable Automatic Deployments
One of the biggest advantages of connecting Amplify to a Git repository is automatic deployment.
After your repository is connected, pushing a new commit to the configured branch can trigger a new build and deployment.
For example:
git add .git commit -m "Update homepage"git push origin mainAfter the push, Amplify can detect the change and start a new deployment.
This means you do not need to manually upload the application every time you make an update.
The general workflow becomes:
Write Code ↓Commit Changes ↓Push to Git ↓Amplify Starts Build ↓Application Is DeployedThis approach is useful for continuous integration and continuous deployment (CI/CD) workflows.
Common Problems and Solutions
Build Command Fails
If the deployment fails during the build stage, first check whether the configured build command matches your project.
For example:
npm run buildYou can run the same command locally to determine whether the problem exists in the project itself.
Also check your
package.json file to make sure the required build script exists.Wrong Output Directory
Different frameworks and build tools can generate production files in different directories.
For a typical Vite React project, the default output directory is:
distIf Amplify is looking for the wrong directory, verify the
baseDirectory setting in the build configuration.Dependencies Are Not Installed Correctly
If dependency installation fails, check that your repository contains the appropriate package manager lock file, such as:
package-lock.jsonFor npm-based projects, using:
npm cican provide a consistent dependency installation based on the lock file.
Environment Variables
If your application requires environment variables, configure them through the Amplify application's environment variable settings.
Do not commit sensitive credentials to your Git repository.
Never place API keys, passwords, access tokens, or other secrets directly into publicly accessible source code.
Verify Your Deployment
After deployment, don't stop at checking whether the homepage loads.
Test important parts of your application, including:
- Navigation
- Images and static assets
- API requests
- Forms
- Authentication, if used
- Environment variables
- Responsive layouts
If something works locally but fails after deployment, check the build logs and verify that your production configuration matches your local environment.
Conclusion
AWS Amplify provides a straightforward way to deploy frontend applications without manually managing a web server.
By connecting a React project to a Git repository, Amplify can handle the build and deployment process and can automatically deploy new changes when commits are pushed to the configured branch.
For beginners, this provides a practical introduction to modern cloud deployment and CI/CD workflows.
Once your basic deployment is working, you can explore additional AWS services and gradually build more advanced cloud applications.
Final Deployment Workflow
The complete process can be summarized as:
React Application ↓Git Repository ↓AWS Amplify ↓Install Dependencies ↓Build Application ↓Deploy Production Files ↓Live Web Application ↓Future Git Pushes ↓Automatic RedeploymentThis workflow makes it easier to move from developing an application locally to maintaining a continuously deployed application in the cloud.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article