Creating a function in Go
Create and execute a function in Go that welcomes the user.
Before you start
Create a folder in Yandex.Cloud.
Create a function
- In the management console, go to the folder where you want to create a function.
- Open Cloud Functions.
- Click Create function.
- Enter a name for the function:
go-function
. - 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=go-function
Result:
id: b09bhaokchn9pnbrlseb
folder_id: aoek49ghmknnpj1ll45e
created_at: "2019-06-14T10:03:37.475Z"
name: go-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
-
Save the following code to a file named
hello.go
:package main import ( "encoding/json" ) type JSONString string func (j JSONString) MarshalJSON() ([]byte, error) { return []byte(j), nil } func Handler() ([]byte, error) { s := `{"body": "Hello, World!"}` return json.Marshal(JSONString(s)) }
-
Add the
hello.go
file to thehello-go.zip
archive.
Create a function version
- In the management console, go to the folder where the function is located.
- Select Cloud Functions.
- Select the function named
go-function
. - Under Latest version, click Create in editor.
- Set the version parameters:
- Runtime environment:
golang114
. - Method: ZIP archive.
- File:
hello-go.zip
. - Entry point:
hello.Handler
. - Timeout, seconds: 3.
- RAM: 128 MB.
- Service account: Not selected.
- Runtime environment:
- 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=go-function \
--runtime golang114 \
--entrypoint hello.Handler \
--memory 128m \
--execution-timeout 3s \
--source-path ./hello-go.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: golang114
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.
- In the management console, go to the folder where the function is located.
- Select Cloud Functions.
- Select a function.
- Go to the Testing tab.
- In the Tag version field, specify
$latest
to invoke the latest function version. - Under Payload template, choose Without template.
- 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 initial launch) is 10 minutes.
- You will see the function execution result in the Function output field.
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>
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:
https://functions.yandexcloud.net/b09bhaokchn9pnbrlseb
The following response appears on the page:
Hello, World!
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.