View a markdown version of this page

Add or remove a backend - AWS Amplify

Add or remove a backend

You can add an AWS Amplify backend to a new app or to an app that you already deployed, and you can remove the backend later.

Create an app with a backend

The following steps create a web app with a backend, run both on your computer, and deploy them to AWS. Before you start, install Docker or Finch, and start it. For more information, see Develop locally.

  1. Create your web app with its framework’s tool, and go to its folder. For example, to create a Next.js app, run the following commands:

    npx create-next-app@latest my-app cd my-app

    Create the web app first, because create-next-app and similar tools refuse a folder that aws amplifygen3 local init has written to.

  2. Add a backend to the app with the following command:

    aws amplifygen3 local init --type fullstack

    The command writes amplify.jsonc with a deploy block for your site and a backend block. It also creates the migrations/ and .secrets/ folders, and it installs the client SDK. To skip the installation, add --skip-install. Include --type fullstack each time you run aws amplifygen3 local init, because without it the command writes the backend block commented out.

  3. Start your app and its backend on your computer:

    aws amplifygen3 local run

    The command prints the address of the local console, where you can see your tables and users.

  4. In another terminal, write your tables as migrations, apply them with aws amplifygen3 local migration apply, and try them in your app. For more information, see Tables and migrations.

  5. Commit amplify.jsonc and migrations/. Don’t commit .secrets/ or amplify.local.json. They hold your local database password and this computer’s local state.

  6. Deploy to the preview environment. The first deployment creates the app and its backend in AWS, applies your migrations and builds your site.

    aws amplifygen3 deploy

When you’re ready, deploy to production with aws amplifygen3 deploy --env production. Production gets its own database, with your tables but none of the rows or users from preview. For a tutorial that also signs a user in and reads and writes rows, see Tutorial: Create a full-stack app.

Add a backend to a deployed app

You add a backend to the app that you already have. Don’t delete the app and create it again.

  1. Confirm that the app has no backend. The output shows "Backend": null.

    aws amplifygen3 app get --app my-app --output json
  2. Copy any comments, and the top-level routes and spa_fallback settings, out of amplify.jsonc. The next step rewrites the file: it keeps only name, env, and deploy, and it adds the backend block. Routes inside an env block stay, as part of env.

  3. In the folder that the app deploys from, run aws amplifygen3 local backend add, and then put back what you copied. If the folder has no amplify.jsonc file, run aws amplifygen3 local init --type fullstack --app my-app instead, with the deployed app’s name. Without --app, the command names the app after the folder, and the next deployment creates a second app.

  4. Write your tables as migrations, each with its grants and RLS policies, and try them with aws amplifygen3 local run.

  5. If the app deploys from git, turn off fork pull request deployments in each environment that has them on. Amplify refuses to add a backend while any environment deploys fork pull requests, and preview does by default. To turn them off in preview, run aws amplifygen3 env edit --env preview --deploy-on-fork-pull-request disable.

  6. Create the backend in one of the following ways. A deployment that a git push starts doesn’t create a backend, even when the backend block is committed.

    • Run aws amplifygen3 deploy. It creates the backend and applies your migrations in the same deployment to preview. If the app deploys from git, then commit and push amplify.jsonc and migrations/, so that later pushes apply your migrations.

    • If the app deploys from git, run aws amplifygen3 app edit --app my-app --backend add. The command creates the backend without a deployment. Then commit and push amplify.jsonc and migrations/. The deployment from that push applies your migrations.

  7. Check the backend. aws amplifygen3 app get --app my-app --output json shows Backend.Status as READY. After aws amplifygen3 app edit --backend add, it shows CREATING first. A request to <AMPLIFY_BACKEND_URL>/health returns 200, and aws amplifygen3 migration list shows each migration as applied.

The first deployment to each backend branch waits as Queued while the branch’s database is created. For more information, see Create a new deployment.

Your site’s own Amplify URLs, its custom domains, and its recent preview URLs can call the backend from the browser without any setting, because each deployment tells the backend which addresses to trust. For a site that Amplify doesn’t host, such as a dev server that you start yourself, see Auth configuration. If you write a backend.auth block, keep "emailAndPassword": { "enabled": true } in it, because a block that you write replaces the default settings, and without that setting password sign-in is off.

Remove a backend

Warning: removing the backend deletes all of its data.

It deletes every branch in every environment, including production: each branch’s database, its automated backups, and its sign-in users. You can’t undo it, and you can’t bring your users back with their passwords.

  1. List your environments with aws amplifygen3 env list, and then each environment’s branches with aws amplifygen3 branch list --env preview, so that you know what you will delete.

  2. Save the data that you want to keep. Run the following command for each environment, and add --branch-arn for each branch of an environment that has more than one. The file holds your tables, not your sign-in users.

    aws amplifygen3 db dump --env production --output-file production.sql
  3. Remove the code that calls the backend, and deploy it to every environment while the backend block is still in amplify.jsonc. A site that still calls the backend fails once the backend is gone.

  4. Comment out the backend block in amplify.jsonc. If the app deploys from git, commit the change, because a later aws amplifygen3 deploy from a checkout that still has the block adds a new, empty backend. This step alone deletes nothing, and aws amplifygen3 deploy warns you that the app still has a backend.

  5. Run the following command. It lists every environment whose data it deletes and asks you to confirm. If you decline, it deletes and deploys nothing.

    aws amplifygen3 deploy --allow-destroy

    In a CI job or an agent session, the command stops before it deletes anything unless you also pass --yes. After you confirm, it deletes the backend and deploys your site without it to the environment that --env names, preview by default. To delete the backend without a deployment, such as for an app that deploys from git, run aws amplifygen3 app edit --app my-app --backend remove --allow-destroy instead. That command deletes the backend at once, without asking you to confirm.

  6. Check that aws amplifygen3 app get --app my-app --output json shows "Backend": null. Amplify then deletes the databases in the background.

Your local backend keeps its data until you run aws amplifygen3 local destroy, and migrations/, .secrets/ and amplify.local.json stay until you delete them. Until the old backend has finished deleting, Amplify refuses to add a new one, so wait a few minutes and try again. If you add a backend again later, it starts with new, empty databases, and your users sign up again. To delete the app and its backend together, see Create and delete an app.