Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating and executing functions
    • Creating a timer
    • Creating a trigger for Message Queue
    • Creating a trigger for Object Storage
    • Creating a trigger for Container Registry
    • Creating a trigger for Cloud Logs
    • Creating a trigger for IoT Core
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Managing rights to access functions
    • Managing functions
      • Creating a function
      • Managing function versions
      • Working in the code editor
      • Invoking a function
      • Updating a function
      • Viewing monitoring charts
      • Viewing the execution log
      • Deleting a function
    • Managing triggers
      • Getting information about a trigger
      • Creating a timer
      • Creating a trigger for Message Queue
      • Creating a trigger for Object Storage
      • Creating a trigger for Container Registry
      • Creating a trigger for Cloud Logs
      • Creating a trigger for Yandex IoT Core
      • Updating a trigger
      • Deleting a trigger
  • Concepts
    • Overview
    • Function
    • Invoking a function
    • Runtime
      • Overview
      • Environment
      • Execution context
    • Builder
    • Trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
    • Dead Letter Queue
    • Log groups
    • Monitoring
    • Backups
    • Quotas and limits
  • Developing in Node.js
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Python
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Go
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in PHP
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in Bash
    • Overview
    • Request handler
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Java
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in R
    • Overview
    • Programming model
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in C#
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Use cases
    • Creating skills for Alice
  • Pricing policy
  • Access management
  • API Functions reference
    • Authentication in the API
    • gRPC
      • Overview
      • FunctionService
      • OperationService
    • REST
      • Overview
      • Function
        • Overview
        • create
        • createVersion
        • delete
        • get
        • getVersion
        • getVersionByTag
        • list
        • listAccessBindings
        • listOperations
        • listRuntimes
        • listTagHistory
        • listVersions
        • removeTag
        • setAccessBindings
        • setTag
        • update
        • updateAccessBindings
  • API Triggers reference
    • Authentication in the API
    • gRPC
      • Overview
      • TriggerService
      • OperationService
    • REST
      • Overview
      • Trigger
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • pause
        • resume
        • update
  • Questions and answers
  1. Getting started
  2. Creating and executing functions

Creating and executing functions

  • Before you start
  • Create a function in Python
    • Create the first version of the function
  • Create a function in Node.js
    • Create the first version of the function
  • Invoke the function
  • What's next

Create and execute a function that welcomes the user. To do this, use Python or Node.js.

  • Before you start
  • Create a function in Python
    • Create a function
    • Create the first version of the function
  • Create a function in Node.js
    • Create a function
    • Create the first version of the function
  • Invoke the function
  • What's next

Before you start

  1. Create a folder in Yandex.Cloud.
  2. Install the YC CLI command line interface.

Create a function in Python

Management console
CLI
  1. In the management console, select the folder to create your function in.

  2. Click Create resource.

  3. Choose Function.

  4. Enter a function name.

    • Length — from 3 to 63 characters.
    • The name may contain lowercase Latin letters, numbers, and hyphens.
    • The first character must be a letter. The last character can't be a hyphen.
  5. Click Create.

Use the following command to create a function:

$ yc serverless function create --name=my-python-function
.done
id: b09bhaokchn9pnbrlseb
folder_id: aoek49ghmknnpj1ll45e
created_at: "2019-06-14T10:03:37.475Z"
name: my-python-function
log_group_id: eolm8aoq9vcppsieej6h
http_invoke_url: https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb

Create the first version of the function

To create versions of functions, you can use one of the code upload formats. A ZIP archive will be used for the example.

Warning

Files larger than 3.5 MB should be uploaded via Object Storage. For more information about limits, see Quotas and limits.

Prepare a ZIP archive with the function code

  1. Save the following code to a file named main.py:

    def handler(event, context):
        name = 'World'
        if 'queryStringParameters' in event and 'name' in event['queryStringParameters']:
            name = event['queryStringParameters']['name']
    
        return {
            'statusCode': 200,
            'headers': {
                'Content-Type': 'text/plain'
            },
            'isBase64Encoded': False,
            'body': 'Hello, {}!'.format(name)
        }
    
  2. Add the main.py file to the hello-py.zip archive.

Create a function version

Management console
CLI
  1. Open Cloud Functions in the folder where you want to create the function version.
  2. Select the function to create the version for.
  3. Under Latest version, click Create in editor.
  4. Set the version parameters:
    • Runtime environment: python37.
    • Timeout, seconds: 5.
    • RAM: 128 MB.
    • Service account: Not selected.
  5. Prepare the function code:
    • Method: ZIP archive.
    • File: hello-py.zip.
    • Entry point: main.handler.
  6. Click Create version.

Create the function version:

$ yc serverless function version create \
--function-name=my-python-function \ # Function name.
--runtime python37 \ # Runtime environment.
--entrypoint main.handler \ # The handler specified in the <function file name>.<handler name> format.
--memory 128m \ # Amount of RAM.
--execution-timeout 5s \ # The maximum function execution time before the timeout is reached.
--source-path ./hello-py.zip # ZIP archive with the function code and all the required dependencies.

Create a function in Node.js

Management console
CLI
  1. In the management console, select the folder to create your function in.

  2. Click Create resource.

  3. Choose Function.

  4. Enter a function name.

    • Length — from 3 to 63 characters.
    • The name may contain lowercase Latin letters, numbers, and hyphens.
    • The first character must be a letter. The last character can't be a hyphen.
  5. Click Create.

Use the following command to create a function:

$ yc serverless function create --name=my-nodejs-function
..........done
id: b09hnm3nucbm1tk8dops
folder_id: aoek49ghmknnpj1ll45e
created_at: "2019-06-14T09:53:39.885Z"
name: my-nodejs-function
log_group_id: eol8eetd68mq3849eurs
http_invoke_url: https://functions.yandexcloud.net/b09hnm3nucbm1tk8dops

Create the first version of the function

To create the function version, you'll need a ZIP archive with the function code and all the required dependencies.

Prepare a ZIP archive with the function code

  1. Save the following code to a file named index.js:

    exports.handler = async function (event, context) {
        let name = 'World';
        if (event.queryStringParameters && event.queryStringParameters.name) {
            name = event.queryStringParameters.name
        }
        return {
            'statusCode': 200,
            'headers': {
                'Content-Type': 'text/plain'
            },
            'isBase64Encoded': false,
            'body': `Hello, ${name}!`
        }
    };
    
    
  2. Add index.js to the hello-js.zip archive.

Create a function version

Management console
CLI
  1. Open Cloud Functions in the folder where you want to create the function version.
  2. Select the function to create the version for.
  3. Under Latest version, click Create in editor.
  4. Set the version parameters:
    • Runtime environment: nodejs12.
    • Timeout, seconds: 5.
    • RAM: 128 MB.
    • Service account: Not selected.
  5. Prepare the function code:
    • Method: ZIP archive.
    • File: hello-js.zip.
    • Entry point: index.handler.
  6. Click Create version.

Create the function version:

$ yc serverless function version create \
--function-name=my-nodejs-function \ # Function name.
--runtime nodejs12 \ # Runtime environment.
--entrypoint index.handler \ The handler, specified in <function file name>.<handler name> format.
--memory 128m \ # Amount of RAM.
--execution-timeout 5s \ # The maximum function execution time before the timeout is reached.
--source-path ./hello-js.zip # ZIP archive with the function code and all required dependencies.

Invoke the function

Note

To allow any user to invoke your function, make it public. For more information about access rights, see Access management.

To access the function, use its name or unique ID, which can be obtained using the command:

$ yc serverless function list
+----------------------+--------------------+----------------------+
|          ID          |        NAME        |      FOLDER ID       |
+----------------------+--------------------+----------------------+
| b097d9ous3gep99khe83 | my-beta-function   | aoek49ghmknnpj1ll45e |
| b09bhaokchn9pnbrlseb | my-python-function | aoek49ghmknnpj1ll45e |
| b09hnm3nucbm1tk8dops | my-nodejs-function | aoek49ghmknnpj1ll45e |
+----------------------+--------------------+----------------------+
HTTP
CLI

Note

For security reasons, the function can only be called using TLS protocol.

You can view the function invocation link in the http_invoke_url parameter while creating a function.

Call the function as a regular HTTP request by inserting the function call link in the browser address bar. In this case, we use the example of a function written in Python:

https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb

The following response appears on the page:

Hello, World!

When calling the function, you can add some parameters to the URL. As an example, add the name parameter:

https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb?name=Username

The following response appears on the page:

Hello, Username!

Call the function by specifying in the parameter a name for the greeting:

$ yc serverless function invoke my-python-function -d '{"queryStringParameters": {"name": "Username"}}'
{"statusCode":200,"isBase64Encoded":false,"body":"{\"message\":\"Hello, Username!\"}"}

What's next

  • For more information about the structure of functions invoked in different ways (HTTP or CLI), seе Invoking a function.
  • Read about service concepts.
  • For information about what you can do with functions and their versions, see our step-by-step instructions.
In this article:
  • Before you start
  • Create a function in Python
  • Create the first version of the function
  • Create a function in Node.js
  • Create the first version of the function
  • Invoke the function
  • What's next
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC