Functiongraph python连接数据库demo

Functiongraph是华为云提供的serverless云服务,该服务可以让用户只需关注code的开发,然后在Functiongraph上部署代码就行了。这和AWS Lambda、Azure Funtions、aliyun Function Compute、google Cloud Functions等是一样的。其是容器技术提供的沙箱环境。

Functiongraph is a serverless cloud service provided by HUAWEI CLOUD. This service allows users to focus on code development and then deploy the code on Functiongraph. This is the same as AWS Lambda, Azure Funtions, aliyun Function Compute, google Cloud Functions, etc. It is a sandbox environment provided by container technology.

一、Python测试代码(Python test code)


Because the code dependency package and business code need to be packaged together, the relatively simple pymysql database package is selected here. The specific code is as follows:

[root@ecs-f0d8 test]# cat
# -*- coding:utf-8 -*-
import pymysql.cursors
def handler (event, context):
    # Connect to the database
    connection = pymysql.connect(host='host_ip',
        with connection.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
            cursor.execute(sql, ('', 'very-secret'))
        # connection is not autocommit by default. So you must commit to save
        # your changes.
        with connection.cursor() as cursor:
            # Read a single record
            sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
            cursor.execute(sql, ('',))
            result = cursor.fetchone()
        output = '^_^'
    return output

二、打包代码(Package code)


For the packaging code, please refer to the official page of huaweicloud:

[root@ecs-f0d8 tmp]# pip3 install PyMySQL --root /tmp/pymysql
[root@ecs-f0d8 site-packages]# cd /tmp/pymysql/usr/local/lib/python3.6/site-packages
[root@ecs-f0d8 site-packages]# cp -rp pymysql/ /tmp/test/
[root@ecs-f0d8 test]# ll
total 108
drwxr-xr-x 4 root root  4096 Aug 24 23:14 pymysql
-rw-r--r-- 1 root root  1150 Aug 24 23:45
[root@ecs-f0d8 test]# zip -r *

将打包好的zip包上传到functiongraph环境,上传完成后的界面如下(Upload the packaged zip file to the functiongraph environment. The web interface after uploading is as follows):


当然也可以在该页面的下拉界面通过提前上传的依赖包进行选择。Of course, you can also select the dependency package uploaded in advance in the drop-down interface of this page.

三、数据库及配置(Database and Configuration)

数据库我使用的是云服务RDS,由于FunctionGraph 所在的VPC和RDS所在的VPC不同,所以这里需要配置FunctionGraph访问VPC、RDS的委托。

I use the cloud service RDS for the database. Because the VPC where the FunctionGraph is located is different from the VPC where the RDS is located, it is necessary to configure the agencies of the FunctionGraph to access the VPC and RDS.

1. 创建数据库和表(Create Database and table)

MySQL [none]> create database testdb;
MySQL [testdb]> create table users (id INT(11) NOT NULL AUTO_INCREMENT,email varchar(255), password varchar(255), PRIMARY KEY (id));

2. 创建委托(Create Agency)


3. 开启 VPC 访问(enable vpc access):


在 configuration —- Basic Settings里设置Handler名称,名称设置为testmysql.handler,其规则是文件外.函数名。

Set the Handler name in configuration —- Basic Settings,The name is set to testmysql.handler and its rule is [filename].[function name].

注意:Configuration — Advanced Settings — Initialization可以设置初始化入口函数。( Note: Configuration — Advanced Settings — Initialization can set the initialization entry function. )


通过Configure Test Event测试 或 通过创建Trigger测试,测试效果如下(Test by Configure Test Event or by creating Trigger test, the test effect is as follows):

FunctionGraph Execute