Wuubb
Wuubb

Reputation: 71

AWS Lambda not invoked when using 'Event' invocation type

I have 2 lambdas, test1 which invokes test2. I want to invoke test2 asynchronously with the "fire and forget" mentality.

Below is the code for both lambdas. When I set the InvocationType to RequestResponse, the function gets invoked and there is a log in CloudWatch. When I change the InvocationType to Event, there is no record in CloudWatch of the function being invoked.

test1:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();

exports.handler = async (event) => {
    const params = {
        'FunctionName': 'test2',
        'InvocationType': 'Event',
        'Payload': JSON.stringify('Hello')
    };

    const response = await lambda.invoke(params).promise();
    return response;
};

test2:

exports.handler = async (event) => {
    console.log(event);
    return 'Success';
};

The IAM policy includes invoke permissions (otherwise the RequestResponse type wouldn't work either). I've also read about limits on the size of the args you can pass, but in this case I'm only passing "Hello"...what gives?

EDIT: I should also add that in every case I get a response code of 202, which indicates a successful invoke, I just see no evidence of the function being executed in the logs.

EDIT 2: I've added await to the end of test1, but still do not see any indication that test2 was invoked in CloudWatch.

Upvotes: 3

Views: 2824

Answers (1)

ttulka
ttulka

Reputation: 10892

I think there is just await missing:

exports.handler = async (event) => {
    const params = {
        'FunctionName': 'test2',
        'InvocationType': 'Event',
        'Payload': JSON.stringify('Hello')
    };

    await lambda.invoke(params).promise();
};

Without await a promise from lambda.invoke(params).promise() exites the lambda execution without resolving that promise.

Because the lambda is called asynchronously there is no result returned.

You can check the CloudWatch log of the test2 lambda to see if has been invoked.

Upvotes: 5

Related Questions