简体   繁体   English

NextJS API Lambda AWS Amplify 执行错误

[英]NextJS API Lambda Execution Error AWS Amplify

I have a NextJS app that runs on AWS amplify.我有一个在 AWS amplify 上运行的 NextJS 应用程序。 I built a basic REST API by adding a pages/api directory to my project.我通过向我的项目添加pages/api目录构建了一个基本的 REST API。 The endpoint returns a 200 and some test JSON data.端点返回 200 和一些测试 JSON 数据。 This works fine when I run the project locally.当我在本地运行项目时,这很好用。 I deployed to AWS Amplify and the build detects that a Next API is present so it provisions a Lambda and configures the CloudFront behavior for /api/* routes to point to the Lambda function. I deployed to AWS Amplify and the build detects that a Next API is present so it provisions a Lambda and configures the CloudFront behavior for /api/* routes to point to the Lambda function. When hitting the API CloudFront returns a 503 error:当点击 API CloudFront 返回 503 错误:

503 ERROR
The request could not be satisfied.
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
Generated by cloudfront (CloudFront)
Request ID: YnQI9alkoBXwkmkkpXwa29zqXaOT06VCXiBZWJI6xQVkhQ8MElB2bQ==

It doesn't appear that CloudFront is even calling the Lambda, as I am unable to see any logs in Cloudwatch. CloudFront 似乎没有调用 Lambda,因为我在 Cloudwatch 中看不到任何日志。 I've tried to debug and built a test that passes a mock CloudFront event request to the Lambda but I am unable to get my API to execute successfully.我尝试调试并构建了一个测试,将模拟 CloudFront 事件请求传递给 Lambda,但我无法让我的 API 成功执行。

It seems that Amplify/Next provides a lot of boilerplate code for supporting Next API routes so I'm not sure where to focus my debugging efforts.似乎 Amplify/Next 提供了很多样板代码来支持 Next API 路由,所以我不确定将调试工作集中在哪里。

Has anyone run into this issue before?有没有人遇到过这个问题? Any guidance or suggestions would be super helpful!任何指导或建议都会非常有帮助!

I did have a similary problem, but i'm using serverless with @sls-next/serverless-component component.我确实遇到了类似的问题,但我正在使用带有@sls-next/serverless-component组件的无服务器。 Because in moment hasn't support for NextJs 12 version.因为目前还不支持 NextJs 12 版本。

In my package.json i'm force NextJs version for more recently in 11, this case is 11.1.4.在我的 package.json 中,我最近在 11 中强制使用 NextJs 版本,这种情况是 11.1.4。

// package.json
 {
  "name": "next-aws",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^11.1.4",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/node": "^14.14.37",
    "@types/react": "^17.0.3",
    "typescript": "^4.2.3"
  }
}

Hope you were able to resolve your issues by now.希望您现在能够解决您的问题。

I also had some problems deploying the API routes to Amplify but found this FAQ page that had the solution for me (missing environment variables).我在将 API 路由部署到 Amplify 时也遇到了一些问题,但我发现这个 FAQ 页面为我提供了解决方案(缺少环境变量)。 The page also lists how to find the logs for the functions.该页面还列出了如何查找函数的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM