3

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?

6
  • 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. Commented Sep 25, 2015 at 13:40
  • This code is called by the API gateway. It's printing the succeed message, but it's not invoking the second lambda function 'cctv' Commented Sep 25, 2015 at 15:29
  • 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. Commented Sep 25, 2015 at 16:04
  • 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 Commented Sep 25, 2015 at 18:26
  • Can you show the complete level 1 source code? Commented Sep 25, 2015 at 19:19

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! With InvocationType: 'Event' I get a 202! :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.