region
and secretId
).testNodejsSDK
in the /Users/xxx/Desktop/testNodejsSDK
path.testNodejsSDK
directory and run the following commands in sequence to install tencentcloud-serverless-nodejs SDK.npm init -ynpm install tencentcloud-serverless-nodejs
node_modules
, package.json
, and package-lock.json
in the testNodejsSDK
directory.{"dependencies": {"tencentcloud-serverless-nodejs":"*"}}
default
will be used by default.secretId
and secretKey
are not manually passed in, the function needs to be bound to a role with SCF Invoke
permissions (or containing SCF Invoke
, such as SCF FullAccess
). For more information, please see Roles and Policies.'use strict';exports.main_handler = async (event, context, callback) => {console.log("\\n Hello World from the function being invoked\\n")console.log(event)console.log(event["non-exist"])return event};
index.js
file in the testNodejsSDK
directory and enter the following sample code to create an invoking Node.js function.const { SDK, LogType } = require('tencentcloud-serverless-nodejs')exports.main_handler = async (event, context) => {context.callbackWaitsForEmptyEventLoop = falseconst sdk = new SDK({region:'ap-beijing'}) // If you bind and run in SCF an execution role with SCF invocation permissions, the authentication information in the environment variable will be used by defaultconst res = await sdk.invoke({functionName: 'FuncInvoked',logType: LogType.Tail,data: {name: 'test',role: 'test_role'}})console.log(res)// return res}
FuncInvoked
function created in step 1 is used as an example in this document.$LATEST
will be used by default. For more information, please see Viewing a Version.default
will be used by default.event
input parameter.testNodejsSDK
directory to ZIP format and upload them to the cloud."Already invoked a function!"
'use strict';exports.main_handler = async (event, context, callback) => {console.log("\\n Hello World from the function being invoked\\n")console.log(event)console.log(event["non-exist"])return event};
index.js
file in the testNodejsSDK
directory as an invoking Node.js function and enter the following sample code:const { SDK, LogType } = require('tencentcloud-serverless-nodejs')exports.main_handler = async (event, context) => {context.callbackWaitsForEmptyEventLoop = falseconst sdk = new SDK({region:'ap-beijing',secretId: 'AKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxj',secretKey: 'WtxxxxxxxxxxxxxxxxxxxxxxxxxxxxqL'}) // If you bind and run in SCF an execution role with SCF invocation permissions, the authentication information in the environment variable will be used by defaultconst res = await sdk.invoke({functionName: 'FuncInvoked',logType: LogType.Tail,data: {name: 'test',role: 'test_role'}})console.log(res)// return res}
index.js
file is located and run the following command to view the result.export NODE_ENV=development && node index.js
set NODE_ENV=development && node index.js
prepare to invoke a function!{"key":"value"}Already invoked a function!
npm init
command to initialize the SDK before using it.region
, secretId
, and secretKey
parameters can be passed in using the initialization command.Parameter Name | Required | Type | Description |
region | No | String | Region |
secretId | No | String | process.env.TENCENTCLOUD_SECRETID is used by default |
secretKey | No | String | process.env.TENCENTCLOUD_SECRETKEY is used by default |
token | No | String | process.env.TENCENTCLOUD_SESSIONTOKEN is used by default |
Parameter Name | Required | Type | Description |
functionName | Yes | String | Function name |
qualifier | No | String | Function version. Default value: $LATEST |
data | No | String | Input parameter for function execution |
namespace | No | String | Namespace, which is default by default. |
region | No | String | Region |
secretId | No | String | process.env.TENCENTCLOUD_SECRETID is used by default |
secretKey | No | String | process.env.TENCENTCLOUD_SECRETKEY is used by default |
token | No | String | process.env.TENCENTCLOUD_SESSIONTOKEN is used by default |
Was this page helpful?