Page updated Jan 16, 2024

Environment variables

Amplify CLI allows you to configure environment variables for your Lambda functions. Each Amplify environment can have a different environment variable value. This enables use cases such as switching between dev and prod URLs depending on the environment.

Environment variables should NOT be used for storing sensitive configuration values such as database passwords, API keys, or access tokens. Use function secrets configuration instead!

Configuring environment variables

To configure a new function with environment variables, run amplify add function, select yes to the advanced settings prompt and select yes to the environment variables configuration prompt. From there, you will be able to specify a key and value for the environment variable.

1$ amplify add function
3? Do you want to configure advanced settings? Yes
5? Do you want to configure environment variables for this function? Yes
6? Enter the environment variable name: API_URL
7? Enter the environment variable value:
8? Select what you want to do with environment variables: (Use arrow keys)
9 Add new environment variable
10 Update existing environment variables
11 Remove existing environment variables
12> I'm done

To configure environment variables for an existing function, run amplify update function, and select Environment variables configuration. You can then add, update, or remove environment variables.

1$ amplify update function
3? Which setting do you want to update?
4 Resource access permissions
5 Scheduled recurring invocation
6 Lambda layers configuration
7> Environment variables configuration
8 Secret values configuration
9? Select what you want to do with environment variables:
10> Add new environment variable
11 Update existing environment variables
12 Remove existing environment variables
13 I'm done

Multi-environment flows

When creating a new Amplify environment using amplify env add, Amplify CLI asks if you want to apply all environment variable values to the new environment or modify them. If you choose to apply the existing values, you can still make edits anytime by running amplify update function.

When creating a new Amplify environment using amplify env add --yes, Amplify CLI will apply all environment variable values from the current environment to the new environment.

In multi-environment workflows, you may have added a new environment variable in one Amplify environment and then checked out a different Amplify environment. In this case, on the next amplify push, Amplify CLI will detect that there is a new environment variable that does not have a value specified in the current environment and prompt for one. Running amplify push --yes in this case will fail with a message explaining the missing environment variable values.

In git-based multi-environment workflows, you may run into errors during deployment. For example, this happens when you add an environment variable in envA (corresponding to a git branch branchA), then amplify checkout envB and git checkout branchB and git merge branchA into branchB. Upon pushing envB, Amplify CLI detects that a new environment variable has been added but can't infer a value for it. To resolve this issue, run the following commands in the terminal:

  1. amplify env checkout <failing env name>
  2. amplify push - when prompted, enter a new value for the environment variable(s)
  3. git commit
  4. git push