Serverless Framework – AWS Lambda Functions

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

Serverless Framework - AWS Lambda Functions

问题

我在AWS上使用无服务器框架部署了一个应用,其中包括多个Lambda函数。
这些函数正常工作,但由于它们需要解压包含Python依赖项的zip文件,所以运行时需要一些时间。
当我首次运行函数时,函数没有足够的时间在30秒内解压并执行(30秒是API Gateway的限制),因此返回HTTP错误503,服务不可用。
我了解Serverless框架,它还使用了缓存策略,这就是为什么第二次调用时函数可以正常执行。但如果我在一段时间内不调用它,缓存就会被清除,再次调用时会失败。

我该怎么做才能消除这种不希望的行为?
我需要为Serverless框架进行额外的配置吗?我需要在AWS中进行某些配置吗?

英文:

I have one deployed in AWS with the help of the serverless framework, several lambda functions.
The functions work correctly. But they take time to run, since they have to unzip a zip file with the python requirements.
When I run the function for the first time, the function does not have time to decompress and execute in less than 30 seconds (30 seconds in the API Gateway limitation). So it returns HTTP error 503 service unavailable
I understand Serverles Framwork, it also creates a cache policy using, that's why when I call it again for the second time, the function executes correctly. When a few minutes go by without me calling it, the cache is cleared, and it fails again the first time I call it.

What could I do to eliminate this unwanted behavior?
Do I have to put any extra configuration for serverless framework? Do I have to configure something in AWS?

答案1

得分: 0

你可以使用 AWS Lambda 预置并发功能。这有助于保持函数在再次调用之前保持热启动

了解更多信息,请点击这里

由于你正在使用 Serverless 框架,你可以像这样做:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
        method: get
    provisionedConcurrency: 5
英文:

You can make use of AWS Lambda Provisioned Concurrency. This helps keep the function warm till it's called again.

Read more about it here

Since you are using the Serverless framework, you can do it like this:

functions:
  hello:
    handler: handler.hello
      events:
        - http:
            path: /hello
          method: get
    provisionedConcurrency: 5

huangapple
  • 本文由 发表于 2023年7月20日 17:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728382.html
匿名

发表评论

匿名网友

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

确定