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. You can persist structured data in a database to share instance status. This topic uses Python 3 as an example to describe how to use Serverless Devs to deploy a function to access an ApsaraDB for MongoDB database.


  • An ApsaraDB for MongoDB instance is created.
    • Make sure that the database instance you created 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 supports Function Compute.

      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 the VPC that reside in other zones. For more information, see How can I resolve the "vSwitch is in unsupported zone" error?.

      Region Region ID Zones supported by Function Compute
      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 a database that is named test-db and a database collection that is named fc_col.

    In this example, s init inserts a document data record into the fc-col collection of a database named test-db, where test-db is the value of the MONGO_DATABASE environment variable.

  • 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 the whitelist for the database

In ApsaraDB for MongoDB, you can add IP addresses to the whitelist or specify security groups to access the database. We recommend that you add IP addresses to the whitelist to access the database.

Note A sharded cluster instance is used as an example. For more information about how to add an IP address to the whitelist, see Configure a whitelist or an ECS security group for an ApsaraDB for MongoDB instance.
  1. Log on to the ApsaraDB for MongoDB console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Sharded Cluster Instances.
  4. On the Sharded Cluster Instances page, click the name of the cluster that you want to manage.
  5. In the left-side navigation pane, choose Data Security > Whitelist Settings.
  6. On the Whitelist Settings page, find the whitelist group name, click the More icon in the Actions column, and select Manually Modify from the drop-down list.
  7. On the Manually Modify page, enter an IP address and click OK.

Deploy and debug functions

  1. Initialize a project.
    s init start-fc-mongodb-python -d start-fc-mongodb-python
  2. Access the project.
    cd start-fc-mongodb-python
  3. Edit the s.yaml file.


    edition: 1.0.0
    name: fcDeployApp
    access: "default"
        component: devsapp/fc
          region: cn-hangzhou
            name: fc-db-demo
            description: 'demo for fc visit db'
            internetAccess: true
              vpcId: vpc-bp1hcg467ekqsv0zr**** # The ID of the VPC where the database instance resides. 
              securityGroupId: sg-bp1j6knvj40yjn0g**** # The ID of the security group. 
                - vsw-bp1lgecrqfslgji9b**** # Make sure that the CIDR block of the vSwitch is added to the whitelist of the database instance. 
            name: mongodb
            description: visit mongodb
            runtime: python3
            codeUri: ./code
            handler: index.handler
            memorySize: 256
            timeout: 60
              MONGO_DATABASE: test-db # The name of the database. 
              MONGO_URL: mongodb://m****:Hang****@s-bp1a2d6c391e****** # The endpoint of the database. 
  4. Run the following command to build the project.
    s build --use-docker
  5. Run the following command to deploy the project.
    s deploy -y
  6. Run the following command to invoke a function.
    s invoke -e "{}"

    The expected command output is as follows:

    [2021-09-22T22:25:58.139] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: b361814a-0a70-4511-8212-6739389d3ca4
    find documents:{'_id': ObjectId('614b3ccea90d5fbfc262e918'), 'DEMO': 'FC', 'MSG': 'Hello FunctionCompute For MongoDB'}
    FC Invoke End RequestId: b361814a-0a70-4511-8212-6739389d3ca4
    Duration: 61.27 ms, Billed Duration: 62 ms, Memory Size: 256 MB, Max Memory Used: 51.21 MB
    ========= FC invoke Logs end =========
    FC Invoke Result:
    {'_id': ObjectId('614b3ccea90d5fbfc262e918'), 'DEMO': 'FC', 'MSG': 'Hello FunctionCompute For MongoDB'}
    End of method: invoke

What to do next

Collection of cases to access the database instance by using Function Compute