Function
A function allows you to put your code in Yandex Cloud and run it on request or trigger.
Once created, a function only contains its own metadata, including its name, description, and unique ID. To start using the function, you need to create a function version. You can execute functions using the HTTPS API or CLI.
Before writing your functions, review the programming model.
Function versions
Versions contain the function code, run parameters, and all required dependencies. You can work with different versions of the same function during different development stages, e.g., under development, alpha version, beta version, etc. When making changes, new versions are always created with the default $latest
tag. It does not matter whether you uploaded a completely new code or only made some minor changes.
Code upload format
To create a version, you can use the code editor or upload your code and its dependencies in one of the formats described in the table below. After you create a function, you no longer need the object with the code. You can delete it from storage.
Format | Management console | Yandex Cloud CLI |
---|---|---|
ZIP archive from your PC | ||
ZIP archive from S3, specifying the bucket and object | ||
Directory | ||
File |
Warning
Files larger than 3.5 MB should be uploaded via Object Storage. For more information about limits, see Quotas and limits in Cloud Functions.
For more information about how to create versions, see Creating a function version.
Version tag
Tags are used for calling a particular version of a function. Each tag must be unique within the function. One version may have multiple tags.
Note
When creating a new version, it's assigned the $latest
tag by default.
Programming model
Cloud Functions supports different programming languages. Regardless of the language you choose, you should write your code based on the programming model accepted in the service. It contains elements that the user needs to implement as well as mechanisms that are already implemented on the service side and can be used when necessary.
Model description
To enable a function call, describe a handler in it. It is defined when writing code. The handler handles the data received by invoking respective functions and methods in the code. The handler should accept the data that comes with function requests and the context provided by Cloud Functions. Some runtime environments have requirements for an absolute path to the handler file:
- Python, Go, Java: No
.
in the file name before the extension, e.g.,.handler.py
. - .NET Core: No
.
in the file name before the extension and at the beginning of the name of the directory containing the handler file, e.g.,.handler.cs
or.test/handler.cs
.
Context allows your function code to interact with Cloud Functions. For example, the function can use it to find how much time is left before Cloud Functions completes its execution.
Incoming requests are processed by the function one at a time. For your function to process multiple requests simultaneously, use asynchronous code execution provided by the runtime environment.
To report a function execution error to the service, handle errors using exceptions.
The service logs the execution of functions. However, if necessary, you can implement additional logging in your function code.
Scaling a function
A function instance processes one function call at a single point in time. If the function is called faster than one instance can process the request, the service scales the function by running additional function instances. This ensures concurrent request processing. In Cloud Functions, you can specify:
zone_instances_limit
: Number of function instances in an availability zone.zone_requests_limit
: Number of concurrent function calls in an availability zone.provisioned_instances_count
: Number of provisioned instances.
Note
Function calls are distributed across availability zones randomly. Cloud Functions does not guarantee their even distribution across zones. For example, all calls, no matter how many, might end up in the same zone.
Scaling settings take effect within five minutes after being configured.
Limits
When the number of function instances reaches the zone_instances_limit
, Cloud Functions stops scaling it. If there are more function calls than instances available, the call is queued and treated as a call-in-progress. When the number of calls-in-progress reaches the zone_requests_limit
, the service stops queuing calls and returns the 429 TooManyRequests
error.
Provisioned instances
A provisioned instance is a function instance that, when started, is guaranteed not to have a cold start. In the provisioned instance before the function is called:
- The interpreter is started.
- Cloud Functions runtime environment components are initialized.
- The user code is loaded and initialized.
Downtime of provisioned instances and runtime of the function in them are charged additionally.
If the number of function calls exceeds the number of provisioned instances, Cloud Functions scales the function within the quotas, but unprovisioned instances have a cold start when they are first started.
Provisioned instances count towards the following quotas even when they are not running:
- Number of function instances per availability zone.
- Total RAM for all running functions per availability zone.
- Number of provisioned function instances per cloud.