Using Next.js with SSR on AWS Amplify, Environment Variables NOT being carried through to auto-created Lambda

Viewed 1454

I have a Next.js app with SSR which relies on environment variables. When I deploy the app to AWS Amplify, 2 Lambda functions are created.

I have my environment variables setup in Amplify, however, those variables are NOT set in the Lambda functions that are created during the build. If I set the environment variables manually on the functions, they are removed during the next build.

Any ideas on how this is supposed to work?

1 Answers

The amplify team have been updating this issue in github here: https://github.com/aws-amplify/amplify-console/issues/1987

Here is the way they are suggesting to do this:

  1. Add your desired environment variable in the Amplify Console like normal (steps)
  2. Update (or create) your next.config.js file with the environment variable you added in the Amplify Console. E.g if you created an environment variable named MY_ENV_VAR in the console in step 1) above, then you would add the following:
module.exports = {
  env: {
    MY_ENV_VAR: process.env.MY_ENV_VAR
  }
};
  1. Now after your next build you will be able to reference your environment variable (process.env.MY_ENV_VAR) in your SSR lambdas!
Related