

# Local development workflow

Develop on your computer first, and deploy when the change works. You apply a migration on the local backend, try it in your app and reset the data as often as you need, with no deployment in between. Deploy to `preview` when you want to share the change or try it in AWS.

Start in one of the following ways, and then follow [the daily loop](#the-daily-loop):
+  ** [Start a new app](#start-a-new-app) ** – You’re adding a backend to an app on your computer.
+  ** [Work on an existing app](#work-on-an-existing-app) ** – You cloned an app that already has a backend.

## Before you begin
<a name="_before_you_begin"></a>

You need the following:
+ Docker or Finch, running. Docker is the default. For more information about Finch, see [Docker and Finch](backend-local-runtimes.md).
+ Node.js and your package manager, for your app’s dev server.
+ A network connection the first time you run `aws amplifygen3 local run`, to download the local backend image. `local init --type fullstack` also needs one in a folder that has a `package.json`, to install the client SDK packages. To skip the install, add `--skip-install`.

Run each `local` command from your app’s folder, the one that holds `amplify.jsonc`. The local commands need no AWS credentials. You need AWS credentials to deploy.

## Start a new app
<a name="start-a-new-app"></a>

Follow these steps to add a backend to a new app:

1. Create your app with its framework’s tool, such as `create-next-app`.

1. Run the following command in the app’s folder to add a backend:

   ```bash
   aws amplifygen3 local init --type fullstack
   ```

   The command writes `amplify.jsonc` with a `backend` block, and creates `migrations/`, `.secrets/` and `amplify.local.json`. If your app has a `package.json`, it also adds the Amplify backend client packages. For an app that already has an `amplify.jsonc` file, run `aws amplifygen3 local backend add` instead.

1. Install your app’s dependencies, such as with `npm install`.

1. Run the following command to start the local backend and your app’s dev server:

   ```bash
   aws amplifygen3 local run
   ```

   The command starts the backend, applies your migrations, and starts the dev server in the foreground. You should see the backend URL, the local publishable key `hpk_v1.local`, and the addresses of the local console and Mailpit. `local run` passes `AMPLIFY_BACKEND_URL` and `AMPLIFY_BACKEND_PUBLISHABLE_KEY` to the dev server. AWS Amplify sets the same variables in every deployment. A dev server that you start another way gets neither variable.

**Important**  
Pass `--type fullstack` each time you run `local init` in a project with a backend. Without it, the command comments out the `backend` block. `local init` and `local backend add` rewrite `amplify.jsonc` with only `name`, `env`, `deploy`, and `backend`. They drop your comments and every other top-level key, such as `routes`, `cache_key`, and `spa_fallback`, without a warning, so save a copy of the file first. With `--force`, the command rewrites `amplify.jsonc` and `amplify.local.json`, and you lose your edits to both, including `backend.auth`. The app name resets to the `--app` value or the folder name.

To stop only the dev server, press Ctrl\+C in the `local run` terminal. The backend keeps running. To stop the backend and the dev server, run `aws amplifygen3 local stop` from any terminal.

## Work on an existing app
<a name="work-on-an-existing-app"></a>

After you clone an app that already has a backend, install its dependencies, and run `aws amplifygen3 local run`. The command creates a local backend for your copy and applies the migrations. To load the sample data in `seed.sql`, run `aws amplifygen3 local backend seed`.

Each copy of your project folder, such as a clone or a Git worktree, has its own local backend.

## What to commit
<a name="_what_to_commit"></a>

The following table shows which project files to commit:


| File or folder | Commit it | What it holds | 
| --- | --- | --- | 
|  `amplify.jsonc`  | Yes | Your app’s settings, including the `auth` block for the local backend and every environment | 
|  `migrations/`  | Yes | Your schema, as SQL migration files | 
|  `seed.sql`  | Yes, if you use one | Sample data for the local backend | 
|  `amplify.local.json`  | No | This computer’s local backend name, ports and addresses | 
|  `.secrets/`  | No | The local database password and your client secrets | 

 `local init` adds `amplify.local.json` and `.secrets/` to `.gitignore`.

## The daily loop
<a name="the-daily-loop"></a>

The following steps add a table on the local backend and deploy it to `preview`:

1. Run the following command to create a migration file, and then write the table, its grants and its row-level security (RLS) policies in it:

   ```bash
   aws amplifygen3 local migration create --description "create notes"
   ```

1. Run the following command to apply the migration to the local backend:

   ```bash
   aws amplifygen3 local migration apply
   ```

   Run `aws amplifygen3 local migration list` to check that no migration is pending.

1. Try the change in your app, and test your RLS policies as a signed-in user and as a visitor. For more information, see [Local database](backend-local-database.md).

1. Commit `amplify.jsonc` and `migrations/`.

1. Run the following command to deploy to `preview`:

   ```bash
   aws amplifygen3 deploy
   ```

   The deployment applies the new migrations to the `preview` environment. After you check `preview`, deploy to `production` with `aws amplifygen3 deploy -e production`.

For more information about migrations and RLS policies, see [Tables and migrations](backend-data.md).

After you pull new migrations with Git, `local run` applies them as it starts. After you change `amplify.jsonc`, `.secrets/client_secrets.env`, or a file in `email-templates/`, press Ctrl\+C in the `local run` terminal, and then run `aws amplifygen3 local run` again. `local run` restarts the backend with the new settings and keeps your data. If the backend can’t start with the new settings, it keeps running with the previous ones.

Stop the first `local run` before you start it again. If the first dev server still holds its port, a second `local run` starts the dev server on the next port. Next.js 16 refuses a second dev server in the same folder.

When you switch Git branches in one folder, the local backend keeps the same database. To keep separate local data for each Git branch, use one Git worktree for each branch.

## Commands at a glance
<a name="_commands_at_a_glance"></a>

The `local` commands act on the local backend. Each task has a deployed command too. The deployed commands sign with your AWS credentials and take `--env` to name the environment.


| Task | Local backend | Deployed environment | 
| --- | --- | --- | 
| Run SQL, dump, lint and inspect |  `local db query`, `local db dump`, `local db lint`, `local db inspect`  |  `db query`, `db dump`, `db lint`, `db inspect`  | 
| Apply migrations |  `local migration apply`  |  `deploy`  | 
| List applied migrations |  `local migration list`  |  `migration list`  | 
| Manage users |  `local backend user create`, `list`, `get`, `edit`, `delete`  |  `backend user create`, `list`, `get`, `edit`, `delete`  | 
| Load seed data |  `local backend seed`  |  `backend seed`  | 
| Generate TypeScript types |  `local gen-types`  |  `backend gen-types`  | 
| Read logs |  `local logs`  |  `logs get`  | 

The `db` and `backend user` commands act on `preview` when you leave out `--env`. Local users are separate from the users in every environment.

## Check the local backend
<a name="_check_the_local_backend"></a>

Run the following command to see the state of each service, the backend URL and the local console address:

```bash
aws amplifygen3 local status
```

The status of each service is `healthy`, `disabled`, `unhealthy` or `unreachable`. To print only the backend URL, such as for a script, add `--output text --query data.endpoint`.

Run the following command to read the backend’s output:

```bash
aws amplifygen3 local logs --tail 200
```

Add `--follow` to stream the output, `--log-type database` for the PostgreSQL logs, or `--log-type backend-access` for the request logs. To find one request, add `--request-id` with the value of the response’s `x-request-id` header. The `--request-id` option requires `--log-type backend-access`.

## Stop and clean up
<a name="_stop_and_clean_up"></a>

Run the following command from any terminal to stop the backend and the dev server that `local run` started:

```bash
aws amplifygen3 local stop
```

Your local data stays, and the next `local run` starts with it.

To delete the local backend and its data, run the following command:

```bash
aws amplifygen3 local destroy
```

On a terminal, the command lists what it deletes and asks you to confirm. In a script, it prints the list and exits with status `2`, and it deletes nothing. To delete without the prompt, add `--yes`.

**Warning**  
 `local destroy` deletes all local data: your tables, rows and local users, and the emails in Mailpit. It keeps your project files, such as `migrations/`, `.secrets/` and `amplify.local.json`.

To keep your schema and delete only the data, reset the local database. For more information, see [Local database](backend-local-database.md).

Before you delete a copy of your project folder, run `aws amplifygen3 local destroy` in it. Otherwise its containers and data stay in Docker or Finch.

## Use the local commands in scripts
<a name="_use_the_local_commands_in_scripts"></a>

When the output isn’t a terminal, the `local` commands print JSON. On a terminal, add `--output json` to get the same JSON. A result has `"ok": true` and a `data` object on standard output. An error has `"ok": false` and an `error` object with `kind`, `message` and `resolution` on standard error. `--query` filters the result, starting at `data`, such as `--query data.endpoint`.

The commands exit with the following status:


| Exit status | Meaning | 
| --- | --- | 
|  `0`  | The command succeeded. | 
|  `1`  | The command failed. Read `kind` and `resolution` in the error. | 
|  `2`  | The command needs confirmation, and it changed nothing. `local destroy` and `local reset` return it without `--yes`. | 
|  `252`  | An argument is wrong, such as an unknown flag. | 

 `local run` exits with the dev server’s exit code when the dev server stops with an error.

## `local run` options
<a name="_local_run_options"></a>

 `local run` takes the following options:


| Option | What it does | 
| --- | --- | 
|  `--port <port>`  | The backend URL’s port. The default is `3001`, or the next free port. | 
|  `--db-port <port>`  | The PostgreSQL port. The default is `5432`, or the next free port. | 
|  `--gui-port <port>`  | The local console’s port. The default is `54323`, or the next free port. | 
|  `--mail-port <port>`  | The port of Mailpit’s web UI and API. The default is `8025`, or the next free port. | 
|  `--rebuild`  | Downloads the current local backend image again, and recreates the backend from it. | 
|  `--skip-migrations`  | Starts the backend without applying pending migrations. | 

 `local run` reuses the same ports on later runs while they’re free. When you set a port with an option, `local run` always uses that port, and it fails if another program uses the port. Every port listens only on your computer.

## Errors
<a name="_errors"></a>

The local commands return the following errors:


|  `kind`  | Cause | What to do | 
| --- | --- | --- | 
|  `not_initialized`  | The folder has no `amplify.jsonc`, or no `amplify.local.json`. | Run the command from your app’s folder. In a new project, run `local init --type fullstack`. In a clone, run `local run`. | 
|  `not_running`  | The local backend isn’t running. | Run `aws amplifygen3 local run`. | 
|  `container_runtime_not_found`  | Docker or Finch isn’t installed or isn’t running. | Start Docker or Finch, and run the command again. For more information, see [Docker and Finch](backend-local-runtimes.md). | 
|  `port_in_use`  | A port that you set with an option is busy, or the dev server’s port is one that the backend uses. | Leave out the option, set another port, or change the dev server’s port. | 
|  `ports_differ`  | A port option names another port than the running backend uses. Nothing changed. | To move the backend, run `local stop`, and then run `local run` with the option again. | 
|  `migration_failed`  | A migration failed as `local run` applied it. The message names the file and the PostgreSQL error. | Fix the file, and run `aws amplifygen3 local migration apply`. The backend keeps running. | 
|  `config_not_applied`  | The backend couldn’t start with the changed `amplify.jsonc` or `.secrets/client_secrets.env`, and it kept the previous settings. | Fix what the message quotes, and run `local run` again. | 
|  `stack_exited`  | The backend stopped while it was starting. The message quotes the first error. | Fix what the message names, and run `local run` again. `local logs` has the full output. | 