Updated on 2025-08-22 GMT+08:00

Introduction

Scenario

In FunctionGraph, different function instances do not share states. Databases can persistently store structured data, which achieves state sharing. FunctionGraph can be used to access cloud databases to query and insert data.

This section describes how to access RDS for MySQL from FunctionGraph and query data, and provides sample code for testing. The sample uses a database connection pool and retry mechanism to improve performance and reliability, demonstrating a secure and efficient way to work with RDS for MySQL in FunctionGraph.

Resource and Cost Planning

Table 1 describes the resources required for the practice of accessing RDS for MySQL from FunctionGraph.

Table 1 Resource and cost planning

Resource

Description

Billing

FunctionGraph

  • Function type: Event Function
  • Region: CN East-Shanghai1
  • Quantity: 1
  • Billing mode: Pay-per-use
  • The first 1 million invocations are free of charge in a month. For details about the billing items, see Pay-per-Use Billing.

RDS

  • Region: CN East-Shanghai1
  • Engine Options: MySQL
  • Quantity: 1
  • Billing mode: Pay-per-use
  • Select specifications based on service requirements. For details about billing items and standards, see RDS for MySQL Pay-per-Use Billing.

VPC

  • Region: CN East-Shanghai1
  • Number of subnets: 1
  • Number of security groups: 1
  • Quantity: 1
  • VPC: free
  • Subnet: free
  • Security group: free

Procedure

This following describes the procedure for using a FunctionGraph function to access RDS for MySQL. For details, see Procedure.

Table 2 Procedure for accessing RDS for MySQL from a function

Step

Description

Prerequisites

Before starting this practice, ensure you have a VPC network environment, an RDS for MySQL instance with its database and tables, and a function agency with VPC Administrator permission already created.

Step 1: Creating Function Dependencies

In this practice, Python sample code is used to implement database connection and access. The code depends on the pymysql and DBUtils packages, which must be uploaded to the FunctionGraph console for subsequent invocations.

Step 2: Creating a Function

On the FunctionGraph console, create a function for accessing RDS for MySQL.

Step 3: Configuring the Function

On the function details page, configure the function code, dependencies, and function settings.

Step 4: Testing the Function

Test whether the function can access records in the database table of the RDS for MySQL instance.