#! /bin/bashset -euo pipefail# Initialization - Load the function file.source ./"$(echo $_HANDLER | cut -d. -f1).sh"# After the initialization is completed, access the runtime API to report the readiness.curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"### Start the loop that listens to events, handles events, and pushes event handling results.while truedoHEADERS="$(mktemp)"# Gets event data through long pollingEVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")# Invokes a function to handle the eventRESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")# Pushes the function handling resultcurl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"done
# After the initialization is completed, access the runtime API to report the readiness.curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"
SCF_RUNTIME_API
: Runtime API addressSCF_RUNTIME_API_PORT
: Runtime API port# Gets event data through long pollingEVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")
/runtime/invocation/next
) to wait for event delivery. If you access this API repeatedly within an invocation, the same event data will be returned. The response body is event_data
. The response header includes:Request_Id
: Request ID, identifying the request that triggers function invocation.Memory_Limit_In_Mb
: Function memory limit, in MB.Time_Limit_In_Ms
: Function timeout limit, in milliseconds.# Invokes a function to handle the eventRESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Pushes the function handling resultcurl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"
# Push the function handling error.curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/error" -d "parse event error"
index.sh
in the command line terminal.function main_handler () {EVENT_DATA=$1echo "$EVENT_DATA" 1>&2;RESPONSE="Echoing request: '$EVENT_DATA'"echo $RESPONSE}
├ bootstrap└ index.sh
chmod 755
command. Therefore, you need to run the command in Linux or macOS.$ chmod 755 index.sh bootstrap
$ zip demo.zip index.sh bootstrapadding: index.sh (deflated 23%)adding: bootstrap (deflated 46%)
Serverless.yml
file in the bootstrap directory to create the dotnet function.#Component informationcomponent: scf # Component name. `scf` is used as an example.name: ap-guangzhou_default_helloworld # Instance name.#Component parametersinputs:name: helloworld #Function name.src: ./description: helloworld blank template function.handler: index.main_handlerruntime: CustomRuntimenamespace: defaultregion: ap-guangzhoumemorySize: 128timeout: 3events:- apigw:parameters:endpoints:- path: /method: GET
scf deploy
command to create a cloud function. A successful creation returns the following message: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
is set to apigw
in the serverless.yml
file, an API gateway is created together with the function. The cloud function can be accessed over this API gateway. If a message similar to the following is returned, the access is successful.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"}}'
CustomRuntime-Bash
.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)
Parameter Type | Description |
"Runtime":"CustomRuntime" | Runtime type of Custom Runtime. |
"InitTimeout":3 | Initialization timeout period. Custom Runtime adds a configuration of initialization timeout period. The initialization period starts from the boot-time of bootstrap and ends when the runtime API is reported ready. When the initialization period exceeds the timeout period, the execution ends and an initialization timeout error is returned. |
"Timeout":3 | Invocation timeout period. This parameter configures the timeout period of function invocation. The invocation period starts from the event delivery time and ends upon the time when the function pushes the handling result to the runtime API. When the invocation period exceeds the timeout period, the execution ends and an invocation timeout error is returned. |
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"}
demo.zip
.
Was this page helpful?