In Function Compute, you can use a function to call the Lindorm API to perform operations
such as insert and query on a Lindorm instance. In normal cases, states cannot be
shared among different execution environments in Function Compute. You can persist
structured data in a database so that states can be shared. This topic describes how
to use Funcraft to deploy a function to access a Lindorm database.
Write a function
The following section describes how to use Funcraft to write a function that accesses
a database. The sample code is written in Python 3.
- On the local machine, create a directory to store code and dependent modules. In the
directory, create the template.yml file. In this example, the /tmp/code/template.yml file is created and contains the following content:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
service:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: This is Lindorm service
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: vpc-XXXX
VSwitchIds:
- vsw-XXX
SecurityGroupId: sg-XXXX
InternetAccess: true
function:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: 'index.handler'
Runtime: python3
Timeout: 100
MemorySize: 1024
CodeUri: '. /'
The following list describes the main parameters:
- A service named service is declared.
- Policies: grants Function Compute permission to manage elastic network interfaces (ENIs) attached
to Elastic Compute Service (ECS) instances. Function Compute can then access resources
in the VPC.
- VpcConfig: binds the VPC to the service. You must replace the following values with the information
for your VPC.
- VpcId: the ID of the VPC.
- VSwitchId: the ID of the VSwitch.
- SecurityGroupId: the ID of the security group.
- A function named function is declared.
- In the directory that contains the template.yml file, create the Funfile file. In this example, the new file contains the following sample content:
RUNTIME python3
RUN fun-install pip install cassandra-driver
- Run the fun install command to install dependencies.
When the code editor shows the following content, the dependencies have been installed.
Install Success
- In the directory that contains the template.yml file, create a Python file. In this example, the /tmp/code/index.py file is created and contains the following sample content:
Note You can log on to the
Lindorm console, find the destination instance, and obtain the endpoint, username, and password on
the
Wide Table Engine tab of the
Database Connection page.
# -*- coding: utf-8 -*-
import logging
import sys
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
logger = logging.getLogger()
def handler(event, context):
logger.info("start to test Lindorm ")
cluster = Cluster(
# Configure the endpoint.
contact_points=["ld-bp10ljb1mxae5****-proxy-lindorm.lindorm.rds.aliyuncs.com"],
# Configure the username and password.
auth_provider=PlainTextAuthProvider("XXX", "XXX"))
session = cluster.connect()
session.execute("CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};");
session.execute("CREATE TABLE IF NOT EXISTS testKeyspace.testTable (id int PRIMARY KEY, name text,age int,address text);");
session.execute("INSERT INTO testKeyspace.testTable (id, name, age, address) VALUES ( 1, 'testname', 11, 'hangzhou');");
rows = session.execute( "SELECT * FROM testKeyspace.testTable ;");
for row in rows:
logger.info("# row: {}".format(row))
session.shutdown()
cluster.shutdown()
- Run the following command to deploy the function to Function Compute by using Funcraft:
When the code editor shows the following content, the function has been deployed.
function <function-name> deploy success
service <service-name> deploy success
After the deployment is complete, log on to the and click Service/Function. On the page that appears, you can see the newly deployed service and function.
Debug a function
After the function is deployed, you can debug it in the Function Compute console.
- Log on to the Function Compute console.
- In the top navigation bar, select a region.
- In the left-side navigation pane, click Service/Function.
- Find the required function in the required service and click the name of the function.
- On the Code tab, click Invoke.
After the execution is complete, you can view the execution results and logs.
