Creating a function in Node.js
Create and execute a function in Node.js that welcomes the user.
Getting started
Create a folder in Yandex Cloud.
Create a function
- In the management console
, select the folder where you want to create a function. - Go to Cloud Functions.
- Click Create function.
- Enter the function name:
nodejs-function. - Click Create.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
To create a function, run the command:
yc serverless function create --name=nodejs-function
Result:
id: b09bhaokchn9********
folder_id: aoek49ghmknn********
created_at: "2023-08-16T19:01:37.475Z"
name: nodejs-function
log_group_id: eolm8aoq9vcp********
http_invoke_url: https://functions.yandexcloud.net/b09bhaokchn9********
status: ACTIVE
You can create a function using the create.
Create the first version of the function
To create a function version, you can use one of the code upload formats. This example uses a ZIP archive.
Warning
Use Object Storage to upload files larger than 3.5 MB. For more information about limits, see Quotas and limits in Cloud Functions.
Prepare a ZIP archive with the function code
-
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}!` } }; -
Add
hello.jsto thehello-js.ziparchive.
Create a function version
- In the management console
, select the folder containing the function. - Go to Cloud Functions.
- Select
nodejs-function. - Under Last version, click Сreate in editor.
- Select the
Node.js 18runtime environment. - Disable Add files with code examples and click Continue.
- Set the version parameters:
- Code source:
ZIP archive. - File: Attach
hello-js.zip. - Entry point:
hello.handler. - Timeout:
3. - Memory:
128 MB. - Service account:
Not selected.
- Click Save changes.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
To create a function version, run the command:
yc serverless function version create \
--function-name=nodejs-function \
--runtime nodejs18 \
--entrypoint hello.handler \
--memory 128m \
--execution-timeout 3s \
--source-path ./hello-js.zip
Where:
--function-name: Name of the function whose version you want to create.--runtime: Runtime environment.--entrypoint: Entry point in<function_file_name>.<handler_name>format.--memory: Amount of RAM.--execution-timeout: Maximum function running time before timeout.--source-path: ZIP archive with the function code and required dependencies.
Result:
done (1s)
id: d4evvn8obisa********
function_id: d4elpv8pft63********
created_at: "2023-08-16T19:09:19.531Z"
runtime: nodejs18
entrypoint: hello.handler
resources:
memory: "134217728"
execution_timeout: 3s
image_size: "4096"
status: ACTIVE
tags:
- $latest
log_group_id: ckg3qh8h363p********
You can create a function version using the createVersion.
Invoke the function
Note
To allow any user to invoke your function, make it public. For more information about access permissions, see Access management in Cloud Functions.
- In the management console
, navigate to the folder containing the function. - Go to Cloud Functions.
- Select the function.
- Navigate to the
Testing tab. - In the Version tag field, specify
$latestto invoke the latest function version. - In the Payload template field, select
No template. - In the Payload field, enter:
{"queryStringParameters": {"name": "Username"}} - Click
Run test. - You will see the test status under Test result in the Function status field. Note that the maximum function execution time before timeout, including first call initialization, is ten minutes.
- 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 do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
To invoke a function, run this 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 find the function invocation link on the Overview tab, in the Link to invoke field.
For security reasons, you can only invoke a function via HTTPS. Invoke it as a regular HTTP request by pasting the link into the browser address bar and adding the name parameter to the URL:
https://functions.yandexcloud.net/<function_ID>?name=Username
The following response will appear on the page:
Hello, Username!
What's next
- To learn more about the function structure required for different invocation methods (HTTP and CLI), see Invoking a function in Cloud Functions.
- Read about service concepts.
- Check out our step-by-step guides to learn what you can do with functions and their versions.