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. Each time a change is made, the system automatically creates a new version and tags it as $latest
by default. 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
Once a new version is created, it gets the default tag: $latest
.
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 the availability zone.zone_requests_limit
: Number of concurrent function calls in the availability zone.provisioned_instances_count
: Number of ready-to-go instances.concurrency
: Maximum number of calls processed concurrently by a single function instance. Available only in certain runtime environments. For more information, see Concurrent function instance calls.
Note
Calls are distributed across availability zones randomly. Cloud Functions does not guarantee the even distribution of calls across the 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.
Concurrent function instance calls
To allow a single function instance to handle multiple function calls concurrently, set the concurrency
parameter when creating a function version. The IDs of such calls (RequestID
) must be unique. Otherwise, you will be getting an error when attempting to process a call with a duplicate ID.
If at least one call reaches a timeout, that call and all other calls handled by the same function instance will be aborted. For more information about the timeout, see Limits.
The concurrency
parameter is available for functions with the following runtime environments:
Limits
When the number of function instances reaches the zone_instances_limit
value, Cloud Functions stops scaling it. If there are more function calls than the instances can handle, the new call is queued and treated as a call in progress. When the number of calls in progress reaches the zone_requests_limit
value, 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 there are more function calls than provisioned instances can handle, Cloud Functions scales the function within the quotas. New calls can be handled by either a provisioned instance or a regular one, whichever becomes free or created first.
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.