Creating and executing functions
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 in Node.js
- Invoke the function
- What's next
Before you start
- Create a folder in Yandex.Cloud.
- Install the YC CLI command line interface.
Create a function in Python
-
In the management console, select the folder to create your function in.
-
Click Create resource.
-
Choose Function.
-
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.
-
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
-
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) }
-
Add the
main.py
file to thehello-py.zip
archive.
Create a function version
- Open Cloud Functions in the folder where you want to create the function version.
- Select the function to create the version for.
- Under Latest version, click Create in editor.
- Set the version parameters:
- Runtime environment:
python37
. - Timeout, seconds: 5.
- RAM: 128 MB.
- Service account: Not selected.
- Runtime environment:
- Prepare the function code:
- Method: ZIP archive.
- File:
hello-py.zip
. - Entry point:
main.handler
.
- 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
-
In the management console, select the folder to create your function in.
-
Click Create resource.
-
Choose Function.
-
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.
-
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
-
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}!` } };
-
Add
index.js
to thehello-js.zip
archive.
Create a function version
- Open Cloud Functions in the folder where you want to create the function version.
- Select the function to create the version for.
- Under Latest version, click Create in editor.
- Set the version parameters:
- Runtime environment:
nodejs12
. - Timeout, seconds: 5.
- RAM: 128 MB.
- Service account: Not selected.
- Runtime environment:
- Prepare the function code:
- Method: ZIP archive.
- File:
hello-js.zip
. - Entry point:
index.handler
.
- 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 |
+----------------------+--------------------+----------------------+
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.