Trying out the nextjs example in examples/nextjs-a...
# help
w
Trying out the nextjs example in examples/nextjs-app/, for the prod deployment I'm getting a 503 error on clicking the "Click Me!" button. "LambdaExecutionError from cloudfront" and "The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions." It works fine with local frontend and the debug stack. I can't see anything wrong with the cloudfront distribution that's obvious to me. What could I do next to track down the problem please?
f
Hey @Warwick Grigg, when you click the button, are you seeing the 503 for the API request? Or are you getting 503 on the page itself?
w
503 on the api request
f
I see. Can you share your CFN template json file in
.build/cdk.out
?
Thanks Warwick. Let’s give this a try: 1. go into ur Lambda console; 2. look for a Lambda function that starts with 
ApiFunction
 in its name; 3. then go to CloudWatch console > select Log Groups on the left 4. because this is a Lambda@Edge function, you need to switch to the AWS region you made the request from, ie. 
eu-west-1
. 5. search for a log group that matches the function name in step 2, you should see a log group called 
/aws/lambda/us-east-1.ur-lambda-function-name
6. go into the log group, and select the first log stream
Do you see an error in there?
w
The environment variables TABLE_NAME and REGION only get exposed to the nextjs backend code through the next.config.js file, like so :
module.exports = {
  
env: {
    
TABLE_NAME: process.env.TABLE_NAME,
    
REGION: process.env.REGION,
  
}}
Nextjs info here The serverless-nextjs repo's README has a FAQ about this. PS Thanks Frank, your help was invaluable in tracking down the problem
f
Hey @Warwick Grigg I pushed out an update last night v0.43.4 and that shouldve fixed this. you shouldn't need to define in
next.config.js
any more!
w
@Frank OK, I'll give that a try. Thanks! I wasn't sure whether that fix was just for server side rendering, or for all the nextjs lambdas. So I properly understand what the fix does, does it expose all environment variables to every nextjs lambda or just those in the environment in NextjsSiteProps?
f
It exposes the environment configured in
NextjsSiteProps
to all the Next.js Lambdas. For example this:
Copy code
environment: {
  API_URL: ...
}
You can reference
process.env.API_URL
in Server Side Render, Incremental Static Regeneration, API routes, etc.
w
@Frank Thanks v0.43.4 fixes my problem and it all makes sense now. The new NextjsSite construct is looking great! I'll be using it a lot! Many, many thanks.
f
Nice! 👍