Deploying a web application using the Java Servlet API
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:
- Before you start.
- Prepare the environment.
- Create a 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 these resources, delete them.
Before you begin
Before working, you need to register in Yandex Cloud and create a billing account:
- Go to the management console. Then log in to Yandex Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
ACTIVE
orTRIAL_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
- Download the archive with project files.
- Create a service account and assign it the
viewer
andeditor
roles for your folder.
Create a Object Storage bucket
Create a bucket and upload index.html
there:
- 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 the bucket name, following the naming guidelines.
- If necessary, limit the maximum bucket size.
- Select the type of access.
- Select the default storage class.
- Click Create bucket to complete the operation.
- Select the created bucket.
- Click Upload.
- In the window that opens, in the project folder, select the
src/main/resources/index.html
file and click Open. - 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 database.
-
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.
-
Under Database type, select Serverless.
-
Click Create database.
-
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 toRunning
. -
Select the database created.
-
Under Connection, find the Endpoint and Database location fields and save their values. You'll need them 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 SQL query in the upper-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 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:
- In the management console, go to the folder where you created the bucket and database.
- Select Cloud Functions.
- Click Create function.
- Enter the name
add-task
and a function description. - Click Create.
- Under Editor, select the Java 11 runtime environment and click Continue.
- 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. - In the Entry point field, enter
yandex.cloud.examples.serverless.todo.AddTaskServlet
. - In the Timeout, sec field, enter
5
. - In the Service account field, enter the account that you created when preparing the environment.
- 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.
- Click Create version.
- Repeat steps 3–12 and create a function named
list-tasks
with the entry pointyandex.cloud.examples.serverless.todo.ListTasksServlet
. - Repeat steps 3–12 and create a function named
delete-task
with the entry pointyandex.cloud.examples.serverless.todo.DeleteTaskServlet
.
-
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
-
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
-
Repeat steps 1–2 and create a function named
list-tasks
with the entry pointyandex.cloud.examples.serverless.todo.ListTasksServlet
. -
Repeat steps 1–2 and create a function named
delete-task
with the entry pointyandex.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:
-
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 created when preparing the environment./add
section,function_id
parameter: ID of theadd-task
function./list
section,function_id
parameter: ID of thelist-tasks
function./delete
section,function_id
parameter: ID of thedelete-task
function.
-
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 created when preparing the environment./add
section,function_id
parameter: ID of theadd-task
function./list
section,function_id
parameter: ID of thelist-tasks
function./delete
section,function_id
parameter: ID of thedelete-task
function.
-
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: