In Function Compute, you can use a function to call the ApsaraDB RDS for SQL Server API to perform operations such as insert and query on an ApsaraDB RDS for SQL Server 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 an ApsaraDB RDS for SQL Server database.
Write a function by using Python 3
The following section describes how to use FunCraft to write a function in Python 3 to access an ApsaraDB RDS for SQL Server database.
- 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 SQL-Server service
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: vpc-xxx
VSwitchIds:
- vsw-xxx
SecurityGroupId: sg-xxx
InternetAccess: true
function:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: 'index.handler'
Runtime: python3
Timeout: 10
MemorySize: 128
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 pymssql
- 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:
# -*- coding: utf-8 -*-
import pymssql
def handler(event, context):
conn = pymssql.connect(host='rm-xxx.sqlserver.rds.aliyuncs.com',
user='xxx',
password='x'x'x'x',
database='xxx',
charset='utf8')
cursor = conn.cursor()
cursor.execute('SELECT * FROM inventory WHERE quantity > 152')
result = ''
for row in cursor:
result += 'row = %r\n' % (row,)
conn.close()
return result
- 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 Function Compute console and click Service/Function. On the page that appears, you can see the newly deployed service and function.
Write a function by using PHP 7.2
The following section describes how to use FunCraft to write a function in PHP 7.2 to access an ApsaraDB RDS for SQL Server database.
- 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 SQL-Server service
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: vpc-xxx
VSwitchIds:
- vsw-x'x'x'x
SecurityGroupId: sg-xxx
InternetAccess: true
function:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: 'index.handler'
Runtime: php7.2
Timeout: 10
MemorySize: 128
CodeUri: '. /'
EnvironmentVariables:
ODBCINI: /code/.fun/root/etc/odbc.ini
ODBCSYSINI: /code/.fun/root/opt/microsoft/msodbcsql17/etc
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 php7.2
RUN apt-get update && apt-get install -y apt-transport-https apt-utils
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN fun-install apt-get install unixodbc-dev
RUN fun-install apt-get install msodbcsql17
- Run the fun install command to install dependencies.
When the code editor shows the following content, the dependencies have been installed.
Install Success
- Modify the odbc.ini file in the root directory, for example, /tmp/code/odbc.ini. Modify the value of the Driver parameter in the file to /code/.fun/root/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.1.1.
- Create a file in a directory at the same level as that of the template.yml file, for example, /tmp/code/index.php. The following sample code is provided:
<? php
function handler($event, $context)
{
try {
$conn = new PDO("sqlsrv:Server=rm-xxx.sqlserver.rds.aliyuncs.com;Database=xxx","xxx","xxx");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->query("set names utf-8");
$sql="SELECT * FROM inventory WHERE quantity > 152";
$result = $conn->prepare($sql);
$result->execute();
print($result);
return ("Connection successed.");
} catch (PDOException $e) {
return ("Connection failed: " . $e->getMessage());
}
}
- 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 Function Compute console and click Service/Function. On the page that appears, you can see the newly deployed service and function.
Debug the 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 your region.
- In the left-side navigation pane, click Service/Function.
- Find the target function in the target service and click the name of the function.
- On the page that appears, click the Code tab. On this tab, click Invoke.
After the execution is complete, you can view the execution results and logs.