Deploying a web application using the Java Servlet API and YDB
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:
- Prepare your cloud.
- Prepare the environment.
- Create an Yandex Object Storage bucket.
- Create a YDB database.
- Create functions Yandex Cloud Functions.
- Create an API gateway.
- Test your application.
If you no longer need the resources you created, delete them.
Getting started
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of resources to support a web application includes:
- Fee for the number of requests to the API gateway and outgoing traffic (see Yandex API Gateway pricing).
- Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
- Fee for the number of function calls, computing resources allocated to a function, and outgoing traffic (see Cloud Functions pricing).
Prepare the environment
-
Create a service account and assign it the
editor
role for your folder. -
Clone the repository
with the project:git clone https://github.com/yandex-cloud-examples/yc-serverless-servlet
-
Add the contents of the
yc-serverless-servlet
local repository you cloned to theservlet.zip
archive. You will need this archive later to create Yandex Cloud Functions functions.
Create an Object Storage bucket
Create a bucket and upload index.html
there:
- In the management console
, select the folder where you wish to create the bucket. - Select Object Storage.
- Click Create bucket.
- On the bucket creation page:
- Enter a name for the bucket according to the naming requirements.
- Limit the maximum bucket size, if required.
- In the Object read access, Object listing access, and Read access to settings fields, select
Restricted
. - Select the default storage class.
- Click Create bucket to complete the operation.
- Select the created bucket.
- Click Upload and select the
src/main/resources/index.html
file in the project folder. - Select the storage class for the file and click Upload.
Create a Managed Service for YDB database
-
Create a database in Serverless mode:
Management console-
In the management console
, select the folder where you created the bucket. -
Select Managed Service for YDB.
-
Click Create a database.
-
Enter the database Name. The naming requirements are as follows:
- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
Under Database type, select
Serverless
. -
Click Create a database.
-
Wait until the database starts. While being created, your database will have the
Provisioning
status. Once it is ready for use, its status will change toRunning
. -
Select the database created.
-
Under Connection, find the Endpoint field and save its value. You will need it when creating functions.
-
-
Create a table named
Tasks
:Management consoleCLI-
In the management console
, select the folder where you created the database. -
Select Managed Service for YDB.
-
Select a database on the Databases page.
-
To open the DB root directory, go to the Navigation tab.
-
To make a query to the database, click New SQL query in the top-right corner. The Query page opens.
-
In the Query field, enter:
CREATE TABLE Tasks ( TaskId Utf8, Name Utf8, Description Utf8, CreatedAt Datetime, PRIMARY KEY (TaskId) );
-
Click Run.
Run the following query:
ydb -e grpcs://<YDB_endpoint> -d <db_name> \ 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:
- In the management console
, go to the folder where you created the bucket and database. - Select Cloud Functions.
- Click Create function.
- Enter a name, e.g.,
add-task
, and description for the function. - Click Create.
- Under Editor, select the
java21
runtime environment, disable Add files with code examples and click Continue. - Prepare the function code. To do this, select
ZIP archive
in the Method field. - In the File field, click Attach file and select the
servlet.zip
archive created earlier. - In the Entry point field, enter
yandex.cloud.examples.serverless.todo.AddTaskServlet
. - Set Timeout, sec to
10
. - In the Service account field, enter the account that you created when preparing the environment.
- Add environment variables:
ENDPOINT
: Enter the first part of the Endpoint field value saved when creating the YDB database (the one betweengrpcs://
and/?database=
), e.g.,ydb.serverless.yandexcloud.net:2135
.DATABASE
: Enter the second part of the Endpoint field value saved when creating the YDB database (the one following/?database=
), e.g.,/ru-central1/r1gra875baom********/g5n22e7ejfr1********
.
- Click Save changes.
- On the Overview page, enable Public function.
- Repeat steps 3-14 and create a function named
list-tasks
with theyandex.cloud.examples.serverless.todo.ListTasksServlet
entry point. - Repeat steps 3-14 and create a function named
delete-task
with theyandex.cloud.examples.serverless.todo.DeleteTaskServlet
entry point.
-
Create a function named
add-task
:yc serverless function create --name=add-task
Result:
id: d4ejb1799eko******** folder_id: aoek49ghmknn******** created_at: "2021-03-08T14:07:32.134Z" name: add-task log_group_id: eolm8aoq9vcp******** http_invoke_url: https://functions.yandexcloud.net/d4ejb1799eko******** status: ACTIVE
-
Create a version of the
add-task
function:yc serverless function version create \ --function-name=add-task \ --runtime java21 \ --entrypoint yandex.cloud.examples.serverless.todo.AddTaskServlet \ --memory 128m \ --execution-timeout 10s \ --source-path ./servlet.zip \ --environment DATABASE=<db_name>,ENDPOINT=<YDB_endpoint>
Where:
--function-name
: Name of the function a version of which you want to create.--runtime
: Runtime environment.--entrypoint
: Entry point in the following format:<function_file_name>
.<handler_name>
.--memory
: Amount of RAM.--execution-timeout
: Maximum function running time before the timeout is reached.--source-path
: Path to the previously createdservlet.zip
archive with the function code and required dependencies.--environment
: Environment variables inkey=value
format.
Result:
done (1s) id: d4evvn8obisa******** function_id: d4ejb1799eko******** ... tags: - $latest log_group_id: ckg3qh8h363p********
-
Make the function public:
yc serverless function allow-unauthenticated-invoke add-task
Result:
done (1s)
-
Repeat steps 1-3 and create a function named
list-tasks
with theyandex.cloud.examples.serverless.todo.ListTasksServlet
entry point. -
Repeat steps 1-3 and create a function named
delete-task
with theyandex.cloud.examples.serverless.todo.DeleteTaskServlet
entry point.
Use the create, createVersion, and setAccessBindings API methods for the Function resource.
You can create a function and its version using the Yandex Cloud Toolkit plugin
Create an API gateway
To ensure interaction between services, create an API gateway:
-
In the management console
, select the folder where you created your bucket, database, and functions. -
Select API Gateway.
-
Click Create API gateway.
-
Enter a name and description of the gateway.
-
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 containingindex.html
.service_account
: ID of the service account you created when preparing the environment./add
section,function_id
parameter:add-task
function ID./list
section,function_id
parameter:list-tasks
function ID./delete
section,function_id
parameter:delete-task
function ID.
-
Click Create.
-
Save the following specification to the
todo.yaml
file: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 containingindex.html
.service_account
: ID of the service account you created when preparing the environment./add
section,function_id
parameter:add-task
function ID./list
section,function_id
parameter:list-tasks
function ID./delete
section,function_id
parameter:delete-task
function ID.
-
Create an API gateway:
yc serverless api-gateway create \ --name todo-list \ --spec=todo.yaml \ --description "simple todo list"
Result:
done (41s) id: d5delqeel34q******** folder_id: b1g86q4m5vej******** created_at: "2021-03-08T14:07:32.134Z" name: todo-list description: simple todo list status: ACTIVE domain: d5delqeel34q********.apigw.yandexcloud.net log_group_id: ckg2hdmevnvc********
You can create an API gateway using the Yandex Cloud Toolkit plugin
Test the application
To open the app, follow the link in the Default domain field of the created API gateway.
How to delete the resources you created
To stop paying for the resources you created: