Connecting Expo Apps to AWS Amplify Backend
Introduction
AWS Amplify simplifies backend integration with features like authentication, GraphQL APIs, and storage. This guide configures Amplify in Expo.
Prerequisites
- Expo project
- AWS account with Amplify CLI installed
Step 1: Install Amplify Libraries
npm install aws-amplify @aws-amplify/auth @aws-amplify/api-graphql
npm install -g @aws-amplify/cli
Step 2: Initialize Amplify Project
amplify init
# Choose default settings: JS, React Native, managed workflow
Step 3: Add Authentication
amplify add auth
# Default configuration
amplify push
Step 4: Configure Amplify in Expo
Create src/aws-exports.js
generated by Amplify.
Update App.js
:
import React, { useEffect } from 'react';
import { withAuthenticator } from 'aws-amplify-react-native';
import Amplify from 'aws-amplify';
import awsconfig from './src/aws-exports';
Amplify.configure({
...awsconfig,
Analytics: {
disabled: true, // Disable Analytics if not needed
},
});
function App() {
return (
// Your app UI
);
}
export default withAuthenticator(App);
Step 5: Add GraphQL API
amplify add api
# Choose GraphQL, default schema
amplify push
Use generated API
module:
import { API, graphqlOperation } from "aws-amplify";
import { createPost } from "./src/graphql/mutations";
import { listPosts } from "./src/graphql/queries";
export async function fetchPosts() {
const result = await API.graphql(graphqlOperation(listPosts));
return result.data.listPosts.items;
}
export async function addPost(title, content) {
await API.graphql(
graphqlOperation(createPost, { input: { title, content } })
);
}
Step 6: Add Storage (S3)
amplify add storage
# Content (Images), auth only
amplify push
Use Storage
module:
import { Storage } from "aws-amplify";
export async function uploadImage(uri) {
const response = await fetch(uri);
const blob = await response.blob();
const key = `images/${Date.now()}.png`;
await Storage.put(key, blob, { contentType: "image/png" });
return key;
}
Summary
AWS Amplify accelerates backend integration for authentication, GraphQL APIs, and storage in Expo apps with minimal boilerplate.