#! /bin/bashset -euo pipefail# 初始化 - 加载函数文件source ./"$(echo $_HANDLER | cut -d. -f1).sh"# 初始化完成,访问运行时API上报就绪状态curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"### 循环监听处理事件调用while truedoHEADERS="$(mktemp)"# 长轮询获取事件EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")# 调用函数处理事件RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")# 推送函数处理结果curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"done
# 初始化完成,访问运行时API上报就绪状态curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"
SCF_RUNTIME_API
:运行时 API 地址。SCF_RUNTIME_API_PORT
:运行时 API 端口。# 长轮询获取事件EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")
# 调用函数处理事件RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# 推送函数处理结果curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"
# 推送函数处理错误curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/error" -d "parse event error"
function main_handler () {EVENT_DATA=$1echo "$EVENT_DATA" 1>&2;RESPONSE="Echoing request: '$EVENT_DATA'"echo $RESPONSE}
├ bootstrap└ index.sh
chmod 755
命令,需要在 Linux 或 Mac OS 系统下执行。$ chmod 755 index.sh bootstrap
$ zip demo.zip index.sh bootstrapadding: index.sh (deflated 23%)adding: bootstrap (deflated 46%)
#组件信息component: scf # 组件名称,本例中为scf组件name: ap-guangzhou_default_helloworld # 实例名称#组件参数inputs:name: helloworld #函数名称src: ./description: helloworld blank template function.handler: index.main_handlerruntime: CustomRuntimenamespace: defaultregion: ap-guangzhoumemorySize: 128timeout: 3events:- apigw:parameters:endpoints:- path: /method: GET
scf deploy
命令创建云函数,创建成功则返回结果如下:serverless-cloud-frameworkAction: "deploy" - Stage: "dev" - App: "ap-guangzhou_default_helloworld" - Instance: "ap-guangzhou_default_helloworld"functionName: helloworlddescription: helloworld blank template function.namespace: defaultruntime: CustomRuntimehandler: index.main_handlermemorySize: 128lastVersion: $LATESTtraffic: 1triggers:apigw:- http://service-xxxxxx-123456789.gz.apigw.tencentcs.com/release/Full details: https://serverless.cloud.tencent.com/apps/ap-guangzhou_default_helloworld/ap-guangzhou_default_helloworld/dev36s › ap-guangzhou_default_helloworld › Success
events
为 apigw
的配置,因此创建函数的同时也创建了 api 网关,可通过 api 网关访问云函数。返回类似如下信息,即表示访问成功。Echoing request:'{"headerParameters":{},"headers":{"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","accept-encoding":"gzip, deflate","accept-language":"zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6","cache-control":"max-age=259200","connection":"keep-alive","host":"service-eiu4aljg-1259787414.gz.apigw.tencentcs.com","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36","x-anonymous-consumer":"true","x-api-requestid":"b8b69e08336bb7f3e06276c8c9******","x-api-scheme":"http","x-b3-traceid":"b8b69e08336bb7f3e06276c8c9******","x-qualifier":"$LATEST"},"httpMethod":"GET","path":"/","pathParameters":{},"queryString":{},"queryStringParameters":{},"requestContext":{"httpMethod":"GET","identity":{},"path":"/","serviceId":"service-xxxxx","sourceIp":"10.10.10.1","stage":"release"}}'
from tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.scf.v20180416 import scf_client, modelsfrom base64 import b64encodetry:cred = credential.Credential("SecretId", "secretKey")httpProfile = HttpProfile()httpProfile.endpoint = "scf.tencentcloudapi.com"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = scf_client.ScfClient(cred, "na-toronto", clientProfile)req = models.CreateFunctionRequest()f = open('demo.zip', 'r')code = f.read()f.close()params = '{\\"FunctionName\\":\\"CustomRuntime-Bash\\",\\"Code\\":{\\"ZipFile\\":\\"'+b64encode(code)+'\\"},\\"Timeout\\":3,\\"Runtime\\":\\"CustomRuntime\\",\\"InitTimeout\\":3}'req.from_json_string(params)resp = client.CreateFunction(req)print(resp.to_json_string())except TencentCloudSDKException as err:print(err)
参数类型 | 说明 |
"Runtime":"CustomRuntime" | Custom Runtime 对应的 runtime 类型。 |
"InitTimeout":3 | 初始化超时时间。Custom Runtime 针对初始化阶段新增超时控制配置,时间区间以 bootstrap 启动为始,以上报运行时 API 就绪状态为止。超出后将终止执行并返回初始化超时错误。 |
"Timeout":3 | 调用超时时间。事件调用的超时控制配置,时间区间以事件下发为始,以函数处理完成推送结果至运行时 API 为止。超出后将终止执行并返回调用超时错误。 |
from tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.scf.v20180416 import scf_client, modelstry:cred = credential.Credential("SecretId", "secretKey")httpProfile = HttpProfile()httpProfile.endpoint = "scf.tencentcloudapi.com"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = scf_client.ScfClient(cred, "na-toronto", clientProfile)req = models.InvokeRequest()params = '{\\"FunctionName\\":\\"CustomRuntime-Bash\\",\\"ClientContext\\":\\"{ \\\\\\"key1\\\\\\": \\\\\\"test value 1\\\\\\", \\\\\\"key2\\\\\\": \\\\\\"test value 2\\\\\\" }\\"}'req.from_json_string(params)resp = client.Invoke(req)print(resp.to_json_string())except TencentCloudSDKException as err:print(err)
{"Result":{"MemUsage": 7417***,"Log": "", "RetMsg":"Echoing request: '{\\"key1\\": \\"test value 1\\",\\"key2\\": \\"test value 2\\"}'","BillDuration": 101,"FunctionRequestId": "3c32a636-****-****-****-d43214e161de","Duration": 101,"ErrMsg": "","InvokeResult": 0},"RequestId": "3c32a636-****-****-****-d43214e161de"}
本页内容是否解决了您的问题?