

# Environment variables

**Welcome to the new AWS Amplify**  
Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.  
Looking for Amplify Hosting? See the [Amplify Hosting User Guide](https://docs.aws.amazon.com/amplify/hosting/userguide/welcome.html).

Environment variables are key-value pairs that configure your AWS Amplify app for each environment. Your app can use them both when it builds and when it runs. Use them for configuration values, feature flags, API keys, and anything else that you don’t want to hardcode in your source code.

Each variable belongs to one environment, such as `preview` or `production`. A variable can be a plain variable or a secret. A secret’s value can’t be read back after you set it. For more information, see [Manage secrets](environment-secrets.md).

When you connect an integration to an environment, you receive that integration’s variables in that environment. For more information, see [Integrations](integrations.md).

For more information about creating variables, see [Set environment variables](setting-env-vars.md).

## Use different values in each environment
<a name="env-vars-environments"></a>

The same variable name can hold a different value in each environment. For example, your preview environment can use a sandbox API key while your production environment uses the live key.

Suppose your code reads the variable `PAYMENTS_API_URL`:

```javascript
const paymentsApiUrl = process.env.PAYMENTS_API_URL
```

The following AWS Command Line Interface (AWS CLI) commands set a value for each environment. Commands that don’t include `--env` apply to the `preview` environment:

```bash
# preview environment
aws amplifygen3 var set PAYMENTS_API_URL https://sandbox.payments.example.com

# production environment
aws amplifygen3 var set PAYMENTS_API_URL https://payments.example.com --env production
```

A change to a variable takes effect on the next deployment of that environment. For instructions on creating a new deployment, see [Create a new deployment](deploy-your-app-deployments.md).

## Use variables in your code
<a name="customize-your-app-env-vars-access"></a>

You can read both variables and secrets through `process.env` in your server-side code. You read a secret the same way that you read a plain variable:

```ts
// Plain variable
const paymentsApiUrl = process.env.PAYMENTS_API_URL

// Secret, read the same way
const stripeKey = process.env.STRIPE_SECRET_KEY
```

You can use variables in server components, API routes, server actions, and loaders. These variables aren’t available in the code that runs in the browser. To use a value in browser code, pass it in through your framework at build time, for example with the `NEXT_PUBLIC_` prefix in Next.js. Don’t pass a secret to browser code.

### Backend variables
<a name="customize-your-app-env-vars-backend"></a>

If your app has a backend, every deployment also sets the following variables. This happens both when your app builds and when it runs. You don’t set them yourself.


| Variable | Value | 
| --- | --- | 
|  `AMPLIFY_BACKEND_URL`  | The URL of the environment’s backend | 
|  `AMPLIFY_BACKEND_PUBLISHABLE_KEY`  | The publishable key that your app sends with each backend request | 
|  `AMPLIFY_BACKEND_BRANCH_ARN`  | The Amazon Resource Name (ARN) of the environment’s backend | 
|  `AMPLIFY_BACKEND_PUBLIC_URL`  | The same URL as `AMPLIFY_BACKEND_URL`  | 
|  `AMPLIFY_APP_URL`  | The environment’s Amplify-assigned `https://` origin, not a custom domain. `production` and custom-environment deployments have it, and `preview` deployments don’t | 
|  `AMPLIFY_DATABASE_HOST`, `AMPLIFY_DATABASE_PORT`, `AMPLIFY_DATABASE_NAME`  | The branch’s PostgreSQL connection. Set together, when the branch has one. For more information, see [Connect to your database](backend-database-connect.md)  | 

None of these variables is a secret, and a deployment never sets a secret key for your backend. The URLs and the publishable key are safe to pass to browser code. Use the branch ARN and the database variables only in server code. A deployment keeps the values it was built with until you deploy the environment again. If your environment has a variable without `--scope BACKEND` whose name is one that the deployment sets, the deployment fails with `BINDING_RESOLUTION_FAILED` and the reason `Customer env vars conflict with binding env vars: <name>`.

Your app gets these variables both while it builds and while it runs. When you develop locally, `aws amplifygen3 local run` gives the dev server that it starts `AMPLIFY_BACKEND_URL` and `AMPLIFY_BACKEND_PUBLISHABLE_KEY` for the local backend, unless your environment already sets them. A browser bundle gets a variable only when your framework exposes it, so pass the URL and the key to the client SDK in your code. For more information, see the [client SDK README](https://github.com/aws-amplifygen3/amplifygen3/blob/main/packages/client-sdk/js/README.md) on GitHub.

Another app that calls this backend doesn’t get these variables. Set this backend’s URL and publishable key as variables of that app.

The publishable key grants only what your row-level security rules allow. You can pass `AMPLIFY_BACKEND_URL` and `AMPLIFY_BACKEND_PUBLISHABLE_KEY` to browser code through your framework at build time.

To see the values for a deployment, run `amplifygen3 deployment get --id <deployment-id> --output json`. Replace `<deployment-id>` with the ID that `aws amplifygen3 deploy` printed. The values are in `EnvironmentVariables`. Run `amplifygen3` directly, because the outer `aws` command doesn’t pass `--output`.

## Names and limits
<a name="env-vars-limits"></a>
+ A name must start with a letter and contain only letters, numbers, and underscores. It can be up to 128 characters long.
+ The names and values of all the variables in one environment, together, can be up to 4 KB. When you deploy, the variables that connected integrations provide count toward this limit too, and a deployment over the limit fails.
+ You can’t use a name that the runtime reserves, such as `AWS_REGION`, `AWS_ACCESS_KEY_ID`, or `AWS_SECRET_ACCESS_KEY`.
+ You can’t use a name that an integration connected to the environment already provides. Choose a different name, or change the name that the integration uses for its variable.

## Best practices
<a name="env-vars-best-practices"></a>
+ Store passwords, API keys, tokens, and any other sensitive value as a secret.
+ Don’t commit `.env` files to version control. Use `aws amplifygen3 var upload` to set the values in Amplify instead.
+ Keep preview and production values separate. Don’t use live credentials in a test environment.
+ Deploy again after you change a variable.
+ When you change a secret’s value, pass `--secret` again. `aws amplifygen3 var set` without `--secret` stores the new value as a plain variable, whose value `aws amplifygen3 var get` shows.

**Don’t expose secrets to browser code**  
Never give a secret a name that starts with `NEXT_PUBLIC_`, `VITE_`, or `PUBLIC_`. Frameworks build those variables into the JavaScript that every visitor downloads. Read a secret only in server code.

## Variables that sign-in uses
<a name="env-vars-sign-in"></a>

If your app’s backend uses sign-in with a social provider, Amplify stores the provider’s client secret as a secret variable. You set it on the **Authentication** page. Change or remove that variable on the **Authentication** page, not with the steps on this page. For more information, see [Authentication](backend-auth.md).

In `amplify.jsonc`, a secret in `backend.auth`, such as a social provider’s `clientSecret` or an SMTP password, is only a reference in the form `${env:NAME}`. Store the value as a backend secret in each environment, and then deploy:

```bash
printf %s "$GOOGLE_CLIENT_SECRET" | aws amplifygen3 var set GOOGLE_CLIENT_SECRET --secret --scope BACKEND --env production
```

With `--scope BACKEND`, only your backend gets the value. Your app’s build and its runtime don’t. If a reference has no backend variable with a value, the deployment fails and names the variable. For more information, see [Auth configuration](backend-auth-config.md).

**Set backend secrets with --scope BACKEND**  
Without `--scope`, a new variable gets the scope `ALL`, so your build and your app’s runtime also receive the secret, and the deployment refuses a `backend.auth` reference to it. `aws amplifygen3 var set` warns when it stores a referenced variable with the scope `ALL`. To move the variable, set it again with `--secret --scope BACKEND`.

**Never commit a secret’s value**  
Never put a secret’s value in `amplify.jsonc` or another file that you commit. Keep only the `${env:NAME}` reference there.

## Manage existing variables
<a name="customize-your-app-env-vars-manage"></a>

### View variables
<a name="customize-your-app-env-vars-view"></a>

You can see the name of every secret, but not its value.

#### View variables in the console
<a name="_view_variables_in_the_console"></a>

1. Sign in to the Amplify console.

1. Choose an app.

1. In the left navigation pane, choose **Environment variables**.

1. To see one environment’s variables, choose the environment in the filter.

#### View variables using the CLI
<a name="_view_variables_using_the_cli"></a>

The following AWS CLI command lists the variables for the `preview` environment. The command masks secret values:

```bash
aws amplifygen3 var list
```

The following AWS CLI command also lists the variables that integrations provide to the `production` environment:

```bash
aws amplifygen3 var list --env production --all
```

The following AWS CLI command reads one variable:

```bash
aws amplifygen3 var get PAYMENTS_API_URL
```

### Edit a variable
<a name="customize-your-app-env-vars-edit"></a>

You can change a variable’s value, or change whether it’s a secret. Deploy again for the change to take effect.

#### Edit a variable in the console
<a name="_edit_a_variable_in_the_console"></a>

You can’t edit a secret in the console. Delete the secret and add it again, or set a new value with the CLI. For instructions on changing or rotating a secret, see [Change or rotate a secret](environment-secrets.md#environment-secrets-change).

1. Sign in to the Amplify console.

1. Choose an app.

1. In the left navigation pane, choose **Environment variables**.

1. For the variable that you want to edit, choose the actions menu, and then choose **Edit**.

1. For **Value**, enter the new value.

   You can also make the variable a secret, and change the environments that it applies to.

1. Choose **Save**.

1. To deploy the change now, choose **Redeploy** in the message that appears, and then choose the environment to deploy.

#### Edit a variable using the CLI
<a name="_edit_a_variable_using_the_cli"></a>

To change a value, set the variable again. The following AWS CLI command changes the value of `PAYMENTS_API_URL` in the `production` environment:

```bash
aws amplifygen3 var set PAYMENTS_API_URL https://payments-v2.example.com --env production
```

Setting a secret without `--secret` makes it a plain variable. Setting a plain variable with `--secret` makes it a secret.

### Delete a variable
<a name="customize-your-app-env-vars-delete"></a>

Deploy again after you delete a variable for the change to take effect.

#### Delete a variable in the console
<a name="_delete_a_variable_in_the_console"></a>

1. Sign in to the Amplify console.

1. Choose an app.

1. In the left navigation pane, choose **Environment variables**.

1. Next to the variable, choose the actions menu, and then choose **Delete**.

#### Delete a variable using the CLI
<a name="_delete_a_variable_using_the_cli"></a>

The following AWS CLI command deletes `PAYMENTS_API_URL` from the `production` environment:

```bash
aws amplifygen3 var unset PAYMENTS_API_URL --env production
```

### Download variables
<a name="customize-your-app-env-vars-download"></a>

Use the CLI to download an environment’s variables to a `.env` file for local development. The file includes the variables that integrations provide. The CLI writes secrets with an empty value.

The following AWS CLI command writes the `preview` environment’s variables to `.env`, and replaces the file if it exists:

```bash
aws amplifygen3 var download
```

The following AWS CLI command writes the `production` environment’s variables to `.env.local`:

```bash
aws amplifygen3 var download .env.local --env production
```

## Next steps
<a name="_next_steps"></a>
+  [Set environment variables](setting-env-vars.md) 
+  [Manage secrets](environment-secrets.md) 