Build a serverless React Native mobile app by using AWS Amplify - AWS Prescriptive Guidance

Build a serverless React Native mobile app by using AWS Amplify

Created by Deekshitulu Pentakota (AWS)

Code repository: aws-amplify-react-native-ios-todo-app

Environment: Production

Source: NA

Target: AWS Amplify, AWS AppSync, Amazon Cognito, Amazon DynamoDB

R Type: Re-architect

Workload: Open-source

Technologies: Serverless; Web & mobile apps

AWS services: AWS Amplify; AWS AppSync; Amazon Cognito; Amazon DynamoDB

Summary

This pattern shows how to create a serverless backend for a React Native mobile app by using AWS Amplify and the following AWS services:

  • AWS AppSync

  • Amazon Cognito

  • Amazon DynamoDB

After you configure and deploy the app’s backend by using Amplify, Amazon Cognito authenticates app users and authorizes them to access the app. AWS AppSync then interacts with the frontend app and with a backend DynamoDB table to create and fetch data.

Note: This pattern uses a simple “ToDoList” app as an example, but you can use a similar procedure to create any React Native mobile app.

Prerequisites and limitations

Prerequisites 

  • An active AWS Account

  • Amplify Command Line Interface (Amplify CLI), installed and configured

  • XCode (any version)

  • Microsoft Visual Studio (any version, any code editor, any text editor)

  • Familiarity with Amplify

  • Familiarity with Amazon Cognito

  • Familiarity with AWS AppSync

  • Familiarity with DynamoDB

  • Familiarity with Node.js

  • Familiarity with npm

  • Familiarity with React and React Native

  • Familiarity with JavaScript and ECMAScript 6 (ES6)

  • Familiarity with GraphQL

Architecture

The following diagram shows an example architecture for running a React Native mobile app’s backend in the AWS Cloud:

Workflow for running a React Native mobile app with AWS services.

The diagram shows the following architecture:

  1. Amazon Cognito authenticates app users and authorizes them to access the app.

  2. To create and fetch data, AWS AppSync uses a GraphQL API to interact with the frontend app and a backend DynamoDB table.

Tools

AWS services

  • AWS Amplify is a set of purpose-built tools and features that helps frontend web and mobile developers quickly build full-stack applications on AWS.

  • AWS AppSync provides a scalable GraphQL interface that helps application developers combine data from multiple sources, including Amazon DynamoDB, AWS Lambda, and HTTP APIs.

  • Amazon Cognito provides authentication, authorization, and user management for web and mobile apps.

  • Amazon DynamoDB is a fully managed NoSQL database service that provides fast, predictable, and scalable performance.

Code

The code for the sample application that’s used in this pattern is available in the GitHub aws-amplify-react-native-ios-todo-app repository. To use the sample files, follow the instructions in the Epics section of this pattern.

Epics

TaskDescriptionSkills required

Set up a React Native development environment.

For instructions, see Setting up the development environment in the React Native documentation.

App developer

Create and run the ToDoList React Native mobile app in iOS Simulator.

  1. Create a new React Native mobile app project directory in in your local environment by running the following command in a new terminal window:

    npx react-native init ToDoListAmplify

  2. Navigate to the project’s root directory by running the following command:

    cd ToDoListAmplify

  3. Run the app by running the following command:

    npx react-native run-ios

App developer
TaskDescriptionSkills required

Create the backend services needed to support the app in Amplify.

  1. In your local environment, run the following command from the project’s root directory (ToDoListAmplify):

    amplify init

  2. A prompt appears that asks you to provide information about the app. Enter the required information based on your use case. Then, press Enter.

For the ToDoList app setup used in this pattern, apply the following example configuration.

Example React Native Amplify app configuration settings

? Name: ToDoListAmplify ? Environment: dev ? Default editor: Visual Studio Code ? App type: javascript ? Javascript framework: react-native ? Source Directory Path: src ? Distribution Directory Path: / ? Build Command: npm run-script build ? Start Command: npm run-script start ? Select the authentication method you want to use: AWS profile ? Please choose the profile you want to use: default

For more information, see Create a new Amplify backend in the Amplify Dev Center documentation.

Note: The amplify init command provisions the following resources by using AWS CloudFormation

  • AWS Identity and Access Management (IAM) roles for authenticated and unauthenticated users (Auth Role and Unauth Role)

  • An Amazon Simple Storage Service (Amazon S3) bucket for deployment (for this pattern’s example app, Amplify-meta.json)

  • A backend environment in Amplify Hosting

App developer
TaskDescriptionSkills required

Create an Amazon Cognito authentication service.

  1. In your local environment, run the following command from the project’s root directory (ToDoListAmplify):

    amplify add auth

  2. A prompt appears that asks you to provide information about the authentication service’s configuration settings. Enter the required information based on your use case. Then, press Enter.

For the ToDoList app setup used in this pattern, apply the following example configuration.

Example authentication service configuration settings

? Do you want to use the default authentication and security configuration? \ Default configuration ? How do you want users to be able to sign in? \ Username ? Do you want to configure advanced settings? \ No, I am done

Note: The amplify add auth command creates the necessary folders, files, and dependency files in a local folder (amplify) within the project’s root directory. For the ToDoList app setup used in this pattern, the aws-exports.js is created for this purpose.

App developer

Deploy the Amazon Cognito service to the AWS Cloud.

  1. From the project’s root directory, run the following Amplify CLI command:

    amplify push

  2. A prompt to confirm deployment appears. Enter Yes. Then, press Enter.

Note: To see the deployed services in your project, go to the Amplify console by running the following command:

amplify console

App developer

Install the required Amplify libraries for React Native and the CocoaPods dependencies for iOS.

  1. Install the required Amplify open-source client libraries by running the following command from the project’s root directory:

    npm install aws-amplify aws-amplify-react-native \ amazon-cognito-identity-js @react-native-community/netinfo \ @react-native-async-storage/async-storage

  2. Install the required CocoaPods dependencies for iOS by running the following command:

    npx pod-install

App developer

Import and configure the Amplify service.

In the app’s entry point file (for example, App.js), import and load the Amplify service’s configuration file by entering the following lines of code:

import Amplify from 'aws-amplify' import config from './src/aws-exports' Amplify.configure(config)

Note: If you receive an error after importing the Amplify service in the app’s entry point file, stop the app. Then, open XCode and select the ToDoListAmplify.xcworkspace from the project’s iOS folder and run the app.

App developer

Update your app's entry point file to use the withAuthenticator Higher-order component (HOC).

Note: The withAuthenticator HOC provides sign-in, sign-up, and forgot password workflows in your app by using only a few lines of code. For more information, see Option 1: Use pre-build UI components in the Amplify Dev Center. Also, Higher-order components in the React documentation.

  1. In the app’s entry point file (for example, App.js), import the withAuthenticator HOC by entering the following lines of code:

    import { withAuthenticator } from 'aws-amplify-react-native'

  2. Export the withAuthenticator HOC by entering the following code:

    export default withAuthenticator(App)

withAuthenticator HOC code example

import Amplify from 'aws-amplify' import config from './src/aws-exports' Amplify.configure(config) import { withAuthenticator } from 'aws-amplify-react-native'; const App = () => { return null; }; export default withAuthenticator(App);

Note: In iOS Simulator, the app shows the login screen that’s provided by the Amazon Cognito service.

App developer

Test the authentication service setup.

In iOS Simulator, do the following:

  1. Create a new account in the app by using a real email address. A verification code is then sent to the registered email.

  2. Verify the account set up by using the code that you receive in the verification email.

  3. Enter the username and password that you created. Then, choose Sign In. A welcome screen appears.  

Note: You can also open the Amazon Cognito console and check if a new user has been created in the Identity Pool or not.

App developer
TaskDescriptionSkills required

Create an AWS AppSync API and DynamoDB database.

  1. Add an AWS AppSync API to your app and automatically provision a DynamoDB database by running the following Amplify CLI command from the project’s root directory:

    amplify add api

  2. A prompt appears that asks you to provide information about the API and database configuration settings. Enter the required information based on your use case. Then, press Enter. The Amplify CLI opens the GraphQL schema file in your text editor.

For the ToDoList app setup used in this pattern, apply the following example configuration.

Example API and database configuration settings

? Please select from one of the below mentioned services: \ GraphQL ? Provide API name: todolistamplify ? Choose the default authorization type for the API \ Amazon Cognito User Pool Do you want to use the default authentication and security configuration ? Default configuration How do you want users to be able to sign in? \ Username Do you want to configure advanced settings? \ No, I am done. ? Do you want to configure advanced settings for the GraphQL API \ No, I am done. ? Do you have an annotated GraphQL schema? \ No ? Choose a schema template: \ Single object with fields (e.g., “Todo” with ID, name, description) ? Do you want to edit the schema now? \ Yes

Example GraphQL schema

type Todo @model { id: ID! name: String! description: String }
App developer

Deploy the AWS AppSync API.

  1. In the project’s root directory, run the following Amplify CLI command:

    amplify push

  2. A prompt appears that asks you to provide more information about the API and database configuration settings. Enter the required information based on your use case. Then, press Enter. Your app can now interact with the AWS AppSync API. 

For the ToDoList app setup used in this pattern, apply the following example configuration.

Example AWS AppSync API configuration settings

Note: The following configuration creates the GraphQL API in AWS AppSync and a Todo table in Dynamo DB.

? Are you sure you want to continue? Yes ? Do you want to generate code for your newly created GraphQL API Yes ? Choose the code generation language target javascript ? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/**/*.js ? Do you want to generate/update all possible GraphQL operations - \ queries, mutations and subscriptions Yes ? Enter maximum statement depth \ [increase from default if your schema is deeply nested] 2
App developer

Connect the app's frontend to the AWS AppSync API.

To use the example ToDoList app provided in this pattern, copy the code from the App.js file in the aws-amplify-react-native-ios-todo-app GitHub repository. Then, integrate the example code into your local environment.

The example code provided in the repository’s App.js file does the following:

  • Shows the form for creating a ToDo Item with Title and Description fields

  • Displays the list of to-do items (Title and Description)

  • Posts and fetches data by using aws-amplify methods

App developer

Related resources