AWS Lambda 函数似乎挂起

问题描述

这很可能是我的误解,但它是这样的。我有一个 lambda 函数。它目前唯一要做的就是从机密管理器中检索机密。如果我在调试器(在本例中为 cloud9)的异步函数中运行它,它就可以工作。一旦我将它放入处理程序并将函数上传到 lambda 然后对其进行测试,它就会超时。目前,该功能的超时设置为 1 分 30 秒。这应该远远超过它所需要的,因为它在 Cloud9 中几乎立即返回。我在 CloudWatch 中看不到任何有用的东西。我觉得要么我的方法不正确,要么我遗漏了一些明显的东西,或者只是误解了一些基本的东西。

示例函数

    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
}

解决方法

您没有调用回调函数。 试试

    exports.handler = (event,context,callback) =>{
    var value = await GetSecrets('SecretName')
    const response = {
    statusCode: 200,body: JSON.stringify({ message: 'hello world' })
    }
    callback(null,'success');
    }

回调函数用于返回成功或失败。这需要传递才能完成 lambda 函数。 如果您想向 lambda 返回响应,请使用响应而不是“成功”