Serverless 简明教程

Serverless - Regions, Memory-size, Timeouts

我们在上一章中了解了如何使用 serverless 来部署我们的第一个函数。在本章中,我们将介绍一些可以对函数执行的配置。我们将主要关注区域、内存大小和超时。

We saw how to deploy our first function using serverless in the previous chapter. In this chapter, we will look at some configurations that we can perform on the function. We will primarily look at the region, the memory-size, and the timeout.

Region

默认情况下,使用 serverless 部署的所有 lambda 函数都是在 us-east-1 区域创建的。如果你希望在其他区域创建 lambda 函数,那么你可以在提供者中指定。

By default, all lambda functions deployed using serverless are created in the us-east-1 region. If you want your lambda functions to get created in a different region, you can specify that in the provider.

provider:
   name: aws
   runtime: python3.6
   region: us-east-2
   profile: yash-sanghvi

不可能在同一个 serverless.yml 文件中为不同函数指定不同的区域。你应该仅在特定的 serverless.yml 文件中包含属于单个区域的函数。属于单独区域的函数可以使用单独的 serverless.yml 文件来部署。

It is not possible to specify different regions for different functions within the same serverless.yml file. You should include only the functions belonging to a single region in a particular serverless.yml file. Function belonging to a separate region can be deployed using a separate serverless.yml file.

MemorySize

AWS Lambda 根据选择的内存按比例分配 CPU。根据最近宣布的更改,你可以为 lambda 函数选择高达 10GB 的 RAM(以前约为 3GB)。

AWS Lambda allocates CPU in proportion to the memory chosen. With the recently announced changes, you can choose up to 10GB of RAM for your lambda function (it was ~3 GB earlier).

选择的 RAM 越大,分配的 CPU 越多,函数执行得越快,执行时间越短。AWS Lambda 会向你收取所用 GB 的费用。因此,如果一个 1GB RAM 的函数执行需要 10 秒,而一个 2GB RAM 的函数执行需要 5 秒,那么这两个调用将收取相同的费用。 时间是否会随着内存的增加而减半很大程度上取决于函数的性质,而且增加内存可能不会给你带来好处。关键要点是,分配的内存量是每个 lambda 函数的重要设置,是你想控制的设置。

The higher the RAM chosen, the higher is the CPU allocated, the faster your function executes, the lower is the execution time. AWS Lambda charges you for the GB-s consumed. Therefore, if a function on 1 GB RAM takes 10 seconds to execute and it takes 5 seconds to execute on a 2 GB RAM, you will be charged the same amount for both the invocations. Whether the time halves on doubling the memory depends a lot on the nature of your function, and you may or may not benefit by increasing the memory. The key takeaway is that the amount of memory allotted is an important setting for each lambda function and one you would like to have control of.

使用 serverless,可以非常轻松地为在 serverless.yml 文件中定义的函数设置内存大小的默认值。还可以为不同的函数定义不同的内存大小。让我们看看如何进行操作。

With serverless, it is quite easy to set the default value of the memory-size for the functions defined within your serverless.yml file. It is also possible to define different memory-sizes for different functions. Let us see how.

Setting default memory-size for all the functions

默认值始终在提供者中提及。该值将被该 serverless.yml 中的所有函数继承。 memorySize 键用于设置此值。值以 MB 为单位表示。

The default values are always mentioned in the provider. This value will be inherited by all the functions within that serverless.yml. The memorySize key is used for setting this value.The value is expressed in MB.

provider:
   name: aws
   runtime: python3.6
   region: us-east-2
   profile: yash-sanghvi
   memorySize: 512 #will be inherited by all functions

如果你未在提供者中或在各个函数中指定 memorySize,那么将考虑默认值 1024。

In case you don’t specify the memorySize in the provider, nor in individual functions, the default value of 1024 will be considered.

Setting custom memory-size for some functions

如果你希望一些函数具有不同于默认内存的值,那么你可以在 serverless.yml 的 functions 部分中指定它。

In case you want some functions to have a value different than the default memory, then you can specify it in the functions section of serverless.yml.

functions:
   custom_memory_func: #will override the default memorySize
      handler: handler.custom_memory
      memorySize: 2048
  default_memory_func: #will inherit the default memorySize from provider
      handler: handler.default_memory

Timeout

就像 memorySize 一样,超时(以秒为单位)的默认值可以在提供者中设置,并且可以在 functions 部分中为各个函数指定自定义超时。

Just like memorySize, the default value of the timeout (in seconds) can be set in the provider, and custom timeouts for individual functions can be specified in the functions section.

如果你未指定全局超时或自定义超时,那么默认值为 6 秒。

If you don’t specify either the global or custom timeouts, the default value is 6 seconds.

provider:
   name: aws
   runtime: python3.6
   region: us-east-2
   profile: yash-sanghvi
   memorySize: 512 #will be inherited by all functions
   timeout: 50 #will be inherited by all functions

functions:
   custom_timeout: #will override the default timeout
      handler: handler.custom_memory
      timeout: 30
  default_timeout_func: #will inherit the default timeout from provider
      handler: handler.default_memory

请务必保守地保持超时。它不应太小,以至于你的函数经常超时,也不应太大以至于你的函数中的一个 bug 会给你带来一笔巨额账单。

Make sure to keep the timeout at a conservative value. It should not be so small that your function times out very frequently, nor should it be so large that a bug in your function leaves you with an outrageous bill to pay.