Build and Deploy a Serverless GraphQL React App Using AWS Amplify
Recently I recognized that some SaaS (Software as a Service) products use AWS Amplify which helps them to build serverless full-stack applications. I think serverless computing will be the future of apps and software. Therefore, I wanted to gather some hands-on experience, and I built a serverless application using AWS Amplify that uses React as frontend framework and GraphQL as backend API.
In this article, I want to guide you through the process how to build and deploy such an Amplify application.
Set up Amplify
AWS Amplify describes itself as:
Fastest, easiest way to build mobile and web apps that scale
Amplify provides tools and services to build scalable full-stack applications powered by AWS (Amazon Web Services). With Amplify configuring backends and deploying static web apps is easy. It supports web frameworks like Angular, React, Vue, JavaScript, Next.js, and mobile platforms including iOS, Android, React Native, Ionic, and Flutter.
You'll need to create an AWS account to follow the following steps. No worries, after signing up you have access to the AWS Free Tier which does not include any upfront charges or term commitments.
The next step is to install the Amplify Command Line Interface (CLI). In my case I used cURL on macOS:
Alternatively, you can watch this video to learn how to install and configure the Amplify CLI.
Now we can configure Amplify using the CLI
which will ask us to sign in to AWS Console. Once we’re signed in, Amplify CLI will ask us to create an AWS IAM user:
We'll be redirected to IAM where we need to finish the wizard and create a user with AdministratorAccess
in our account to provision AWS resources. Once the user is created, Amplify CLI will ask us to provide the accessKeyId
and secretAccessKey
to connect Amplify CLI with our created IAM user:
Set up full-stack Amplify project
At this point, we are ready to set up our full-stack project using a React application in the frontend and GraphQL as backend API. We'll build a Todo CRUD (create, read, update, delete) application that uses this architecture:
The complete source code of this demo is available at GitHub.
Create React frontend
Let's start by creating a new React app using create-react-app. From our projects directory we run the following commands to create our new React app in a directory called amplify-react-graphql-demo
and to navigate into that new directory:
To start our React app we can run
which will start the development server at http://localhost:3000
.
Initialize Amplify
Now it's time to initialize Amplify in our project. From the root of the project we run
which will prompt some information about the app:
When our new Amplify project is initialized, the CLI:
- created a file called
aws-exports.js
in the src directory that holds all the configuration for the services we create with Amplify - created a top-level directory called
amplify
that contains our backend definition - modified the
.gitignore
file and adds some generated files to the ignore list
Additionally, a new cloud project is created in the AWS Amplify Console that can be accessed by running amplify console
. Amplify Console provides two main services: hosting and the Admin UI. More information can be found here.
The next step is to install some Amplify libraries:
aws-amplify
: the main library for working with Amplify in your apps@aws-amplify/ui-react
: includes React specific UI componentstypescript
: we will use TypeScript in some parts of this demo
Next, we need to configure Amplify on the client. Therefore, we need to add the following code below the last import in src/index.js
:
At this point wee have a running React frontend application, Amplify is configured, and we can now add our GraphQL API.
Create GraphQL API
We will now create a backend that provides a GraphQL API using AWS AppSync (a managed GraphQL service) that uses Amazon DynamoDB (a NoSQL database).
To add a new API we need to run the following command in our project’s root folder:
After the process finished successfully we can inspect the GraphQL schema at amplify/backend/api/demoapi/schema.graphql
:
The generated Todo type is annotated with a @model
directive that is part of the GraphQL transform library of Amplify. The library contains multiple directives which can be used for authentication, to define data models, and more. Adding the @model
directive will create a database table for this type (in our example a Todo table), the CRUD (create, read, update, delete) schema, and the corresponding GraphQL resolvers.
Now it's time to deploy our backend:
After it is finished successfully our GraphQL API is deployed and we can interact with it. To see and interact with the GraphQL API in the AppSync console at any time we can run:
Alternatively, we can run this command
to view the entire app in the Amplify console.
Connect frontend to API
The GraphQL mutations, queries and subscriptions are available at src/graphql
. To be able to interact with them we can use the generated src/API.ts
file. So we need extend App.js
to be able to create, edit and delete Todos via our GraphQL API:
The full source code of this demo is available at GitHub.
The application should show a list of available Todos which can be edited or deleted. Additionally, we have the possibility to create new Todos:
Add authentication
Amplify uses Amazon Cognito as the main authentication provider. We'll use it to add authentication to our application by adding a login that requires a password and username.
To add authentication we need to run
and deploy our service by running
Now we can add the login UI to our frontend. The login flow can easily be handled by using the withAuthenticator
wrapper from the @aws-amplify/ui-react
package. We just need to adjust our App.js
and import withAuthenticator
:
Now we need to wrap the main component with the withAuthenticator
wrapper:
Running npm start
will now start the app with an authentication flow allowing users to sign up and sign in:
Deploy and host app
Finally, we want to deploy our app which can be either done manually or via automatic continuous deployment. In this demo I want to deploy it manually and host it as static web app. If you want to use continuous deployment instead, please check out this official guide.
First, we need to add hosting:
and then we are ready to publish our app:
After publishing, we can see the app URL where our application is hosted on an `amplifyapp.com domain in our terminal.
What's next
Amplify provides also a way to run your API locally, check out this tutorial.
Here are some cool things that you can additionally add to your Amplify application:
Take a look at the official Amplify docs for further information about the framework.
Conclusion
In this article I showed you that building and deploying a full-stack serverless application using AWS Amplify requires a minimum amount of work. Without using such a framework it would be much harder and this way you can focus more on the end product instead of what is happening inside.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Run, Build & Deploy Stencil and Storybook From One Repository
I recently joined a project where the team used two separate Git repositories for their web components based on Stencil and Storybook. But the idea of Storybook is that the so-called "stories" live next to the components source code. Therefore, it made no sense to me to have those two tools in different repositories, and I combined them both in one repository.
How to Use Environment Variables to Store Secrets in AWS Amplify Backend
The twelve-factor app is a known methodology for building software-as-a-service apps. One factor describes that an application's configuration should be stored in the environment and not in the code to enforce a strict separation of config from code.