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 for Lindorm database by using Serverless Devs. Python 3 is used in this topic

Prerequisites

  • Create a Lindorm instance
    Notice
    • 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
  • 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.

  1. In the left-side navigation pane, click Access Control.
  2. Click Modify Whitelist.
  3. In the Modify Whitelist dialog box, enter the CIDR block that is allowed to access the database and click OK.
    Notice The IP address that you specify must belong to the CIDR block of the vSwitch that you want to add when you configure the network for Function Compute.

Deploy and invoke a function

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

    Example:

    edition: 1.0.0          
    name: fcDeployApp       
    access: "default"  
    
    services:
      fc-db-lindorm-python: 
        component: devsapp/fc  
        props:
          region: cn-hangzhou
          service:
            name: fc-db-demo
            description: 'demo for fc visit db'
            internetAccess: true
            vpcConfig:
              vpcId: vpc-bp1hcg467ekqsv0zr**** # The ID of the virtual private cloud (VPC) in which the database instance resides. 
              securityGroupId: sg-bp1gg2tyyzuvz8x0**** # The ID of the security group. 
              vswitchIds:
                - vsw-bp1sciqyju9qxocys**** # Make sure that the CIDR block of the vSwitch is added to the whitelist of the database instance. 
          function:
            name: lindorm
            description: visit lindorm
            runtime: python3
            codeUri: ./code
            handler: index.handler
            memorySize: 256
            timeout: 30
            environmentVariables:
              DATABASE_ADDR: ld-bp13c5s05n1mu****-proxy-lindorm.lindorm.rds.aliyuncs.com:30** # The endpoint of the database. 
              USER: r*** # The username of the database. 
              PASSWORD: r*** # The password of the database user. 
  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 "{}"

    Expected command output:

    [2021-09-22T22:54:19.851] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: a41a7647-cf8b-466d-a63f-59635043453e
    2021-09-22T14:54:20.289Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] start to test Lindorm 
    2021-09-22T14:54:20.291Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['ld-bp13c5s05n1mu****-proxy-lindorm.lindorm.rds.aliyuncs.com'], lbp = None)
    2021-09-22T14:54:20.301Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 66 to 65 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.309Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 65 to 5 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.317Z a41a7647-cf8b-466d-a63f-59635043453e [ERROR] Closing connection <LibevConnection(13962401490****) 192.168.**.**:90**> due to protocol error: Error from server: code=000a [Protocol error] message="Beta version of the protocol used (5/v5-beta), but USE_BETA flag is unset"
    2021-09-22T14:54:20.318Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 5 to 4 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.364Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] Using datacenter 'datacenter1' for DCAwareRoundRobinPolicy (via host '192.168.**.**:90**'); if incorrect, please specify a local_dc to the constructor, or limit contact points to local cluster nodes
    2021-09-22T14:54:20.550Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] # row: Row(id=1, address='hangzhou', age=11, name='testname')
    FC Invoke End RequestId: a41a7647-cf8b-466d-a63f-59635043453e
    
    Duration: 267.05 ms, Billed Duration: 268 ms, Memory Size: 256 MB, Max Memory Used: 87.87 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    null
    
    
    End of method: invoke