All Products
Search
Document Center

Function Compute:Access a database

Last Updated:Jun 18, 2024

Generally, states are not shared among instances that run in different execution environments in Function Compute. You can persist structured data in databases to achieve state sharing. After you implement access to a cloud-based database from Function Compute. You can perform operations such as data query and data insertion in a function of Function Compute. This topic describes how Function Compute accesses databases and provides links to examples.

Access mechanism

You can access a database over the Internet or a virtual private cloud (VPC). We recommend that you access database resources over a VPC so that data is transmitted in Alibaba Cloud VPC. This enhances data security.

  • Access a database over the Internet

    You can choose to access database data over the Internet in specific scenarios, such as development and debugging scenarios. In this case, your function can access database data by using a static public IP address. For more information, see Configure static public IP addresses. You must add the static public IP address to the whitelist of the database to access.

  • Access a database over a VPC

    In a production environment, we recommend that you place your database in a secure VPC and access the database over the VPC. Specifically, configure your function settings to allow your function to access resources in the VPC. Then, add the CIDR block of the configured vSwitch to the whitelist of the database.

The following figure shows how Function Compute accesses a database over a VPC.

image
  1. The client initiates a request to Function Compute.

  2. Function Compute accesses the database in the specified VPC based on the configured network settings.

    For information about how to configure network settings in the Function Compute console, see Configure network settings. You can also use a YAML file of Serverless Devs to configure network settings. For more information, see Common commands of Serverless Devs.

  3. Function Compute obtains data and returns the data to the client.

Examples