Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
      • Overview
      • Node.js
      • Python
      • Go
      • PHP
      • Bash
      • Java
      • Kotlin
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

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
  • Invoke the function
  • What's next
  1. Getting started
  2. Creating a function
  3. Java

Creating a function in Java

Written by
Yandex Cloud
Improved by
amatol
Updated at May 13, 2025
  • 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
  • Invoke the function
  • What's next

Create and execute a function in Java that welcomes the user.

Before you startBefore you start

Create a folder in Yandex Cloud.

Create a functionCreate a function

Management console
CLI
API
Yandex Cloud Toolkit
  1. In the management console, select the folder where you want to create a function.
  2. Select Cloud Functions.
  3. Click Create function.
  4. Enter the function name: java-function.
  5. Click Create.

If you do not have the Yandex Cloud (CLI) command line interface yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. 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=java-function

Result:

id: b09bhaokchn9********
folder_id: aoek49ghmknn********
created_at: "2019-06-14T10:03:37.475Z"
name: java-function
log_group_id: eolm8aoq9vcp********
http_invoke_url: https://functions.yandexcloud.net/b09bhaokchn9********
status: ACTIVE

You can create a function using the create.

You can create a list of function versions using the Yandex Cloud Toolkit plugin for the IDE family on the JetBrains IntelliJ platform.

Create the first version of the functionCreate the first version of the function

To create a function version, 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 in Cloud Functions.

Prepare a ZIP archive with the function codePrepare a ZIP archive with the function code

  1. Save the following code to a file named Handler.java:

    import java.util.HashMap;
    import java.util.Map;
    import java.util.function.Function;
    
    class Request {
        Map<String, String> queryStringParameters;
    }
    
    class Response {
        private int statusCode;
        private Map<String, String> headers;
        private Boolean isBase64Encoded;
        private String body;
    
        public Response(int statusCode, Map<String, String> headers, Boolean isBase64Encoded, String body) {
            this.statusCode = statusCode;
            this.headers = headers;
            this.isBase64Encoded = isBase64Encoded;
            this.body = body;
        }
    }
    
    public class Handler implements Function<Request, Response> {
        private Integer statusCode = 200;
        private Boolean isBase64Encoded = false;
    
        @Override
        public Response apply(Request request) {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "text/plain");
    
            String name = request.queryStringParameters.get("name");
            return new Response(statusCode, headers, isBase64Encoded, String.format("Hello, %s!", name));
        }
    }
    
  2. Add the Handler.java file into the hello-java.zip archive.

Create a function versionCreate a function version

Management console
CLI
API
Yandex Cloud Toolkit
  1. In the management console, select the folder containing the function.
  2. Select Cloud Functions.
  3. Select the java-function function.
  4. Under Last version, click Сreate in editor.
  5. Select the Java 21 runtime environment.
  6. Disable Add files with code examples and click Continue.
  7. Set the version parameters:
    • Method: ZIP archive
    • File: Attach hello-java.zip
    • Entry point: Handler
    • Timeout: 3
    • Memory: 128 MB
    • Service account: Not selected
  8. Click Save changes.

If you do not have the Yandex Cloud (CLI) command line interface yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. 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=java-function \
  --runtime java21 \
  --entrypoint Handler \
  --memory 128m \
  --execution-timeout 3s \
  --source-path ./hello-java.zip

Where:

  • --function-name: Name of the function whose version you want to create.
  • --runtime: Runtime environment.
  • --entrypoint: Entry point.
  • --memory: Amount of RAM.
  • --execution-timeout: Maximum function running time before the timeout is reached.
  • --source-path: ZIP archive with the function code and required dependencies.

Result:

done (1s)
id: d4evvn8obisa********
function_id: d4elpv8pft63********
created_at: "2020-08-01T19:09:19.531Z"
runtime: java21
entrypoint: 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.

You can create a function version using the Yandex Cloud Toolkit plugin for the IDE family on the JetBrains IntelliJ platform.

Invoke the functionInvoke 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.

Management console
CLI
HTTPS
Yandex Cloud Toolkit
  1. In the management console, select the folder containing the function.
  2. Select Cloud Functions.
  3. Select a function.
  4. Go to the Testing tab.
  5. In the Version tag field, specify $latest to invoke the latest function version.
  6. In the Payload template field, select No template.
  7. In the Payload 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: Maximum function execution time before timeout (including original initialization at first call) 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 do not have the Yandex Cloud (CLI) command line interface yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. 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 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!

You can invoke a function using the Yandex Cloud Toolkit plugin for the IDE family on the JetBrains IntelliJ platform.

What's nextWhat's next

  • For more information about the structure of functions invoked in different ways (HTTP or CLI), see Invoking a function in Cloud Functions.
  • Read about service concepts.
  • For information about what you can do with functions and their versions, see our step-by-step instructions.

Was the article helpful?

Previous
Bash
Next
Kotlin
Yandex project
© 2025 Yandex.Cloud LLC