Skip to content

Instantly share code, notes, and snippets.

@kevinhooke
Created November 17, 2022 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinhooke/cffa89efd51b074f8b9162c0a7c8ff4c to your computer and use it in GitHub Desktop.
Save kevinhooke/cffa89efd51b074f8b9162c0a7c8ff4c to your computer and use it in GitHub Desktop.
AWS SDK Lambda invokAsync
If you call lambda.invokeAsync() from one lambda calling another, if the calling lambda completes/exits before the second
lambda has been successfully invoked then depending on the timing it's possible the second Lambda will not invoke sucessfully.
The SDK docs show calling invokeAsync with a callback:
const params = {
"FunctionName": "fucntion-name-to-invoke",
"InvokeArgs": JSON.stringify(payload-to-pass-to-lambda)
};
lambda.invokeAsync(params, function (error, result) {
if (error){
console.error(`Error occurred attempting invokeAsync call: ${JSON.stringify(error)}`);
throw (error);
}
else {
console.log(`Result: ${JSON.stringify(result)}`);
}
});
This will not wait for the callback to complete - if the calling Lambda completes and exists before invokeAsync successfully
calls the next Lambda then it won't get invoked. Adding await on the invokeAync call also does not work as this call
is not returning a promise.
To get this to work as expected, call .promise() and then you can await the result:
try{
let result = await lambda.invokeAsync(params).promise();
console.log(`Result: ${JSON.stringify(result)}`);
}
catch(error){
console.error(`Error occurred attempting invokeAsync call: ${JSON.stringify(error)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment