Deploying a web application using the Java Servlet API
Learn how to use serverless technology and Java Servlet API to create a simple web application for managing a task list.
To create a web app:
- Get your cloud ready.
- Set up your environment.
- Create a Yandex Object Storage bucket.
- Create a YDB.
- Create 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 in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - 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 and link a cloud to it.
If you have an active billing account, you can navigate 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 outbound traffic (see Yandex API Gateway pricing).
- Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
- Fee for function invocation count, computing resources allocated to run the function, and outbound traffic (see Cloud Functions pricing).
Set up your 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.
Create an Object Storage bucket
Create a bucket and upload index.html
into it:
- In the management console
, select the folder where you want to create a bucket. - Select Object Storage.
- Click Create bucket.
- On the bucket creation page:
- Enter a name for the bucket as per 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 bucket you created.
- Click Upload and select
src/main/resources/index.html
in the project directory. - Select the storage class for the file and click Upload.
Create a database in Managed Service for YDB
-
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.
-
Specify a Name for the database. Follow these naming requirements:
- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
Under Database type, select
Serverless
. -
Click Create a database.
-
Wait until the database is up and running. While being created, your database will have the
Provisioning
status. Once it is ready for use, its status will change toRunning
. -
Select the created database.
-
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 the database on the Databases page.
-
To open the DB root directory, navigate to the Navigation tab.
-
To make a database query, click New SQL query in the top-right corner. The Query page will open.
-
In the Query field, enter:
CREATE TABLE Tasks ( TaskId Utf8, Name Utf8, Description Utf8, CreatedAt Datetime, PRIMARY KEY (TaskId) );
-
Click Run.
Run this request:
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
Create a function for each servlet:
- In the management console
, navigate to the folder where you created the bucket and the database. - Select Cloud Functions.
- Click Create function.
- Enter a name, e.g.,
add-task
, and description for the function. - Click Create.
- Under Editor, select
java21
as the runtime environment, disable Add files with code examples, and click Continue. - Set up the function code. To do this, select
ZIP archive
in the Method field. - In the File field, click Attach file and select
servlet.zip
you created earlier. - In the Entry point field, enter
yandex.cloud.examples.serverless.todo.AddTaskServlet
. - Set Timeout to
10
. - In the Service account field, specify the account you created when setting up your environment.
- Add these environment variables:
ENDPOINT
: Enter the first part of the Endpoint field value you saved when creating your YDB (the one betweengrpcs://
and/?database=
), e.g.,ydb.serverless.yandexcloud.net:2135
.DATABASE
: Enter the second part of the Endpoint field value you saved when creating your YDB (the one following/?database=
), e.g.,/ru-central1/r1gra875baom********/g5n22e7ejfr1********
.
- Click Save changes.
- On the Overview page, enable Public function.
- Repeat steps 3 through 14 to create a function named
list-tasks
with theyandex.cloud.examples.serverless.todo.ListTasksServlet
entry point. - Repeat steps 3 through 14 to 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 whose version you want to create.--runtime
: Runtime environment.entrypoint
: Entry point in<function_file_name>
.<handler_name>
format.--memory
: Amount of RAM.--execution-timeout
: Maximum function running time before timeout.--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 through 3 to create a function named
list-tasks
with theyandex.cloud.examples.serverless.todo.ListTasksServlet
entry point. -
Repeat steps 1 through 3 to 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 Yandex Cloud Toolkit
Create an API gateway
To enable communication between the 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 for the gateway.
-
In the Specification field, add this 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 setting up your 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
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 containingindex.html
.service_account
: ID of the service account you created when setting up your 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: d5dm1lba80md********.i9******.apigw.yandexcloud.net log_group_id: ckg2hdmevnvc********
You can create an API gateway using Yandex Cloud Toolkit
Test the application
To open the app, follow the link in the Default domain field of the API gateway you created.
How to delete the resources you created
To stop paying for the resources you created: