Before you start
Create an ApsaraDB for MongoDB database instance.
Important
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. For more information, see Zones where Function Compute is available.
If your database instance is not in a zone that is supported by Function Compute, you can create a vSwitch in your VPC in the same zone as Function Compute and use this vSwitch ID in the VPC configurations of the function. 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 What do I do if the vSwitch is in unsupported zone error is reported?
Create a database named test-db and a database collection named fc_col. For more information, see Create a database and a collection and write data.
In this topic, the code logic in the sample code of index.py is to insert a piece of document data into the fc-col collection of the test-db database.
Procedure
Install Serverless Devs and Docker and configure the AccessKey information.
Run the following command to initialize a project.
In the CLI, specify Alibaba Cloud as the vendor, specify the quick start mode, and select a built-in Python runtime. Configure the project name and the region where the project is deployed. In this example, the start-fc-mongodb-python project is deployed in the China (Hangzhou) region.
Run the following command to go to the project directory.
cd start-fc-mongodb-python
Modify the directory file based on your own business requirements.
Run the following command to build a project.
sudo s build --use-docker
Run the following command to deploy the project.
Run the following command to invoke the function.
The following code snippet shows the expected output. The returned result indicates that a data record has been successfully inserted into the ApsaraDB for MongoDB database.
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 1-65d30578-1586adb3-4d0f099c1ada
find documents:{'_id': ObjectId('65d2faef467c916ed6401a73'), 'DEMO': 'FC', 'MSG': 'Hello FunctionCompute For MongoDB'}
FC Invoke End RequestId: 1-65d30578-1586adb3-4d0f099c1ada
Duration: 161.75 ms, Billed Duration: 162 ms, Memory Size: 256 MB, Max Memory Used: 34.44 MB
========= FC invoke Logs end =========
FC Invoke instanceId: c-65d304d7-15f440b6-20443f00e921
FC Invoke Result:
{'_id': ObjectId('65d2faef467c916ed6401a73'), 'DEMO': 'FC', 'MSG': 'Hello FunctionCompute For MongoDB'}
End of method: invoke
Configure an IP address whitelist for the database
Important
Use an IP address whitelist to authorize functions to access the database. Do not use the security group mode. Otherwise, functions may occasionally fail to connect to the database, which affects the businesses.
A sharded cluster instance is used as an example.
Log on to the ApsaraDB for MongoDB console.
In the top navigation bar, select a region.
In the left-side navigation pane, click Sharded Cluster Instances.
On the Sharded Cluster Instances page, click the name of the desired cluster.
In the left-side navigation pane, choose .
On the Whitelist Settings page, find the desired whitelist group, click the
icon in the Actions column. In the drop-down menu, choose Manually Modify.
In the Manually Modify panel, enter an IP address and click OK.
Important
You must specify an IP address that belongs to the CIDR block of the vSwitch that you add when you configure the network for Function Compute.