`
sillycat
  • 浏览: 2542357 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF

 
阅读更多
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF

More configurations here
https://github.com/serverless-components/tencent-scf/blob/master/docs/configure.md

Trigger the SCF from Cloud
Follow documents here https://github.com/TencentCloud/tencentcloud-sdk-nodejs

Here is my final version of code to trigger the SCF
const tencentcloud = require('tencentcloud-sdk-nodejs');

const ScfClient = tencentcloud.scf.v20180416.Client;
const models = tencentcloud.scf.v20180416.Models;

const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;

const secretId = process.env.TENCENT_SECRET_ID || '';
const secretKey = process.env.TENCENT_SECRET_KEY || '';
const cred = new Credential(secretId, secretKey);

const httpProfile = new HttpProfile();
httpProfile.endpoint = "scf.tencentcloudapi.com";
const clientProfile = new ClientProfile();
clientProfile.httpProfile = httpProfile;
const client = new ScfClient(cred, "ap-shanghai", clientProfile);


class LambdaService {

    /**
     *
     * @param {*} functionName eg: taskrunner
     * @param {*} invocationType eg: Event
     * @param {*} data eg: id1, id2, id3
     */
    static async callRunner(functionName, invocationType, data) {
        const request = new models.InvokeRequest();
        const requstParams = '{"FunctionName":"' + functionName +
                             '","InvocationType":"' + invocationType +
                             '","ClientContext":' + JSON.stringify(JSON.stringify(data)) +
                             '}';
        console.log('sending params: ' + requstParams);
        request.from_json_string(requstParams);
        await new Promise((resolve, reject) => {
            client.Invoke(request, (err, res) => {
              return void err ? reject(err) : resolve(res)
            })
        });
    }
}

module.exports = LambdaService;


References:
https://cloud.tencent.com/document/api/583/17235
https://cloud.tencent.com/document/sdk/Node.js
https://github.com/TencentCloud/tencentcloud-sdk-nodejs/tree/master/tencentcloud/scf/v20180416
https://github.com/TencentCloud/tencentcloud-sdk-nodejs
https://cloud.tencent.com/document/api/583/17243
https://github.com/TencentCloud/tencentcloud-serverless-nodejs
https://cloud.tencent.com/document/product/583/37316

https://github.com/serverless-components/tencent-scf

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics