This is most likely a misunderstanding on my part but here it goes. I have a lambda function. The only thing it is doing at the moment is retrieving a secret from secrets manager. If I run it in an async function in my debugger (cloud9 in this case) it works. Once I put it in the handler and upload the function to lambda and then test it, it times out. Currently the time out on the function is set to 1 minute 30 seconds. That should be way over what it needs as it returns almost instantly in Cloud9. I don't see anything in CloudWatch that would be helpful. I feel like either my approach is incorrect or I'm missing something obvious or just misunderstanding something fundamental.
Example Function
async function GetSecrets(secretName) {
// Load the AWS SDK
var AWS = require('aws-sdk'),
region = "Region Secret is in",
secretName = secretName,
secret,
decodedBinarySecret;
// Create a Secrets Manager client
var client = new AWS.SecretsManager({
region: region
});
return new Promise((resolve,reject)=>{
client.getSecretValue({SecretId: secretName}, function(err, data) {
// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
// We rethrow the exception by default.
if (err) {
reject(err);
}
else {
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if ('SecretString' in data) {
console.log("I got here and here is the data: " + data)
resolve(data.SecretString);
} else {
let buff = new Buffer(data.SecretBinary, 'base64');
resolve(buff.toString('ascii'));
}
}
});
});
}
// This Works in Cloud9
/*(async myFunc =>{
var value = await GetSecrets('SecretName')
console.log(value)
})();
*/
// inside handler - this times out in lambda
exports.handler = async function(event, context) {
var value = await GetSecrets('SecretName')
const response = {
statusCode: 200,
body: JSON.stringify({ message: 'hello world' })
}
return response
}
You are not calling callback function. try
exports.handler = (event, context, callback) =>{
var value = await GetSecrets('SecretName')
const response = {
statusCode: 200,
body: JSON.stringify({ message: 'hello world' })
}
callback(null, 'success');
}
callback function is used to return success or failure.This needs to be passed for lambda function to be completed. If you want to return response to lambda use response instead of 'success'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With