AWS Lambda call Lambda

别等时光非礼了梦想. 提交于 2020-01-16 01:19:11

问题


I'm trying to call one Lambda function from another one that I have. I set up my permissions so that is not problem.

My problem is that the function doesn't wait for the Invoke function to complete and return NULL all the time.

Here is the code I'm using:

const AWS = require('aws-sdk');

exports.handler = async (event, context, callback) => {

    var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

    var params = {
        FunctionName: 'testFunction',
        InvocationType: 'RequestResponse'
    }
    lambda.invoke(params, function(err, data){
        console.log(err);
        console.log('here');
    }).promise().then(data=> { callback(null, {message:'done'}); });

};

The {message:'done'} its never shown. I was recommended to use invokeAsync but that function is deprecated by AWS.

I know the problem is that the function is running lambda.invoke as synchronously because if I add callback(null, {message:'done'}); outside of the lambda.invoke function then I can see the console.logs working.

Any help?


回答1:


TL;DR - Remove "async" in line 3, and it should work.

Your issue seems to be caused by the async keyword here. I have recreated this and deployed it to Lambda to run on Node v8.10 (but pointing it to invoke one of my own lambda functions of course).

Why are you using "async" here anyway? The async keyword declaration defines an asynchronous function and returns an AsyncFunction object. AWS Lambda is expected a function, not an AsyncFunction, and your "null" result is probably just Lambda immediately giving up because it can't find a regular function. Also, async is almost exclusively used with await (at least is was in 99% of the cases I've seen), and since your code isn't using await at all I don't see any reason to use async either.



来源:https://stackoverflow.com/questions/49693346/aws-lambda-call-lambda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!