+ All Categories
Home > Documents > FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token...

FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token...

Date post: 09-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
20
FunctionGraph FAQs Issue 01 Date 2020-05-25 HUAWEI TECHNOLOGIES CO., LTD.
Transcript
Page 1: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

FunctionGraph

FAQs

Issue 01

Date 2020-05-25

HUAWEI TECHNOLOGIES CO., LTD.

Page 2: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

Copyright © Huawei Technologies Co., Ltd. 2020. All rights reserved.

No part of this document may be reproduced or transmitted in any form or by any means without priorwritten consent of Huawei Technologies Co., Ltd. Trademarks and Permissions

and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.All other trademarks and trade names mentioned in this document are the property of their respectiveholders. NoticeThe purchased products, services and features are stipulated by the contract made between Huawei andthe customer. All or part of the products, services and features described in this document may not bewithin the purchase scope or the usage scope. Unless otherwise specified in the contract, all statements,information, and recommendations in this document are provided "AS IS" without warranties, guaranteesor representations of any kind, either express or implied.

The information in this document is subject to change without notice. Every effort has been made in thepreparation of this document to ensure accuracy of the contents, but all statements, information, andrecommendations in this document do not constitute a warranty of any kind, express or implied.

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. i

Page 3: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

Contents

1 General FAQs............................................................................................................................ 11.1 What Is FunctionGraph?....................................................................................................................................................... 11.2 Do I Need to Apply for Any Compute, Storage, or Network Services When Using FunctionGraph?.........11.3 Do I Need to Deploy My Code After Programming?.................................................................................................. 11.4 How Do I Obtain a Token?.................................................................................................................................................. 21.5 What Runtimes Does FunctionGraph Support?............................................................................................................ 31.6 What Is the Disk Space That Can Be Allocated to a FunctionGraph Function?................................................ 31.7 Does FunctionGraph Support Function Versioning?................................................................................................... 31.8 How Does a Function Read Files?..................................................................................................................................... 31.9 How Do I Set a Proxy When Using CLI?.......................................................................................................................... 41.10 Does FunctionGraph Support Function Expansion?..................................................................................................41.11 What Permissions Are Required for an IAM User to Use FunctionGraph?....................................................... 41.12 How Do I Generate Function Dependencies?............................................................................................................. 41.13 What Is the Quota of FunctionGraph?.......................................................................................................................... 61.14 What Chinese Fonts Does FunctionGraph Support?.................................................................................................6

2 Function Creation FAQs......................................................................................................... 72.1 Does FunctionGraph Support Class Libraries?...............................................................................................................72.2 Does FunctionGraph Support Threads and Processes?.............................................................................................. 72.3 What Are the Rules (Restrictions) for Packaging a Function Project?..................................................................72.4 How Does FunctionGraph Isolate Code?........................................................................................................................ 7

3 Trigger Management FAQs................................................................................................... 83.1 What Events Can Trigger a FunctionGraph Function?............................................................................................... 83.2 What Can I Do When a Function with APIG Triggers Returns Error Code 500?............................................... 83.3 What Are LATEST and TRIM_HORIZON in DIS Trigger Configuration?............................................................... 93.4 What Are the Precautions for Editing Code of an APIG Trigger Online?.............................................................9

4 Function Execution FAQs..................................................................................................... 114.1 How Long Does It Take to Execute a FunctionGraph Function?.......................................................................... 114.2 What Steps Are Included in Function Execution?...................................................................................................... 114.3 What Is the Concurrent Processing of FunctionGraph Functions?...................................................................... 114.4 How Does FunctionGraph Process Instances That Have Not Been Executed for a Long Time?...............114.5 What Can I Do to Speed Up Initial Access to a Function?..................................................................................... 124.6 How Do I Know the Actual Memory Used for Function Execution?................................................................... 12

FunctionGraphFAQs Contents

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. ii

Page 4: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

4.7 Why Is the System Memory Queried by Running the free -m Command Is Different from theAllocated Memory?..................................................................................................................................................................... 12

5 Function Configuration FAQs............................................................................................. 135.1 Does FunctionGraph Support Environment Variables?............................................................................................135.2 Are There Any Security Risks in Storing Sensitive Information in Environment Variables?........................13

6 Interconnection with Other Cloud Services.................................................................... 146.1 How Do I Connect a Function to RDS?......................................................................................................................... 146.2 How Do I Connect a Function to DCS?......................................................................................................................... 15

FunctionGraphFAQs Contents

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. iii

Page 5: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

1 General FAQs

1.1 What Is FunctionGraph?FunctionGraph hosts and computes event-driven functions in a serverless contextwhile ensuring high availability, high scalability, and zero maintenance. All youneed to do is write your code and set conditions. You pay only for what you useand you are not charged when your code is not running.

1.2 Do I Need to Apply for Any Compute, Storage, orNetwork Services When Using FunctionGraph?

No. When using FunctionGraph, you do not need to apply for or pre-configure anycomputing, storage, or network services, but only need to upload and run code insupported runtimes. FunctionGraph provides and manages underlying computeresources, including server CPUs, memory, and networks. It performs configurationand resource maintenance, code deployment, automatic scaling, load balancing,secure upgrade, and resource monitoring.

1.3 Do I Need to Deploy My Code After Programming?No. After local programming, package your code into a ZIP file (Java, Node.js,Python, and Go) or JAR file (Java), and upload the file to FunctionGraph forexecution.

When creating a ZIP file, place the handler file under the root directory to ensurethat your code can be run normally after decompressing.

If you edit code in Go, zip the compiled file, and ensure that the name of thedynamic library file is consistent with the plugin name of the handler. Forexample, if the name of the dynamic library file is testplugin.so, set the handlerto testplugin.Handler.

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 1

Page 6: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

1.4 How Do I Obtain a Token?API call requests can be authenticated using a token, which is obtained throughthe standard API of Identity and Access Management (IAM).

● Run the following command to obtain a token in the CN South-Guangzhouregion:curl -k -i -X POST https://iam.cn-south-1.myhuaweicloud.com/v3/auth/tokens -H 'Content-Type: application/json' -d '{ "auth": { "identity": { "methods": [ "password" ], "password": { "user": {"name": "HUAWEI CLOUD account","password": "Login password", "domain": {"name": "HUAWEI CLOUD account" } } } }, "scope": { "project": { "name": "cn-south-1" } } }}'

● Run the following command to obtain a token in the CN North-Beijing1region:curl -k -i -X POST https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens -H 'Content-Type: application/json' -d '{ "auth": { "identity": { "methods": [ "password" ], "password": { "user": {"name": "HUAWEI CLOUD account","password": "Login password", "domain": {"name": "HUAWEI CLOUD account" } } } }, "scope": { "project": { "name": "cn-north-1" } } }}'

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 2

Page 7: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

● The value of X-Subject-Token in the response header is the obtained token.

● A token obtained in the CN North-Beijing1 region is used to call only theFunctionGraph API in the same region.

● A token obtained in the CN South-Guangzhou region is used to call only theFunctionGraph API in the same region.

1.5 What Runtimes Does FunctionGraph Support?FunctionGraph supports Python 2.7, Python 3.6, Node.js 6.10, Node.js 8.10, Node.js10.16, Node.js 12.13, Java 8, Go 1.8, C# (.NET Core 2.0), C# (.NET Core 2.1), C#(.NET Core 3.1), and PHP 7.3.

1.6 What Is the Disk Space That Can Be Allocated to aFunctionGraph Function?

A 512 MB ephemeral disk space can be allocated to each FunctionGraph function.The maximum size of deployment packages allowed for each tenant is 10 GB. Formore information about function resource limits, see Quotas and UsageRestrictions.

1.7 Does FunctionGraph Support Function Versioning?Yes, FunctionGraph supports function versioning. By default, each function has asingle version, that is, the LATEST version. FunctionGraph clients are allowed toinvoke a specific version or obtain the newest one. For more information, seeVersion Management.

1.8 How Does a Function Read Files?

Background

A function can read files in the code directory. The working directory of a functionis the upper-level directory of the handler file. To read the test.conf file in thesame directory level as the handler file, use the relative path code/test.conf or anabsolute path (value of the RUNTIME_CODE_ROOT environment variable). Towrite a file (for example, to create or download a file), go to the /tmp directory oruse the file system mounting function provided by FunctionGraph.

Typical Scenarios● To process files stored in Object Storage Service (OBS), download the files to

the /tmp directory.

● To store function execution data in OBS, create a file in /tmp, write the datainto the file, and then upload the file to OBS.

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 3

Page 8: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

1.9 How Do I Set a Proxy When Using CLI?

DescriptionWhen using CLI to upload a code package (.zip), how do I set a proxy server andidentity information to complete authentication through the proxy gateway on theinternal network?

SolutionRun the following command to set a proxy:

export HTTP_PROXY="http://user:password@proxyIp:proxyPort"

For more information, see https://www.cyberciti.biz/faq/unix-linux-export-variable-http_proxy-with-special-characters/.

1.10 Does FunctionGraph Support Function Expansion?FunctionGraph has integrated with some non-standard dependencies, such asredis, http, and obs_client. You can use these libraries when developing functions.For details, see Developer Guide.

Alternatively, you may use private dependencies in your functions. For details, seeDependency Management.

1.11 What Permissions Are Required for an IAM User toUse FunctionGraph?

If an IAM user is prompted insufficient permissions when creating, deleting,modifying, or querying functions and triggers in FunctionGraph, contact theadministrator to grant the required permissions, such as Tenant Administratorpermissions for creating OBS buckets and triggers. Permissions assignment shouldfollow the principle of least privilege for security purposes. For more information,see Permissions Description.

1.12 How Do I Generate Function Dependencies?You are advised to generate function dependencies in CentOS 7 or EulerOS.

Generating a Dependency for a Python FunctionThe Python version of the packaging environment must be the same as theruntime version of the corresponding function. For example, for Python 2.7,Python 2.7.12 or later is recommended; for Python 3.6, Python 3.6.3 or later isrecommended.

To install the PyMySQL dependency for a Python 2.7 function in the local /tmp/pymysql directory, run the following command:

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 4

Page 9: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

pip install PyMySQL --root /tmp/pymysql

After the command is successfully executed, go to the /tmp/pymysql directory:

cd /tmp/pymysql/

Go to the site-packages directory (generally, usr/lib64/python2.7/site-packages/) and then run the following command:

zip -rq pymysql.zip *

The generated package is the required dependency package.

Generating a Dependency for a Node.js Function

Ensure that the corresponding Node.js version has been installed in theenvironment.

To install the MySQL dependency for a Node.js 8.10 function, run the followingcommand:

npm install mysql --save

The node_modules folder is generated under the current directory.

● Linux OS

Run the following command to generate a ZIP package.zip -rq mysql-node8.10.zip node_modules

The required dependency is generated.

● Windows OS

Compress node_modules into a ZIP file.

To install multiple dependency packages, create a package.json file first. Forexample, enter the following content into the package.json file and then run thefollowing command:

{ "name": "test", "version": "1.0.0", "dependencies": { "redis": "~2.8.0", "mysql": "~2.17.1" }}npm install --save

Do not run the CNPM command to generate Node.js dependencies.

Compress node_modules into a ZIP package. In this way, you can generate adependency that contains both MySQL and Redis.

For other Node.js versions, you can create dependencies in the same way as statedabove.

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 5

Page 10: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

1.13 What Is the Quota of FunctionGraph?For details about the resource quota of FunctionGraph, see Quotas and UsageRestrictions. For details about how to increase the quota, see How Do I Apply fora Higher Quota?.

1.14 What Chinese Fonts Does FunctionGraph Support?FunctionGraph supports the following Chinese fonts:

● NotoSansTC-Regular.otf● NotoSerifTC-Regular.otf● NotoSansSC-Regular.otf● NotoSerifSC-Regular.otf

FunctionGraphFAQs 1 General FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 6

Page 11: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

2 Function Creation FAQs

2.1 Does FunctionGraph Support Class Libraries?Yes, FunctionGraph supports both standard libraries and non-standard third-partylibraries. It has integrated with several non-standard third-party libraries. For moreinformation, see Dependency Management.

2.2 Does FunctionGraph Support Threads andProcesses?

Yes, you can create additional threads and processes in your function by using thefeatures of a runtime and operating system (OS).

2.3 What Are the Rules (Restrictions) for Packaging aFunction Project?

In addition to inline code editing, you can create a function by directly uploading aZIP file or JAR file, or uploading a ZIP file from OBS. For more information aboutfunction project packaging rules and project package directory examples, seeFunction Project Packaging Rules.

2.4 How Does FunctionGraph Isolate Code?Each FunctionGraph function runs in an environment with a file system view andseparate resources. FunctionGraph isolates code at the same technical securitylevel as Elastic Cloud Server (ECS).

FunctionGraphFAQs 2 Function Creation FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 7

Page 12: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

3 Trigger Management FAQs

3.1 What Events Can Trigger a FunctionGraphFunction?

For details, see Supported Event Sources.

3.2 What Can I Do When a Function with APIG TriggersReturns Error Code 500?

Check whether the function response for an invocation by API Gateway has beenencapsulated and whether it contains body(String), statusCode(int),headers(Map), and isBase64Encoded(boolean).

The following is an example response returned by a Node.js function that uses anAPIG trigger:

exports.handler = function (event, context, callback) { const response = { 'statusCode': 200, 'isBase64Encoded': false, 'headers': { "Content-type": "application/json" }, 'body': 'Hello, FunctionGraph with APIG', } callback(null, response);}

The following is an example response returned by a Java function that uses anAPIG trigger:

import java.util.Map;

public HttpTriggerResponse index(String event, Context context){ String body = "<html><title>FunctionStage</title>" + "<h1>This is a simple APIG trigger test</h1><br>" + "<h2>This is a simple APIG trigger test</h2><br>" + "<h3>This is a simple APIG trigger test</h3>"

FunctionGraphFAQs 3 Trigger Management FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 8

Page 13: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

+ "</html>"; int code = 200; boolean isBase64 = false; Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "text/html; charset=utf-8"); return new HttpTriggerResponse(body, headers, code, isBase64); }

class HttpTriggerResponse { private String body; private Map<String, String> headers; private int statusCode; private boolean isBase64Encoded; public HttpTriggerResponse(String body, Map<String,String> headers, int statusCode, boolean isBase64Encoded){ this.body = body; this.headers = headers; this.statusCode = statusCode; this.isBase64Encoded = isBase64Encoded; }}

3.3 What Are LATEST and TRIM_HORIZON in DISTrigger Configuration?

LATEST and TRIM_HORIZON are two slider positions in DIS streams from which tostart reading data.

● TRIM_HORIZON: Data is read from the earliest valid record stored in thepartition.For example, a tenant used a DIS stream to upload three pieces of data A1,A2, and A3. N days later, A1 has expired and A2 and A3 are still in the validityperiod. If the tenant specifies TRIM_HORIZON for downloading data, only A2and A3 are downloadable.

● LATEST: Data is read from the latest record in the partition. This settingensures that the most recent data in the partition can be read.For details, see Obtaining a Cursor.

3.4 What Are the Precautions for Editing Code of anAPIG Trigger Online?

1. When you use PHP or Python to edit code online, the response formats are asfollows (the body needs to be modified based on actual requirements):– PHP:

function handler($event, $context) {$payload='{"statusCode":200,"body":"hello world!","headers": {"Content-Type": "text/html"},"isBase64Encoded":false}';return json_decode($payload);}

– Python:# -*- coding:utf-8 -*-import jsondef handler (event, context):

FunctionGraphFAQs 3 Trigger Management FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 9

Page 14: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

body = "<html><title>Functiongraph Demo</title><body><p>Hello, FunctionGraph!</p></body></html>"return {"statusCode":200,"body":body,"headers": {"Content-Type": "text/html",},"isBase64Encoded": False}

2. When you use Python to edit code online and need to output Chinese, addthe following code to the editor:# -*- coding:utf-8 -*-import jsondef handler (event, context):output = 'Hello message: ' + json.dumps(event,ensure_ascii=False)return output

FunctionGraphFAQs 3 Trigger Management FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 10

Page 15: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

4 Function Execution FAQs

4.1 How Long Does It Take to Execute a FunctionGraphFunction?

For synchronous function invocation, the execution duration is within 900s. Thedefault execution timeout duration is 3s. You can set the timeout duration to aninteger ranging from 3s to 900s. If the execution timeout duration of a function isset to 3s, it will be terminated after 3s.

4.2 What Steps Are Included in Function Execution?Function execution includes two steps:

1. Select an idle instance with required memory.2. Run specified code.

4.3 What Is the Concurrent Processing ofFunctionGraph Functions?

FunctionGraph automatically scales in or out function instances based on thenumber of requests. When the number of concurrent requests increases,FunctionGraph allocates more function instances to process the requests. Whenthat number decreases, FunctionGraph allocates fewer function instancesaccordingly.

4.4 How Does FunctionGraph Process Instances ThatHave Not Been Executed for a Long Time?

If a function has not been executed for a period of time, all instances related tothe function will be released.

FunctionGraphFAQs 4 Function Execution FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 11

Page 16: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

4.5 What Can I Do to Speed Up Initial Access to aFunction?

C# and Go support a lower startup speed than other languages due to mechanismissues. However, you can increase the running speed as follows:

● Increase the memory of the function.● Simplify function code, for example, delete unnecessary dependency

packages.● When using C# in non-concurrent scenarios, you can also:

Create a one-minute timer trigger to ensure that there is at least one activeinstance.

4.6 How Do I Know the Actual Memory Used forFunction Execution?

The returned information about a function contains the maximum memoryconsumed. For more information, see SDK APIs in the FunctionGraph DeveloperGuide. Alternatively, check the memory usage in the execution result, as shown inFigure 4-1.

Figure 4-1 Actual memory usage

4.7 Why Is the System Memory Queried by Runningthe free -m Command Is Different from the AllocatedMemory?

The memory of a function is the memory allocated to it rather than that queriedby running the free -m command.

FunctionGraphFAQs 4 Function Execution FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 12

Page 17: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

5 Function Configuration FAQs

5.1 Does FunctionGraph Support EnvironmentVariables?

Yes, you can set environment variables to dynamically pass settings to yourfunction code and libraries without making any changes to your code. For moreinformation, see Environment Variables.

5.2 Are There Any Security Risks in Storing SensitiveInformation in Environment Variables?

Yes. When you define environment variables, FunctionGraph displays all your inputinformation in plain text. Do not include sensitive information, such as passwords,to prevent information leakage.

FunctionGraphFAQs 5 Function Configuration FAQs

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 13

Page 18: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

6 Interconnection with Other CloudServices

6.1 How Do I Connect a Function to RDS?The following procedure uses RDS for MySQL as an example.

1. Check whether your MySQL database has been deployed in a Virtual PrivateCloud (VPC).– If yes, configure the function to access the MySQL database in the VPC.

For details, see Configuring VPC Access.– If no, build a MySQL database on the public network, and obtain the

public IP address of the MySQL database.2. Compile code for connecting a function to the MySQL database.

a. Create a function, and add the pymysql dependency to the function onthe Code tab page, as shown in Figure 6-1.For details on how to upload the pymysql dependency, see Creating aDependency.

Figure 6-1 Adding a dependency

FunctionGraphFAQs 6 Interconnection with Other Cloud Services

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 14

Page 19: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

pymysql is a public dependency provided by FunctionGraph, and therefore can beused directly.

b. Edit the following code to connect the function to the MySQL database:# -*- coding:utf-8 -*-import pymysql.cursors

def handler (event, context): # Connect to the database connection = pymysql.connect(host='host_ip', user='user', password='passwd', db='db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)

try: with connection.cursor() as cursor: # Create a new record sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" cursor.execute(sql, ('[email protected]', 'very-secret'))

# connection is not autocommit by default. So you must commit to save # your changes. connection.commit()

with connection.cursor() as cursor: # Read a single record sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s" cursor.execute(sql, ('[email protected]',)) result = cursor.fetchone() print(result) finally: connection.close() output = '^_^' return output

If the function needs to access RDS APIs, create an agency to grant the requiredpermissions to the function.

6.2 How Do I Connect a Function to DCS?The following procedure uses a Distributed Cache Service (DCS) Redis instance asan example.

1. Check whether your Redis instance has been deployed in a VPC.– If yes, configure the function to access the Redis instance in the VPC. For

details, see Configuring VPC Access.– If no, build a Redis instance on the public network, and obtain the public

IP address of the Redis instance.2. Compile code for connecting a function to the Redis instance.

redis-py is a third-party library that has already been integrated into thePython 2.7 and Python 3.6 runtimes. You do not need to download any otherRedis libraries.

FunctionGraphFAQs 6 Interconnection with Other Cloud Services

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 15

Page 20: FAQs - Huawei...The value of X-Subject-Token in the response header is the obtained token. A token obtained in the CN North-Beijing1 region is used to call only the FunctionGraph API

# -*- coding:utf-8 -*-import redisdef handler (event, context): r = redis.StrictRedis(host="host_ip",password="passwd",port=6379) print(str(r.get("hostname"))) return "^_^"

● If access to the Redis instance on the public network fails, perform the followingoperations:● Modify the settings in the redis.conf file to allow access from any IP

addresses.● Set the access password of the Redis instance in the redis.conf file.● Disable the firewall.

● If the function needs to access DCS APIs, create an agency to grant the requiredpermissions to the function.

FunctionGraphFAQs 6 Interconnection with Other Cloud Services

Issue 01 (2020-05-25) Copyright © Huawei Technologies Co., Ltd. 16


Recommended