I'm looking for a solution whereby I can safely (and nearly reliably) emit CW metrics (putMetricData) within synchronous code. My use case is as follows:
// Lambda Main Handler
export const handler = async () => {
await step1Aync();
step2Sync();
step3Sync();
return {};
}
async function step1Aync() {
await doSomething();
await doSomething();
// could await this call, but dont want to since I dont need the value, nor do I want to handle failures
cloudwatch.putMetricData();
}
function step2Sync() {
doSomething();
// do not want to make this function async to use await here
cloudwatch.putMetricData();
}
function step3Sync() {
doSomething();
// do not want to make this function async to use await here
cloudwatch.putMetricData();
}
My understanding is that as the putMetricData requests are pushed onto Lambda's Event Loop, there is no guarantee that they end up sending their HTTP request to CW (as Lambda execution context may freeze before the request happens). That is of course, unless I make step2Sync and step3Sync asynchronous and use await for each Promise.
Part of my reasoning here is that I'd like reduce the response time of the Lambda by specifically not handling the responses from CloudWatch. This Lambda is a time-sensitive API (API Gateway). If 'some' requests to CW fail (less than 1%), thats okay.
I read that if the same Lambda Context is used again, it will empty the event loop. This is however, NOT guaranteed. I dont know what the SLA here is, so to speak.
Any who, in short, I dont want to use async functions when I don't explicitly have to. Is my understanding here correct?