3
votes

I have an AWS Lambda function, that need's ~ 30 seconds. When I connect it to the API Gateway, it's sending a 504 because of the 5 second timeout. So my easyCron Job is failing and will not try it again (I only have a free plan)

So I need an API, that sends a correct 200 status. My Idea:

Invoke the long term lambda via a short term lambda. The policy is allowing the invocation.

Here is the code

var AWS = require('aws-sdk'),

	params = {
		FunctionName: 'cctv',
		InvocationType: 'RequestResponse',
		LogType: 'Tail'
	},
	lambda;
AWS.config.update({region: 'us-east-1'});
lambda = new AWS.Lambda();

exports.handler = function (event, context) {
	'use strict';
	lambda.invoke(params, function (err, data) {
		if (err) {
			console.log(err, err.stack);
		}
		else {
			console.log(data);
		}
	});
	context.succeed('hey cron job, I think my lambda function is not called');

};

But I think, context.succeed() aborts the execution of lambda.invoke()

Do you have any idea how to solve this?

2
You're not actually calling the code that calls your Lambda function. You're defining a function to do the work into exports.handler, but that's not being called. - Matt Houser
This code is called by the API gateway. It's printing the succeed message, but it's not invoking the second lambda function 'cctv' - Christian Haller
Yes, because the code you listed above is not actually calling lambda.invoke. So your level 1 function is being called, but your level 1 function is not actually calling your level 2 function. - Matt Houser
I connected API Gateway with the first Lamba Function. It's calling the handler(). I thinks it's more or less this code: require('./index').handler({},{succeed:function(message){ console.log(message); },'error':function(){}}); So I guess lambda.invoke()should be called - Christian Haller
Can you show the complete level 1 source code? - Matt Houser

2 Answers

6
votes

This is incorrect

InvocationType: 'RequestResponse'

You should use

InvocationType: 'Event'

From http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax

By default, the Invoke API assumes "RequestResponse" invocation type. You can optionally request asynchronous execution by specifying "Event" as the InvocationType.

0
votes

Rather than directly calling your 30+ second Lambda function, you could trigger it from an SNS or S3 PutObject event. This would be asynchronous to your API Gateway route, so it can return very quickly. Of course, you would not have the outcome of the Lamdba job at that time.