How to manage Huawei Cloud DB in Cloud Functions environment ? | Python

Mustafa Sarıkaya
Huawei Developers
Published in
7 min readJan 26, 2023
Cloud Function

Introduction

Hello everyone,

In this article, we will see how we can manage Huawei Cloud DB Python Server Sdk without using a physical server, using the Cloud Function python runtime. First, I want to talk about the services that I use in this application. These are: Cloud DB and Cloud Function.

What is Cloud DB?

Cloud DB is a database that provides seamless data synchronization between devices and between devices, and allows us to perform offline application operations.

Cloud DB uses an Object-based data storage structure. Data is stored as objects in different DB Zone regions. Each object is a data record. Cloud DB Zone is an independent storage area.

Object Type: Represents each table in classical database logic.

Cloud DB Zone: Represents the data region on the cloud side. It corresponds to the database name.

What is Cloud Function?

Cloud Functions enables serverless computing. It provides the Function as a Service (FaaS) capabilities to simplify app development and O&M by splitting service logic into functions and offers the Cloud Functions SDK that works with Cloud DB and Cloud Storage so that your app functions can be implemented more easily. Cloud Functions automatically scales in or out server resources for functions based on actual traffic, freeing you from server resource management and reducing your O&M workload.

Getting Started

I assume that you have created a Cloud DB table and Objects. If you haven’t, you can read the Cloud DB documents and follow the steps. I will also show alternative ways.

Note

How can I be a Huawei Developer?

To become a Huawei developer, you need to first register and create a developer account at https://developer.huawei.com/consumer/en/. You can create the account as an individual or as a company.

How long does it take for Huawei Developer account to be reviewed and approved?

Make sure you enter the information correctly when creating the developer account. The account will be approved and active within one or two business days.

Creating Your Project and Adding an Application

If you haven’t added a project in AppGallery Connect, log in to AppGallery Connect, create a project, and add an application to the project.

Activating Cloud Function

  1. Log in to AppGallery Connect’ and click My projects.
  2. Find the project where you need to enable Cloud Functions from the project list.
  3. Click Build > Cloud Functions, and click the Enable now button in the top right corner.
Cloud Function Enable Screen

Now we are ready to create a Cloud Function. Now we need a Cloud DB python server package.

Note: If you want to manage Cloud DB through Cloud Function, you cannot use the edit code inline option, in this case, you should use the upload .zip file option. In fact, this .zip file is the Cloud DB python server application.

Creating the Cloud DB Server Package

The server code should be coded in Python. After coding, we will create a .zip package to upload to the Cloud Function, the contents of the .zip package should be as follows.

Latest Folder Structure

Let’s take a look at the contents of this zip.

The python_modules folder contains the AppGallery Connect Python Server SDK. This is created by running the following command.

pip3 install -t ./python_modules agconnect

The other file, agconnect_credentials.json, is the file you obtained by referring to the Project-level Identity Information. Save your agc-apiclient-*.json credential file to the specified location.

The cloud_db_zone_wrapper allows you to execute_query, execute_upsert, execute_delete, order_by_asc, limit, etc. with the Python SDK on Cloud DB in an asynchronous way, sorting and limiting the data records to be displayed with the python server sdk.

The code in the get_books_list function, as you can see from the comments in the code, is used to create a table in Cloud DB.

Exporting Cloud DB Tables to the Project

You can export the tables you created in Cloud DB as JSON and create a file similar to book_info.py using the python sdk. This file contains all the information of your tables, including all indexes and rules.

You can use the object_creator script in your project to create the objects you want to use in the cloud db service. To do this, simply specify the path of your json file and the path where you want to place your model. Run the command in the following format:

python object_creator.py your_json_path your_model’s_path

Another way is to make sure that the File format is JS and the File Type of js is set to serverSDK. You can also export it as js and convert it to python.

File Format Screen

Creating a Python Async Lambda Handler Function

If you have completed all the steps so far, when you look at the .zip image, all parts should be complete except for handler.py and nest_asyncio.py. In this section, the Cloud function environment is expecting a myHandler method in the following structure. handler.py:

Here, the return statement is optional and you can return whatever you want.

If you notice, there is a statement called nest_asyncio. The reason for using this code snippet is that, by design, asyncio does not allow nested loops. This creates a problem. It is impossible to run tasks and wait for the result in an environment where the loop is already running. Therefore, this module patches asyncio to allow nested usage. You have to first create a new loop, then patch it using nest_apply, and finally run it using run_until_complete.

To use the nest_asyncio module, you can install it using pip into the python modules or, as you can see in the zip file, you can directly include the source file in the zip and call the apply method.

Additionally, the myHandler function should not be async. The async_handler method requires the async keyword.

You can create a method like the one in the image above. You can also use the context for logging during runtime. You can also import the modules like the example, imports like CloudDbZoneWrapper, asyncio are required.

Uploading the created package to the Cloud Function

Once all the steps are completed, we need to upload our .zip file to the cloud function environment and run it..

From the AG Connect page > My Project > Build > Cloud Functions page, click on the New Function button.

Create Function Screen
  1. Enter the name of the function.
  2. Upload the .zip file that we previously created.
  3. This is important, myHandler is our main method, it’s in the handler.py file.

4. Click the Save button and wait until the process is complete.

After saving, click on the created function, the page that opens will look like this, click on the code tab and press the test button.

Test Run Screen

When the code runs successfully, you will see a log similar to this one.

Remember!

The logs here are thrown based on how you set the code in the handler.py with the context.

Result Screen

As you can see, I just printed the name of an element of the book list and the value returned from query_average() as a log only.I also returned the average value.

Note: If you don’t see any logs, there’s probably a syntax error or an exception error in the code. To fix this issue, enclose all function returns in a try {} except{} block and print the error message. Check the code and add logs to each line if necessary, and finally, upload the package again in its final form to make sure the code runs successfully.

Caution

The point to be careful here is that asyncio is used specifically in AGC Python SDK, and if you want to work with this server in an environment, asyncio doesn’t allow running another loop on a running loop and waiting for the result. That’s why you need to use nest_asyncio to make clouddb transaction operations async in this example. If you don’t, you might get errors similar to the ones above.

RuntimeError: JSON serialization of response: <coroutine object handler

RuntimeWarning: coroutine 'handler' was never awaited

RuntimeError: no current loop thread'/O'

RuntimeError: This event loop is already running.

Conclusion

We have seen how to create a Cloud DB server package, upload the created package to Cloud Function and run it. In this way, we were able to complete all the operations through Cloud Function without the need for a physical server.

Thank you for reading…

For more information, you can check out the xHuawei developer documentation.

References

Huawei Cloud DB

Huawei Cloud Function

--

--