All Products
Search
Document Center

:Access an ApsaraDB for Lindorm database

Last Updated:Oct 17, 2024

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 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

    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 about the message routing feature, see Zones where Function Compute is supported.

      If your database instance is not deployed in a zone that is supported by Function Compute, create a vSwitch in your VPC. The vSwitch must be in the same zone as Function Compute. In addition, you must specify the vSwitch ID in the VPC configuration of the specified service in 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?.

  • 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

We recommend that you use an IP address whitelist, instead of a security group, for secure access. Otherwise, the function connection may be unstable, which affects your business.

  1. Log on to the Lindorm console.

  2. In the upper-left corner of the page, select the region where the instance is deployed.

  3. On the Instances page, click the ID of the instance that you want to manage or click Manage in the Actions column corresponding to the instance.

  4. In the left-side navigation pane, click Access Control.

  5. On the page that appears, click Create Whitelist.

  6. On the Create Whitelist dialog box, configure Whitelist Name and Whitelist.

    Important
    • A whitelist name can contain only letters, digits, and underscores (_).

    • You can specify IP addresses or Classless Inter-Domain Routing (CIDR) blocks in the following formats:

      • A single IP address in the 192.0.XX.XX format.

      • A CIDR block that specifies IP address ranges. For more information about CIDR, see What is CIDR? For example, you can add 192.0.XX.XX/24 to the whitelist. The suffix /24 indicates that the network prefix of the CIDR block is 24 bits in length. The value of the suffix ranges from 1 to 32.

    • Separate multiple IP addresses or CIDR blocks with commas (,).

    • If you add 127.0.0.1 to the whitelist, all IP addresses are prohibited from accessing the Lindorm instance.

  7. Click OK.

    After a whitelist is configured, you can click Modify Group in the Actions column corresponding to the whitelist to modify the IP addresses or CIDR blocks in the whitelist. You can create multiple whitelists to manage access from different IP addresses and CIDR blocks.

Important 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 the 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.

    The following sample code provides a configuration 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 VPC where 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

References

For more information, see Cases to access the database instance by using Function Compute.