Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex Managed Service for YDB
  • Getting started
  • Step-by-step instructions
    • Overview
    • Database management via the console Yandex Cloud
    • Database management using the YC CLI
    • Connecting to a database using the YDB CLI
    • Managing tables and directories
    • Reading and writing data
    • Monitoring DB status
  • Practical guidelines
    • Deploying a web application
    • Developing a Slack bot
    • Connecting to YDB from Yandex Cloud Functions function in Python
    • Connecting to a YDB database from Yandex Cloud Functions function in Node.js
    • Converting a video to a GIF in Python
    • Developing a skill for Alice and a website with authorization
  • Concepts
    • Overview
    • Terms and definitions
    • Serverless and Dedicated operation modes
    • DynamoDB tables
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
      • Pricing policy for serverless mode
      • Request cost for YQL
      • Request cost for the Document API
      • Request cost for special APIs
      • Cost of topic operations
    • Dedicated mode
  • Amazon DynamoDB-compatible Document API
    • Tools
      • Setting up AWS tools
      • Working with data through the HTTP interface
      • Working with the AWS CLI
        • Overview
        • Creating a table
        • Adding data to a table
        • Reading data from a table
        • Updating data
        • Data selections
        • Deleting created resources
      • Working with the AWS SDK
        • Overview
        • Creating a table
        • Uploading data to a table
        • Managing records in a table
          • Creating a record
          • Reading a record
          • Updating a record
          • Deleting a record
        • Searching and extracting data
        • Deleting a table
    • Document API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • DescribeTimeToLive
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
        • UpdateTimeToLive
    • Common errors when working with the Document API
  • API Reference Yandex Cloud for Managed Service for YDB
    • Overview
    • Authentication in the API
    • gRPC
      • Overview
      • BackupService
      • DatabaseService
      • LocationService
      • ResourcePresetService
      • StorageTypeService
      • OperationService
    • REST
      • Overview
      • Backup
        • Overview
        • delete
        • get
        • list
        • listPaths
      • Database
        • Overview
        • backup
        • create
        • delete
        • get
        • list
        • move
        • restore
        • start
        • stop
        • update
      • Location
        • Overview
        • get
        • list
      • ResourcePreset
        • Overview
        • get
        • list
      • StorageType
        • Overview
        • get
        • list
  • Questions and answers
  1. Practical guidelines
  2. Deploying a web application

Deploying a web application using the Java Servlet API

Written by
Yandex Cloud
  • Before you begin
    • Required paid resources
  • Prepare the environment
  • Create a Object Storage bucket
  • Create a Managed Service for YDB database
  • Create Cloud Functions functions
  • Create an API gateway
  • Test the application
  • How to delete created resources

Learn how to use serverless technologies and the Java Servlet API to create a simple web application for managing a task list.

To create a web application:

  1. Before you start.
  2. Prepare the environment.
  3. Create a Yandex Object Storage bucket.
  4. Create a YDB database.
  5. Create functions Yandex Cloud Functions.
  6. Create an API gateway.
  7. Test your application.

If you no longer need these resources, delete them.

Before you begin

Before working, you need to register in Yandex Cloud and create a billing account:

  1. Go to the management console. Then log in to Yandex Cloud or sign up if don't already have an account.
  2. On the billing page, make sure you linked a billing account, and it has the ACTIVE or TRIAL_ACTIVE status. If you don't have a billing account, create one.

If you have an active billing account, you can create or select a folder to run your VM in from the Yandex Cloud page.

Learn more about clouds and folders.

Required paid resources

The cost of resources to support a web application includes:

  • A fee for the number of requests to the API gateway and outgoing traffic (see Yandex API Gateway pricing).
  • A fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
  • A fee for the number of function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).

Prepare the environment

  1. Download the archive with project files.
  2. Create a service account and assign it the viewer and editor roles for your folder.

Create a Object Storage bucket

Create a bucket and upload index.html there:

Management console
  1. In the management console, select the folder where you want to create a bucket.
  2. Select Object Storage.
  3. Click Create bucket.
  4. On the bucket creation page:
    1. Enter the bucket name, following the naming guidelines.
    2. If necessary, limit the maximum bucket size.
    3. Select the type of access.
    4. Select the default storage class.
    5. Click Create bucket to complete the operation.
  5. Select the created bucket.
  6. Click Upload.
  7. In the window that opens, in the project folder, select the src/main/resources/index.html file and click Open.
  8. Select the storage class for the file and click Upload.

Create a Managed Service for YDB database

  1. Create a database in Serverless mode:

    Management console
    1. In the management console, select the folder where you created the bucket.

    2. Select Managed Service for YDB.

    3. Click Create database.

    4. Enter the database Name. Naming requirements:

      • The length can be from 3 to 63 characters.
      • It may contain lowercase Latin letters, numbers, and hyphens.
      • The first character must be a letter. The last character can't be a hyphen.
    5. Under Database type, select Serverless.

    6. Click Create database.

    7. Wait until the database starts. When a database is being created, it has the Provisioning status. When it's ready for use, the status changes to Running.

    8. Select the database created.

    9. Under Connection, find the Endpoint and Database location fields and save their values. You'll need them when creating functions.

  2. Create a table named Tasks:

    Management console
    CLI
    1. In the management console, select the folder where you created the database.

    2. Select Managed Service for YDB.

    3. Select a database on the Databases page.

    4. To open the DB root directory, go to the Navigation tab.

    5. To make a query to the database, click SQL query in the upper-right corner. The Query page opens.

    6. In the Query field, enter:

      CREATE TABLE Tasks (
        TaskId Utf8,
        Name Utf8,
        Description Utf8,
        CreatedAt Datetime,
        PRIMARY KEY (TaskId)
      );
      
    7. Click Run.

    Run the query:

    ydb -e grpcs://<YDB endpoint> -d <database> \
    scripting yql -s \
    "CREATE TABLE Tasks
    (
      TaskId Utf8,
      Name Utf8,
      Description Utf8,
      CreatedAt Datetime,
      PRIMARY KEY (TaskId)
    );"
    

Create Cloud Functions functions

Create a function for each servlet:

Management console
CLI
API
Yandex Cloud Toolkit
  1. In the management console, go to the folder where you created the bucket and database.
  2. Select Cloud Functions.
  3. Click Create function.
  4. Enter the name add-task and a function description.
  5. Click Create.
  6. Under Editor, select the Java 11 runtime environment and click Continue.
  7. Prepare the function code. For this, in the Method field, select ZIP archive, and specify the path to the downloaded servlet.zip archive, then click Open.
  8. In the Entry point field, enter yandex.cloud.examples.serverless.todo.AddTaskServlet.
  9. In the Timeout, sec field, enter 5.
  10. In the Service account field, enter the account that you created when preparing the environment.
  11. Add environment variables:
    • ENDPOINT: Enter the name of the Endpoint field that you saved when creating the YDB database.
    • DATABASE: Enter the value from the Database location field that you also saved when creating your YDB database.
  12. Click Create version.
  13. Repeat steps 3–12 and create a function named list-tasks with the entry point yandex.cloud.examples.serverless.todo.ListTasksServlet.
  14. Repeat steps 3–12 and create a function named delete-task with the entry point yandex.cloud.examples.serverless.todo.DeleteTaskServlet.
  1. Create a function named add-task:

    yc serverless function create --name=add-task
    

    Result:

    id: d4ejb1799eko6re4omb1
    folder_id: aoek49ghmknnpj1ll45e
    created_at: "2021-03-08T14:07:32.134Z"
    name: add-task
    log_group_id: eolm8aoq9vcppsieej6h
    http_invoke_url: https://functions.yandexcloud.net/d4ejb1799eko6re4omb1
    status: ACTIVE
    
  2. Create a version of the add-task function:

    yc serverless function version create \
      --function-name=add-task \
      --runtime java11 \
      --entrypoint yandex.cloud.examples.serverless.todo.AddTaskServlet \
      --memory 128m \
      --execution-timeout 5s \
      --source-path ./servlet.zip \
      --environment DATABASE=<database>,ENDPOINT=<YDB endpoint>
    

    Where:

    • function-name: The name of the function you want to create a version of.
    • runtime: The runtime environment.
    • entrypoint: The entry point specified in the <function file name>.<handler name> 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.
    • environment: Environment variables in key=value format.

    Result:

    done (1s)
    id: d4evvn8obisajd51plaq
    function_id: d4ejb1799eko6re4omb1
    ...
    tags:
    - $latest
    log_group_id: ckg3qh8h363p40gmr9gn
    
  3. Repeat steps 1–2 and create a function named list-tasks with the entry point yandex.cloud.examples.serverless.todo.ListTasksServlet.

  4. Repeat steps 1–2 and create a function named delete-task with the entry point yandex.cloud.examples.serverless.todo.DeleteTaskServlet.

Use the create and the createVersion API methods.

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

Create an API gateway

To ensure interaction between services, create an API gateway:

Management console
CLI
Yandex Cloud Toolkit
  1. In the management console, select the folder where you created your bucket, database, and functions.

  2. Select API Gateway.

  3. Click Create API gateway.

  4. Enter a name and description of the gateway.

  5. In the Specification field, add the specification:

    openapi: 3.0.0
    info:
      title: ToDo list
      version: 1.0.0
    paths:
      /:
        get:
          x-yc-apigateway-integration:
            type: object-storage
            bucket: <bucket>
            object: index.html
            presigned_redirect: false
            service_account: <service account>
          operationId: static
      /add:
         post:
           x-yc-apigateway-integration:
             type: cloud-functions
             function_id: <add-task ID>
           operationId: addTask
      /list:
        get:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <list-tasks ID>
          operationId: listTasks
      /delete:
        delete:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <delete-task ID>
          operationId: deleteTask
    

    Where:

    • bucket: Name of the bucket containing index.html.
    • service_account: ID of the service account created when preparing the environment.
    • /add section, function_id parameter: ID of the add-task function.
    • /list section, function_id parameter: ID of the list-tasks function.
    • /delete section, function_id parameter: ID of the delete-task function.
  6. Click Create.

  1. Save the following specification to todo.yaml:

    openapi: 3.0.0
    info:
      title: ToDo list
      version: 1.0.0
    paths:
      /:
        get:
          x-yc-apigateway-integration:
            type: object-storage
            bucket: <bucket>
            object: index.html
            presigned_redirect: false
            service_account: <service account>
          operationId: static
      /add:
        post:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <add-task ID>
          operationId: addTask
      /list:
        get:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <list-tasks ID>
          operationId: listTasks
      /delete:
        delete:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <delete-task ID>
          operationId: deleteTask
    

    Where:

    • bucket: Name of the bucket containing index.html.
    • service_account: ID of the service account created when preparing the environment.
    • /add section, function_id parameter: ID of the add-task function.
    • /list section, function_id parameter: ID of the list-tasks function.
    • /delete section, function_id parameter: ID of the delete-task function.
  2. Create an API gateway:

    yc serverless api-gateway create \
      --name todo-list \
      --spec=todo.yaml \
      --description "simple todo list"
    

    Result:

    done (41s)
    id: d5delqeel34qjjfcdj81
    folder_id: b1g86q4m5vej8lkljme5
    created_at: "2021-03-08T14:07:32.134Z"
    name: todo-list
    description: simple todo list
    status: ACTIVE
    domain: d5delqeel34qjjfcdj81.apigw.yandexcloud.net
    log_group_id: ckg2hdmevnvcngprqvqb
    

You can create an API gateway using the Yandex Cloud Toolkit plugin for the family of IDEs on the IntelliJ platform from JetBrains.

Test the application

To open the app, follow the link in the Service domain field of the created API gateway.

How to delete created resources

To stop paying for the resources created:

  • Delete the bucket.
  • Delete the database.
  • Delete the functions.
  • Delete the API gateway.

Was the article helpful?

Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
In this article:
  • Before you begin
  • Required paid resources
  • Prepare the environment
  • Create a Object Storage bucket
  • Create a Managed Service for YDB database
  • Create Cloud Functions functions
  • Create an API gateway
  • Test the application
  • How to delete created resources