Deploy React App with Full CI/CD Pipeline on AWS | GitHub + CodePipeline + S3
In this tutorial, you'll learn how to build a fully automated CI/CD pipeline using AWS CodePipeline, CodeBuild, and Amazon S3 to deploy a React.js application hosted on GitHub. No need for manual deployments, every time you push to your repo, your app will automatically build and deploy to a static website on S3!
- Push code to the
mainbranch on GitHub. - AWS CodePipeline detects the change.
- AWS CodeBuild installs dependencies and builds the React app.
- The build output is deployed to the S3 bucket.
- S3 serves the app as a public static website.
First, we'll set up a React app by cloning the React app from my GitHub repository. You can use your own or follow along with mine. Make sure the app is committed to GitHub.
git clone https://github.com/leswlk/saas-landing-page.git
For the deploy provider we are going to use Amazon S3, we will create an S3 bucket.
- Head over to the S3 service.
- Click Create bucket.
- Name it something unique like
demo-react-cicd-bucket
Once the s3 bucket is created, leave it for now, as we will come for it to finish the setup later.
Now the fun part---building the pipeline.
- Go to AWS CodePipeline, click Create pipeline.
- Name your pipeline:
aws-codepipeline-react-app-demo - Choose a new service role or an existing one.
- Add source stage:
- Source provider: GitHub (connect your GitHub account).
- Select your repository and branch.
Note: Make sure you select the repository that we cloned in Step 1 Once you are connected to your Github and select your repository, then choose "Next"
- Add build stage:
- Provider: AWS CodeBuild.
- Choose "Create project"
Let's proceed to next step and create the CodeBuild Project.
Now let's set up CodeBuild, which will handle building the React app.
- Go to CodeBuild, click Create Build Project.
- Give it a name, something fuego!
- Choose a managed image: aws/codebuild/standard:6.0 (or latest).
- Under build specifications, choose "Use a buildspec file"
- Inside your GitHub repo, create a file named
buildspec.ymlin the root:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 18
commands:
- echo Installing dependencies...
- npm ci --legacy-peer-deps
build:
commands:
- echo Building the React app...
- npm run build
artifacts:
files:
- '**/*'
base-directory: dist
discard-paths: no
Note: This file tells CodeBuild to install dependencies, build the app, and copy the contents of the build folder as artifacts.
- Back to the CodeBuild Project, keep the rest as default and choose "Continue to CodePipeline"
- Then the CodeBuild project will be create and added to the build stage as shown below, then choose "Next"
- Add deploy stage:
- Provider: Amazon S3.
- Bucket: Select the one you created earlier
demo-react-cicd-bucket - Extract file option: YES, choose "Next"
- Lastly, review all the configuration and click "Create pipeline"
Once the pipeline is successfully created, you'll see it run through the source build and deploy stages.
- Go to Amazon S3 console
- Select our existing S3 bucket
demo-react-cicd-bucket - You should see the S3 bucket with objects inside, extracted from our CodePipeline.
- Now let's make this S3 Bucket public:\
- On the top bar, choose "Properties"
- Scroll down to "Static Website Hosting" and click "Edit"
- Under "Static Website Hosting", choose "Enable"
- And specify
index.htmlas the index document, then click "Save"
- Next, edit some permissions, while still on the top bar choose "Permissions"
- Scroll down to "Static Website Hosting" and click "Edit"
- Under "Static Website Hosting", choose "Enable"
- And specify
index.htmlas the index document, then click "Save"
- Next, edit some permissions, still on the tob bar choose "Permissions"
- Uncheck "Block all public access" to allow public access, then click "Save changes"
- Next, we will add a bucket policy to allow public read access inside our s3 bucket. Here's the sample policy you can use:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
Replace: your-bucket-name with your actual bucket name, then click "Save"
- Go back to the S3 Bucket console, on the top bar, choose Objects, then click on
index.html\ - To visit your React.js App, click on the Object URL.
- You should see your React.js App running on Amazon S3
Alrighty, time to test the whole pipeline. Let's say that during development of the app, a junior developer wanted to make a little joke on the senior team since he overheard someone mentioning food in an earlier meeting. But, since he accidentally pushed to production:
Wings are good, can't blame em. But, as soon as the code is pushed, CodePipeline is triggered. You'll see it run through the source, build, and deploy stages.
When you're done, remember to clean up your AWS resources to avoid an unwanted surprise waiting for you at the end of the month!













