Invoking a function
Note
To allow any user to invoke (call) a function, make it public. For more information about rights, see Access management.
To access a function, use its name or unique ID. To find them, get a list of functions in a folder.
Invoking a function
As an example, we'll use the function described in Creating a function version.
Invoke the function:
- Select Cloud Functions in management console.
- Click in the row of the function you want to invoke.
- In the window that opens, go to Testing.
- In the Tag version field, specify which function version to invoke.
- Under Payload template, choose one of the options:
- Without preset: Custom data format.
- HTTP call: HTTP call data format for a function acting as an HTTP request handler.
- Trigger for Message Queue: Data format for the function called by a trigger in order to process messages from the queue.
- In the Input field, enter the input data to test the function.
- Click Run test.
- You will see the testing status under Test result in the Function status field. Important: The maximum function execution time before timeout (including original initialization at first call) is 10 minutes.
- You will see the data processed by the function in the Function output field.
Note
For security reasons, the function can only be called using TLS protocol.
Call the function as a regular HTTP request, such as by entering the function call link in your browser address bar. Call link format:
https://functions.yandexcloud.net/<function ID>
The function call link is also shown in the http_invoke_url
parameter when creating a function.
You can call a specific function version using the tag
parameter. The function with the $latest
tag is called by default.
-
Example of function call with no additional parameters:
https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb
The response depends on the function you call. In this case, it will be as follows:
Hello, World!
-
Example of function call with the
name
parameter added to the URL:https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb?name=<user name>
The following response appears on the page:
Hello, Username!
-
Example of calling a specific function version with the
tag
parameter added to the URL:https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb?tag=<version tag>
You can call a specific function version using the --tag
parameter. The function with the $latest
tag is called by default.
-
Call the function by specifying in the parameter a name for еру greeting:
$ yc serverless function invoke <function ID> -d '{"queryStringParameters": {"name": "Username"}}'
Result:
{"statusCode": 200, "headers": {"Content-Type": "text/plain"}, "isBase64Encoded": false, "body": "Hello, Username!"}
-
Use a tag to call a specific function version:
$ yc serverless function invoke <function ID> --tag <function version tag>
To learn more about function structure dependence for different invocation methods (HTTP and CLI), see Invoking a function.
Authentication when invoking a private function via HTTP
To invoke a private function via HTTP, you must authenticate. To do this, get:
-
- Instructions for a Yandex account.
- Instructions for a service account.
- Instructions for a federated account.
Pass the obtained IAM token in the
Authorization
header using the following format:Authorization: Bearer <IAM-TOKEN>
IAM tokens are valid for 12 hours maximum.
-
API key for the service account.
Pass the obtained API key in the
Authorization
header using the following format:Authorization: Api-Key <API key>
API keys do not expire. This means that this authentication method is simpler, but less secure. Use it if you can't request an IAM token automatically.