如何在将NextJS应用托管在AWS Amplify上时配置函数超时?

huangapple go评论56阅读模式
英文:

How do I configure function timeout when hosting NextJS app on AWS Amplify?

问题

我已经将我的Next.js应用程序部署到AWS Amplify。我需要延长无服务器函数的超时设置,因为操作的时间超过了当前的最大限制。

是否有一种方法可以做到这一点?

我只能找到指向Lambda的响应,但我的函数没有设置为Lambda。我的Next.js /api函数是由AWS Amplify直接处理的。

是否有一种访问配置的方法?或者我必须将我的函数移动到Lambda以配置这个?

PS:长时间的读者,第一次发布 - 如果我遗漏了任何关键信息,请温柔一点

在AWS Amplify中,我找不到这个设置:
AWS Amplify

在Lambda中,这很简单 - 我应该将我的函数移到Lambda吗?
Lambda

英文:

I have my nextjs app deployed with AWS amplify. I need to extend the timeout setting for the serverless functions, because the operation takes longer than the current max.

Is there a way to do this?

I can only find responses pointing to Lambda, but I don't have my functions set up with lambda. My nextjs /api functions are handled directly by AWS Amplify.

Is there a way to access the configuration? Or do I have to move my functions to Lambda in order to configure this?

PS: Long-time reader, first-time poster - be gentle if I missed any critical info

I can't find setting for this in AWS Amplify:
AWS Amplify

In Lambda its straight forward - should I move my functions to Lambda?
Lambda

答案1

得分: 1

如果您正在使用/api,我相信构建工具会将所有这些文件捆绑在一个Lambda@Edge函数中。我不知道您是否可以设置Lambda的超时或授予它资源访问权限。

我相信超时已经从8秒增加到30秒。这是最长的时间。您可以让Lambda运行更长时间,但API Gateway将在30秒内超时。API将失败,但Lambda将继续运行,但其响应将被忽略。

由于您正在使用Amplify,您可以非常轻松地运行amplify add function并创建自己的Lambda函数。然后,您可以设置其超时时间,将运行时设置为所需的Node版本,为其授予访问资源的权限等等。您还可以使用amplify add api来添加REST API或通过GraphQL访问它。但请注意,即使您将Lambda的运行时间设置为更长,这些方法仍然会在30秒内超时。

您可以将请求放入SQS队列并立即返回消息ID。插入Seinfeld "yada yada yada",然后调用者可以使用消息ID来轮询作业完成并获取结果。

或者,您可以创建一个Lambda函数,并使用新的Lambda URL来直接调用Lambda,绕过API Gateway。我在Amplify中撰写了一个GIST,解释了如何做到这一点

英文:

If you're using /api, I believe the build tooling bundles all those files together into one Lambda@Edge function. I don't know if you can set the Lambda's timeout or grant it resource access permissions.

I believe the timeout was increased from 8 seconds to 30 seconds. That's the most you'll get. You can make a lambda run longer, but API Gateway will timeout at 30 seconds. The API will fail, but the lambda will continue running but it's response will be ignored.

Since you're using Amplify, you can very easily amplify add function and create your own lambda. You can then set its timeout, set the runtime to the node version you want, give it permissions to access your resources, etc. And you can amplify add api to add a rest-api or access it via GraphQL. But beware, those approaches will still timeout at 30 seconds even if you set the Lambda to run for longer than that.

You take the request, stick it in an SQS queue and return the message-id instantly. Insert Seinfeld "yada yada yada", and then the caller can then use the message-id to poll for job complete and get the result.

Or you can make a lambda and use the new Lambda URL to directly invoke the lambda bypassing API Gateway. I wrote-up a GIST that explains how to do that in Amplify.

huangapple
  • 本文由 发表于 2023年3月15日 19:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定