Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Solutions
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating a function
      • Overview
      • Python
      • Node.js
      • PHP
      • Go
      • Bash
    • Creating a trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex 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
      • View monitoring charts
      • Viewing the execution log
      • Delete 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 environment
      • 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 a function
  3. Node.js

Creating a function in Node.js

  • Before you start
  • Create a function
  • Create the first version of the function
    • Prepare a ZIP archive with the function code
    • Create a function version
  • What's next

Create and execute a function in Node.js that welcomes the user.

Before you start

Create a folder in Yandex.Cloud.

Create a function

Management console
CLI
API
  1. In the management console, go to the folder where you want to create a function.
  2. Open Cloud Functions.
  3. Click Create function.
  4. Enter a name for the function: nodejs-function.
  5. Click Create.

If you don't have the Yandex.Cloud command line interface yet, install and initialize it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

To create a function, run the command:

yc serverless function create --name=nodejs-function

Result:

id: b09bhaokchn9pnbrlseb
folder_id: aoek49ghmknnpj1ll45e
created_at: "2019-06-14T10:03:37.475Z"
name: nodejs-function
log_group_id: eolm8aoq9vcppsieej6h
http_invoke_url: https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb
status: ACTIVE

You can create a function version using the create API method.

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 hello.js:

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

Create a function version

Management console
CLI
API
  1. In the management console, go to the folder where the function is located.
  2. Select Cloud Functions.
  3. Select the function named nodejs-function.
  4. Under Latest version, click Create in editor.
  5. Set the version parameters:
    • Runtime environment: nodejs12.
    • Method: ZIP archive.
    • File: hello-js.zip.
    • Entry point: hello.handler.
    • Timeout, seconds: 3.
    • RAM: 128 MB.
    • Service account: Not selected.
  6. Click Create version.

If you don't have the Yandex.Cloud command line interface yet, install and initialize it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

To create a function version, run the command:

yc serverless function version create \
--function-name=nodejs-function \
--runtime nodejs12 \
--entrypoint hello.handler \
--memory 128m \
--execution-timeout 3s \
--source-path ./hello-js.zip

where:

  • --function-name: The function you want to create a version of.
  • --runtime: The runtime environment.
  • --entrypoint: The entry point specified in . format.
  • --memory: The amount of RAM.
  • --execution-timeout: The maximum function execution time before the timeout is reached.
  • --source-path: ZIP archive with the function code and required dependencies.

Result:

done (1s)
id: d4evvn8obisajd51plaq
function_id: d4elpv8pft639ku7k0u6
created_at: "2020-08-01T19:09:19.531Z"
runtime: nodejs12
entrypoint: hello.handler
resources:
    memory: "134217728"
execution_timeout: 3s
image_size: "4096"
status: ACTIVE
tags:
- $latest
log_group_id: ckg3qh8h363p40gmr9gn

You can create a function version using the createVersion API method.

Invoke the function

Note

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

Management console
CLI
HTTPS
  1. In the management console, go to the folder where the function is located.

  2. Select Cloud Functions.

  3. Select a function.

  4. Go to the Testing tab.

  5. In the Tag version field, specify $latest to invoke the latest function version.

  6. Under Payload template, choose Without template.

  7. In the Input field, enter:

    {"queryStringParameters": {"name": "Username"}}
    
  8. Click Run test.

  9. 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 initial launch) is 10 minutes.

  10. You will see the function execution result in the Function output field:

    {
    	"statusCode": 200,
    	"headers": {
    		"Content-Type": "text/plain"
    	},
    	"isBase64Encoded": false,
    	"body": "Hello, Username!"
    }
    

If you don't have the Yandex.Cloud command line interface yet, install and initialize it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

To invoke a function, run the command:

yc serverless function invoke <function ID> -d '{"queryStringParameters": {"name": "Username"}}'

Result:

{"statusCode":200,"headers":{"Content-Type":"text/plain"},"isBase64Encoded":false,"body":"Hello, Username!"}

The function version with the $latest tag is invoked by default.

You can view the function invocation link on the Overview tab, in the Link to invoke field.

For security reasons, you can only invoke the function via HTTPS. Invoke it as a regular HTTP request by inserting the function invocation link in the browser address bar and adding the name parameter to the URL:

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

The following response appears on the page:

Hello, Username!

What's next

  • For more information about the structure of functions invoked in different ways (HTTP or CLI), see 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
  • Create the first version of the function
  • Prepare a ZIP archive with the function code
  • Create a function version
  • What's next
Language / Region
Careers
Privacy policy
Terms of use
Brandbook
© 2021 Yandex.Cloud LLC