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.
-
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-appCreate the web app first, because
create-next-appand similar tools refuse a folder thataws amplifygen3 local inithas written to. -
Add a backend to the app with the following command:
aws amplifygen3 local init --type fullstackThe command writes
amplify.jsoncwith adeployblock for your site and abackendblock. It also creates themigrations/and.secrets/folders, and it installs the client SDK. To skip the installation, add--skip-install. Include--type fullstackeach time you runaws amplifygen3 local init, because without it the command writes thebackendblock commented out. -
Start your app and its backend on your computer:
aws amplifygen3 local runThe command prints the address of the local console, where you can see your tables and users.
-
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. -
Commit
amplify.jsoncandmigrations/. Don’t commit.secrets/oramplify.local.json. They hold your local database password and this computer’s local state. -
Deploy to the
previewenvironment. 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.
-
Confirm that the app has no backend. The output shows
"Backend": null.aws amplifygen3 app get --app my-app --output json -
Copy any comments, and the top-level
routesandspa_fallbacksettings, out ofamplify.jsonc. The next step rewrites the file: it keeps onlyname,env, anddeploy, and it adds thebackendblock. Routes inside anenvblock stay, as part ofenv. -
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 noamplify.jsoncfile, runaws amplifygen3 local init --type fullstack --app my-appinstead, with the deployed app’s name. Without--app, the command names the app after the folder, and the next deployment creates a second app. -
Write your tables as migrations, each with its grants and RLS policies, and try them with
aws amplifygen3 local run. -
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
previewdoes by default. To turn them off inpreview, runaws amplifygen3 env edit --env preview --deploy-on-fork-pull-request disable. -
Create the backend in one of the following ways. A deployment that a git push starts doesn’t create a backend, even when the
backendblock is committed.-
Run
aws amplifygen3 deploy. It creates the backend and applies your migrations in the same deployment topreview. If the app deploys from git, then commit and pushamplify.jsoncandmigrations/, 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 pushamplify.jsoncandmigrations/. The deployment from that push applies your migrations.
-
-
Check the backend.
aws amplifygen3 app get --app my-app --output jsonshowsBackend.StatusasREADY. Afteraws amplifygen3 app edit --backend add, it showsCREATINGfirst. A request to<AMPLIFY_BACKEND_URL>/healthreturns200, andaws amplifygen3 migration listshows 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
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.
-
List your environments with
aws amplifygen3 env list, and then each environment’s branches withaws amplifygen3 branch list --env preview, so that you know what you will delete. -
Save the data that you want to keep. Run the following command for each environment, and add
--branch-arnfor 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 -
Remove the code that calls the backend, and deploy it to every environment while the
backendblock is still inamplify.jsonc. A site that still calls the backend fails once the backend is gone. -
Comment out the
backendblock inamplify.jsonc. If the app deploys from git, commit the change, because a lateraws amplifygen3 deployfrom a checkout that still has the block adds a new, empty backend. This step alone deletes nothing, andaws amplifygen3 deploywarns you that the app still has a backend. -
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-destroyIn 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--envnames,previewby default. To delete the backend without a deployment, such as for an app that deploys from git, runaws amplifygen3 app edit --app my-app --backend remove --allow-destroyinstead. That command deletes the backend at once, without asking you to confirm. -
Check that
aws amplifygen3 app get --app my-app --output jsonshows"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.