Manage secrets
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.
A secret is an environment variable whose value can’t be read back after you set it. Use secrets for API keys, database passwords, tokens, and any other value that must not be exposed.
Your app reads a secret through process.env, the same way that it reads a plain variable. For more information, see Use variables in your code.
AWS Amplify encrypts all of an environment’s variables, plain variables and secrets alike, with AWS Key Management Service (AWS KMS). A secret also differs from a plain variable in the following ways:
-
The console, the AWS Command Line Interface (AWS CLI), and the API never return a secret’s value.
aws amplifygen3 var listandaws amplifygen3 var getmask it. -
aws amplifygen3 var downloadwrites a secret with an empty value. -
The CLI, with
--secret, reads the value from a hidden prompt or from standard input in a script. This keeps the value out of your shell history.
A secret follows the same naming rules and size limit as any other variable. For more information, see Names and limits.
Create a secret
After you create a secret, deploy the environment again for the secret to take effect.
Create a secret in the console
To create a secret in the console, do the following:
-
Sign in to the Amplify console.
-
Choose an app.
-
In the left navigation pane, choose Environment variables.
-
Choose Add.
-
Choose Manual.
-
For Key, enter the secret’s name.
-
For Value, enter the secret’s value.
-
Select Secret.
-
For the environments, keep the ones that should get the secret.
-
Choose Save.
-
To deploy the change now, choose Redeploy in the message that appears, and then choose the environment to deploy.
Create a secret using the CLI
The following AWS CLI command creates the secret STRIPE_SECRET_KEY in the preview environment. The CLI asks for the value and hides what you type:
aws amplifygen3 var set STRIPE_SECRET_KEY --secret
The following AWS CLI command creates the secret in the production environment:
aws amplifygen3 var set STRIPE_SECRET_KEY --secret --env production
Don’t put the value on the command line. In a script or a continuous integration (CI) pipeline, pass the value through standard input instead:
echo "$STRIPE_SECRET_KEY" | aws amplifygen3 var set STRIPE_SECRET_KEY --secret --env production
To upload several secrets from a .env file, name them with --secrets. The other variables in the file are stored as plain variables:
aws amplifygen3 var upload .env.production --env production --secrets STRIPE_SECRET_KEY,DATABASE_PASSWORD
Change or rotate a secret
You can’t edit a secret in the console. To give a secret a new value in the console, delete it and add it again. With the CLI, set it again with --secret. Then deploy the environment again, so that your app uses the new value:
aws amplifygen3 var set STRIPE_SECRET_KEY --secret --env production
If you set a secret’s name again without --secret, the variable becomes a plain variable. Its value can then be read. To keep it a secret, always include --secret.
Delete a secret
In the console, choose Environment variables, choose the actions menu next to the secret, and then choose Delete. With the CLI, the following command deletes the secret from the production environment. Deploy again for the change to take effect:
aws amplifygen3 var unset STRIPE_SECRET_KEY --env production
Best practices for secrets
-
Use a different secret value in each environment. Don’t use a live credential in
preview. -
Don’t pass a secret to code that runs in the browser, for example through a
NEXT_PUBLIC_variable in Next.js. -
Don’t log the values that your app reads from
process.env. -
Rotate a secret by setting a new value and deploying, then revoke the old value with its provider.