-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Next.js SSR mode - Environment Variables not being carried over to the created Lambda Functions #1987
Comments
hey @joelvarty did you find any solution? I am having the same problem |
Nope. I think this is an actual bug in the Next.js SSR Logic with Amplify |
Hi, yes this is a bug and we are working on a fix. As a temporary workaround you can manually add the environment variable to your SSR Lambda function in the Lambda Console. Sorry you are facing this issue; we will follow up when this is fixed. Thanks! |
@Athena96 thanks for the workaround, however, after a redeploy all lambda functions are published as a new version without the env. variables, is there a way to prevent that from happening? On each push to my branches I'm losing the data and the app starts crashing. Thanks |
Same issue here. The only solution I can think of in the meantime is to use secrets manager or ssm parameter store |
This comment has been minimized.
This comment has been minimized.
Would be nice to have an ETA here, in the meanwhile I moved my config to parameter store as pointed out by @joekendal, however, it is not ideal as the parameter key name is "hardcoded" and it is not environment independent. I can survive with it for now, but I can see this being a major issue for some others, it is very inconvinient. |
Additionally, what would be the best way to give those lambda functions a policy to read from parameter store? They have a custom role created by amplify, but there's no cloud formation template for it. I did it manually for now |
@bajcmartinez yeah I haven't opted for that solution myself. Waiting for an update here. Will have to just deploy the API lambdas separately using CDK or something as opposed to Next.js /api/ route feature. |
The problem is even more deep, even if I try to add the env variables manually I cannot deploy those functions as the CloudFront edge run environment does not support lambda environment variables as described here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-functions-restrictions.html#edge-function-restrictions-all |
I'm very confused then @Athena96 declares this is a bug and not a feature. But it seems it is in fact a feature and not a bug. Vercel uses Lambda@Edge and enables the user to provide environment variables. So they must be doing something during build time to make that available to process.env behind the scenes |
Hi @Athena96 has there been any movement on this? |
This is a really big issue. |
Same issue here. |
Hi all! We have added support for Environment variables for Next.js SSR apps. Follow these steps to enable:
Thank you, and please let us know if you have any additional questions! |
This is great news @Athena96, will try it today. Thanks so much, have a great weekend! |
I was just thinking, nextjs has public and private env variables, some that get sent to the client start with NEXT_PUBLIC_, how are they treated now? will env. variables treated the same way? meaning non-public like SECRET_ENV_VAR, will that one remain for API and SSR ONLY? |
I appreciate this @Athena96! However... is there any reason not to simply create ALL the environment vars that are defined in Amplify on the Lambda functions? That way we don't need to have the extra config. The reason this is important to us is that we build starter templates that run in a TON of cloud environments, and having to put vendor-specific config in there seems out of place. |
@Athena96 I'm having issues now with my production build. It's not taking the env. overrides and always picking the default sets for the variables. Could you please confirm this? Or is there anything else I should fix? e.g.: In my app I have the following settings: And it's always using the test...... URL |
Hi @bajcmartinez currently we do not make a distinction between the Next public/private env vars. They are all treated the same way. |
@bajcmartinez yes this is a bug and we are working on a fix. I will follow up when it is deployed. |
Hi @Athena96 I'm trying to follow your work around. I understand creating separate variables for different environments (e.g. API_URL_DEV vs API_URL_QA). My quesiton still stands at build time how would the application know which one to point to ? Is there a default variable from Amplify that can be used to know which ("env" or branch) is being built. |
Hi All who are looking for a work around. AWS Amplify makes the branch you're building available in an envrionment variable (process.env.AWS_BRANCH). Using this env var I'm able to point to the correct url I want to use pending that branch build.
If there are other feature branches you can add them as options in the object, if a variable is not available for the AWS_BRANCH being built it will default to the |
This should probably be documented on the Amplify Docs here: https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html#access-env-vars |
If someone else did all of the above and is hopeless as I was:
My guess is there was some cache somewhere and my changes on the |
An alternative approach is to use |
Instead I need ;) |
This bug is a pretty big deal - I can't see how we can use Amplify for CI/CD until this gets fixed.. |
There are 3-4 issues that stop us from using Amplify to deploy our Next.js app on Amplify:
I really hope these will get fixed at some point in the near future, because that would make Amplify a truly great alternative to Vercel. |
@traianturcu |
When declaring two variables in env.local - one NEXT_PUBLIC_VAR1 and one VAR2, VAR2 is undefined in the frontend if you try to use it/output it/console.log it, and only the NEXT_PUBLIC_VAR1 is available. VAR2 is available only in the backend (for the pages/api handlers). I'm not sure if this means the security of the app is compromised (e.g. secret keys being incorporated in the .js files being sent to the frontend), but at the very least by using the next.config.js hack we are losing some of the Next.js functionality around env vars. |
I did not undestand. I used AAA_VAR in next config. This var is available on files under /api. And searching on code I cannot find that secret on browser. |
I apologize for not explaining properly - I've recorded a very short video to show you the difference between Amplify vs. local/ Vercel /expected behavior - https://www.screencast.com/t/At4iOMAl |
@traianturcu thank for video. That is what I understood after my experience (deploy next ssr without next export).
Reading this: https://nextjs.org/docs/api-reference/next.config.js/environment-variables Reading doc we can use aldo "phase" variables to know when use the trick or not. Probably we can use "PHASE_PRODUCTION_SERVER" only |
@bojidaryovchev does this solution solve the problem of not exposing secret env vars to the browser? |
@code-runner-2017 and... Is this valid solution in a case of normal build or only for next export build |
@code-runner-2017 @multivoltage not sure guys, you should test it out |
How is this still not fixed? @Athena96 ? |
I think the reason that it hasn't been fixed is that it is working as planned by the amplify console team. In my case, the only way around this was to create the It worked for a while but is not comfortable to support on multiple projects, so I just move away to Elastic Beanstalk |
Ticket open for over a year now! |
Is this been solved in any way? I can't believe that such a fundamental feature has bugs. |
This worked for me! 👍 |
adding:
to amplify.yml solved the issue for me. But that seems more like a workaround than a solution. |
Every guy with amplify seem to implement custom solution :) |
This as well as adding the environment variables to the module exports worked for me! |
This worked for me -- BUT, only if I wrote to |
Does not work for me. I see my env variables in the build phase if I log process.env, but not in the actual api code that executes in Lambda. I also tried the method of adding env vars to amplify.yml commands like @aktoriukas did but that didn't work either. Then I tried setting environment variables in the configuration of my lambda@edge and on depoloy it says "Environment variables are not supported" |
I was able to launch on Amplify today using the setup below. I do have a amplify.yml version: 1
frontend:
phases:
preBuild:
commands:
- touch .env.local
- echo "DATABASE_URL=$DATABASE_URL" >> .env.local
- yarn install
- npx dotenv -e .env.local -- npx prisma generate
- npx dotenv -e .env.local -- npx prisma db push
- npx dotenv -e .env.local -- npx prisma db seed
build:
commands:
- yarn run build
artifacts:
baseDirectory: .next
files:
- "**/*"
cache:
paths:
- node_modules/**/* package.json {
"name": "example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start -p 80",
"start:local": "next start",
"lint": "next lint",
"migrate": "dotenv -e .env.local -- npm run generate && npm run push && npm run seed",
"generate": "npx prisma generate",
"push": "dotenv -e .env.local -- npx prisma db push",
"seed": "dotenv -e .env.local -- npx prisma db seed",
"test": "npm run lint && jest --forceExit --detectOpenHandles"
},
"prisma": {
"seed": "node prisma/seed.js"
},
"dependencies": {
"@prisma/client": "^4.3.1",
"@reduxjs/toolkit": "^1.8.5",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"next": "12.2.5",
"next-redux-wrapper": "^7.0.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.34.2",
"react-redux": "^8.0.2"
},
"devDependencies": {
"@types/jest": "^27.0.1",
"@types/node": "18.7.14",
"@types/react": "18.0.18",
"@types/react-dom": "18.0.6",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"autoprefixer": "^10.4.8",
"dotenv": "^16.0.3",
"dotenv-cli": "^6.0.0",
"eslint": "^8.20.0",
"eslint-config-next": "12.2.5",
"husky": "^8.0.1",
"jest": "^27.2.0",
"lint-staged": "^13.0.3",
"postcss": "^8.4.16",
"prettier": "^2.7.1",
"prettier-eslint": "^15.0.1",
"prisma": "^4.3.1",
"supertest": "^6.1.6",
"tailwindcss": "^3.1.8",
"ts-jest": "^27.1.4",
"ts-node": "^10.8.1",
"typescript": "4.8.2"
}
} next.config.js /** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
};
module.exports = nextConfig; One thing that did bite me along the path was the Amplify default command:
My file started with a comment:
Output the results of the file after the Amplify echo command (cat .env) showed that the output had been append to my comment not a new line:
Thus my decision to flee and immediately swap to a |
I am pleased to share we have now launched support for Next.js 12 and 13. This new release solves this problem in our previous support, take a look at the documentation to learn how to add environment variables to the functions: https://docs.aws.amazon.com/amplify/latest/userguide/ssr-environment-variables.html For more information about the launch, please see our blog post at https://aws.amazon.com/blogs/mobile/amplify-next-js-13 If you run into issues with Next.js 12 or 13, please open a new issue so we can track it specifically. Thank you everyone for your patience and support. It's much appreciated. |
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Before opening, please confirm:
App Id
d36aqre3ndgr2h
Region
us-east-1
Amplify Console feature
Environment variables
Describe the bug
When I set environment variables in the Amplify app, they are NOT being carried through to the Lambda functions that are automatically created for my Next.js SSR website.
Even if I set the environment variables later on the Lambda functions that are created, they get wiped out the next time I do a build.
Here's a link to my repo: https://github.com/agility/agilitycms-nextjs-ssr-starter
Expected behavior
I expected the environment variables to propagate from the Amplify settings to the Lambda functions that are created.
Reproduction steps
Build Settings
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: