从另一个lambda异步调用aws lambda [英] invoke aws lambda from another lambda asynchronously

查看:242
本文介绍了从另一个lambda异步调用aws lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个lambda异步调用aws lambda.我有一个用于同步呼叫的工作代码.

I need to invoke aws lambda from another lambda asynchronously. i have a working code for synchronous calls.

exports.handler = (event, context, callback) => {
    var aws = require('aws-sdk');
    var lambda = new aws.Lambda({
        region: 'myregion' //change to your region
    });
    console.log("lambda invoke started");
    lambda.invoke({
        FunctionName: 'testLambda',
        Payload: JSON.stringify(event, null, 2) // pass params
    }, function (error, data) {
        if (error) {
            console.log("error");
            callback(null, 'hello world');
        }
        else {
            console.log("lambda invoke end");
            callback(null, 'hello world');
        }
    });
}

但是在我的情况下,"testLambda"是一个耗时的函数.因为我需要在调用"testLambda"函数后立即退出.然后像这样更新代码

But in my case, 'testLambda' is a time taken function. Because i need to exit just after invoking the 'testLambda' function. Then code is updated like this

exports.handler = (event, context, callback) => {
    var aws = require('aws-sdk');
    var lambda = new aws.Lambda({
        region: 'myregion' //change to your region
    });
    console.log("lambda invoke started");
    lambda.invoke({
        FunctionName: 'testLambda',
        Payload: JSON.stringify(event, null, 2) // pass params
    });
    console.log("lambda invoke end");
    callback(null, 'hello world');
}

它正确返回消息.但是我的'testLambda'函数没有被调用(没有为测试lambda生成云监视日志).与此代码相关的问题是什么.

it returns message correctly . But my 'testLambda' function is not invoked(no cloud watch logs are generated for test lambda). what is the issue related with this code.

推荐答案

每个 Lambda invoke()文档,您将看到默认情况下使用RequestResponse调用类型来调用Lambda函数.要异步调用该函数,您需要指定Event调用类型,如下所示:

Per the Lambda invoke() documentation, you will see that by default the Lambda function is invoked using the RequestResponse invocation type. To invoke the function asynchronously you need to specify the Event invocation type, like so:

lambda.invoke({
    FunctionName: 'testLambda',
    InvocationType: 'Event',
    Payload: JSON.stringify(event, null, 2)
},function(err,data){});

这篇关于从另一个lambda异步调用aws lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆