Created
November 17, 2022 22:11
-
-
Save kevinhooke/cffa89efd51b074f8b9162c0a7c8ff4c to your computer and use it in GitHub Desktop.
AWS SDK Lambda invokAsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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