I've a lambda deployed on AWS, in a VPC that has internet access via NAT. The deploy is made using Serverless.
The lambda uses some Middy middlewares and fetches some credentials from SSM.
The problem is that the SSM fetch randomly goes in timeout!
Here's the lambda code:
/* requirements are omitted */
const authorize = async (_event, _context) => {
try {
const ssm = new SSM({
maxRetries: 6, // lowers a chance to hit service rate limits, default is 3
retryDelayOptions: { base: 200 }
})
const params = {
Names: ["param1", "param2"],
WithDecryption: true
}
const fetch = () => new Promise(resolve => {
ssm.getParameters(params, function(err, data) {
if (err) resolve(err, err.stack); // an error occurred
else resolve(data); // successful response
})
})
const res = await fetch()
return {
statusCode: 200,
body: JSON.stringify(res)
}
} catch (_err) {
console.error(_err)
return {
statusCode: 500,
body: 'error'
}
}
}
export default middy(authorize)
.use(warmup({ waitForEmptyEventLoop: false }))
.use(doNotWaitForEmptyEventLoop({ runOnError: true }))
.use(httpSecurityHeaders())
question from:
https://stackoverflow.com/questions/65951537/aws-lambda-ssm-calls-randomly-goes-in-timeout 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…