I need help triggering the start of a Lambda function from a different Lambda function. It would be nice to know if the triggering was successful or not, but not required. I have made many unsuccessful attempts to accomplish this. I’m hoping that the community can help me out.
So far I have done the following.
- Created a role manually in AWS and assign
- Add extra permissions to the provider section in the
serverless.yml
file - Call
invoke
withInvocationType
set toEvent
- Call
invokeAsync
- Call
invoke
without a callback and manually callsend
What does it mean to fail?
I’ve set the test function’s run time to be 30 seconds. All runs show the standard 3 log messages for start, end, and report. In between the start and end messages I should see a pair of messages before and after the call to invoke. Failure is happening because I only get the before invoke log message.
Details
My test function is the following
export const handler = (event, context, callback) => {
console.log('event', JSON.stringify(event, true));
console.log('context', JSON.stringify(context, true));
const functionName = 'the-other-function; // this exists
const payload = { foo: 'bar' };
console.log('before invoke');
promiseInvoke({ functionName, payload })
.then(() => {
console.log('after invoke');
callback(null, null);
})
.catch(error => {
console.error(error);
callback(error);
});
console.log('handler exit');
};
- Created a role manually in AWS and assign
I created a IAM role manually and assigned admin level privileges. Then for the test function I set its role
property in the serverless.yml
file. This failed in the same way as the other attempts.
- Add extra permissions to the provider section in the
serverless.yml
file
I followed the results of forum post. I also needed to connect to a DB so I included some ec2 permissions to connect to the network. This failed in the same way as the other attempts.
provider:
name: aws
iamRoleStatements:
- Effect: 'Allow'
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
- "ec2:CreateNetworkInterface"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DeleteNetworkInterface"
- "lambda:InvokeFunction"
- "lambda:InvokeAsync"
Resource: "*"
- Call
invoke
withInvocationType
set toEvent
- Call
invokeAsync
- Call
invoke
without a callback and manually callsend
I created several methods of calling the invoke
method from the aws-sdk
for JavaScript. I put these in a Gist on Github. The AWS docs say to use the invoke
method since the invokeAsync
method is deprecated. Even so, I tried that method too.
The basic method was the following. This failed in the same way as the other attempts.
export const promiseInvoke = ({ functionName, payload }) => {
console.log('Starting promiseInvoke with native promise');
const lambda = new AWS.Lambda();
return lambda.invoke({
InvocationType: 'Event',
FunctionName: functionName,
LogType: 'None',
Payload: JSON.stringify(payload)
}).promise();
};