View a markdown version of this page

How the Amplify backend works - AWS Amplify

How the Amplify backend works

An Amplify backend has three parts: a database, auth, and a data API. Your app calls them with the client SDK. Each environment of your app keeps its data in one or more backend branches. A backend branch has its own database, auth, and schema.

Database

Each backend branch has its own PostgreSQL 17 database. You connect to it with psql, Drizzle, Prisma, or another PostgreSQL client, and you query it with SQL.

You write your tables as SQL migrations in the migrations/ folder of your repository. Each deployment applies the new ones. To use an extension such as vector for embeddings, create it in a migration. You set each environment’s maximum database capacity and backup retention.

To export your data, run aws amplifygen3 db dump. To recover from a mistake, restore production to a time in its backup retention window. The restore creates a new backend branch, and production stays as it is.

For more information, see Connect to your database and Tables and migrations.

Auth

Auth gives your app authentication, authorization, and user management, built into your backend. Your users sign up and sign in with email and password, or with a social provider such as Google, Apple, GitHub, or Microsoft. You can also connect an existing Amazon Cognito user pool as a provider. AWS Amplify runs auth for your backend on managed Better Auth.

You set the sign-in methods and trusted origins in the backend.auth block of amplify.jsonc, next to your code. In your app, the client SDK signs users in and keeps them signed in. To give users sign-in screens without building them, turn on the managed sign-in page. It’s available in English, Spanish, or Simplified Chinese.

For more information, see Auth.

Data API

Amplify generates the data API from your database schema. You create a table, grant access to it, and your app reads and writes it over HTTPS from browser or server code. The data API is PostgREST-compatible, so it accepts PostgREST query syntax.

Row-level security (RLS) checks every request. Browser code sends the publishable key, so its requests run as a visitor or as the signed-in user. Server code signs its requests with AWS credentials and gets full access through AWS Identity and Access Management (IAM) permissions.

To generate TypeScript types for your tables, run aws amplifygen3 local gen-types --output-file src/db.types.ts.

For more information, see Data API overview and API keys and roles.

Client SDK

Your app calls the backend with the client SDK, @aws-amplifygen3/backend-client-js, in browser code and in server code. createClient connects to the backend with the backend URL and the publishable key. The SDK signs users in, keeps their session, and reads and writes rows through the data API. For more information, see the client SDK README on GitHub.

Branching

Each environment, such as preview, staging, or production, keeps its data in one or more backend branches with your schema applied: production in the production environment, source-upload for CLI deployments to any other environment, and one backend branch for each Git branch that the environment deploys. A backend branch has its own database, users, publishable key, and URL. The first deployment to a backend branch creates it and applies your migrations. Deployments copy no rows or users between backend branches, so the test data in preview stays out of production.

To fill a backend branch with test data, run your seed file with aws amplifygen3 backend seed. Reference data that every backend branch needs, such as a list of categories, goes in a migration.

For more information, see Environments and Seed data.

Request flow

  1. Your app creates the client SDK with the backend URL and the publishable key. Each deployment gives your app both values as the environment variables AMPLIFY_BACKEND_URL and AMPLIFY_BACKEND_PUBLISHABLE_KEY.

  2. A user signs in through auth. The SDK keeps the session and adds the user’s token to each request.

  3. The data API checks the key and the token, and runs the query as the authenticated database role, so auth.uid() in a policy returns the user’s ID. A request without a token runs as anon.

  4. The database applies your RLS policies and returns only the rows that they allow for that user.

Warning

Your backend URL and publishable key are public. Anyone who opens your website can read both values and send requests with them. Your grants and RLS policies are what limit those requests, so write policies for every table that you grant access to. For more information, see Secure your data.

Build locally, then deploy

The aws amplifygen3 local run command starts a local backend in Docker or Finch, with its own database, auth, and data API, plus your app’s dev server and a local console. When your app is ready, aws amplifygen3 deploy creates the backend in AWS, applies your migrations and builds your website. Deploying copies no rows or users from your computer.

To add a backend to your app or remove one, see Add or remove a backend. For more information about local development, see Develop locally.