In Function Compute, you can use a function to call database APIs to insert or query data. In most cases, instance status is not shared among different execution environments in Function Compute. If you want to share instance status, you need to store structured data in a database. This topic describes how to deploy a function to access an ApsaraDB RDS for MySQL database by using Serverless Devs. Python 3 is used in this topic.
Prerequisites
- Create an ApsaraDB RDS for MySQL instanceNotice
-
Make sure that the database instance that you create is in the same region as the function that needs to access the database instance.
-
We recommend that you create the database instance in a zone that Function Compute supports.
If your resources are not deployed in the zones shown in the following list, create a vSwitch in your virtual private cloud (VPC). The vSwitch must be in the same zone as Function Compute. Also, you must specify the vSwitch ID when you configure the VPC for Function Compute. vSwitches in the same VPC can communicate with each other over the private network. Therefore, Function Compute can use the vSwitch to access resources in VPCs that reside in other zones. For more information, see How can I resolve the "vSwitch is in unsupported zone" error?.
Region Region ID Zone China (Hangzhou) cn-hangzhou cn-hangzhou-f, cn-hangzhou-g, cn-hangzhou-h China (Shanghai) cn-shanghai cn-shanghai-b, cn-shanghai-e, cn-shanghai-g, cn-shanghai-f China (Qingdao) cn-qingdao cn-qingdao-c China (Beijing) cn-beijing cn-beijing-h, cn-beijing-c, cn-beijing-e, cn-beijing-f China (Zhangjiakou) cn-zhangjiakou cn-zhangjiakou-b, cn-zhangjiakou-a China (Hohhot) cn-huhehaote cn-huhehaote-a, cn-huhehaote-b China (Shenzhen) cn-shenzhen cn-shenzhen-e, cn-shenzhen-d China (Chengdu) cn-chengdu cn-chengdu-a, cn-chengdu-b China (Hong Kong) cn-hongkong cn-hongkong-c Singapore (Singapore) ap-southeast-1 ap-southeast-1a, ap-southeast-1b Australia (Sydney) ap-southeast-2 ap-southeast-2a, ap-southeast-2b Malaysia (Kuala Lumpur) ap-southeast-3 ap-southeast-3a Indonesia (Jakarta) ap-southeast-5 ap-southeast-5a, ap-southeast-5b Japan (Tokyo) ap-northeast-1 ap-northeast-1b, ap-northeast-1a UK (London) eu-west-1 eu-west-1a Germany (Frankfurt) eu-central-1 u-central-a, eu-central-1a, eu-central-1b US (Silicon Valley) us-west-1 us-west-1a, us-west-1b US (Virginia) us-east-1 us-east-1b, us-east-1a India (Mumbai) ap-south-1 ap-south-1a, ap-south-1b
-
- Create databases and accounts for an ApsaraDB RDS for MySQL instance
- Create a table named users in an existing database.
In this example, you can use the
s init
command to query all data in the users table in the database. You must create the users table in advance. - Install Serverless Devs and Docker
- Configure Serverless Devs
- Create a security group
Create a default security group in the VPC where the database instance is deployed. If you want to use an existing security group, make sure that the security group allows outbound traffic to the ports and private CIDR blocks of the database instance. By default, the security group allows all outbound traffic.
Configure an IP address whitelist for a database
We recommend that you use an IP address whitelist, instead of security groups, for secure access. Otherwise, the function connection may be unstable, which affects your business.
Deploy and debug a function