”;
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
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
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 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).
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.
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
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
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
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
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.
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
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.
”;