@aws-sdk/client-sqs 在 lambda 层中不起作用

问题描述 投票:0回答:1

我正在尝试使用 @aws-sdk/client-sqs 包将消息从 lambda 层推送到 sqs,但这不起作用,也不会引发任何错误。它在本地和 lambda 中都可以工作,但是从 lambda 层尝试时会发生错误。

该层代码如下:

const crypto = require("crypto");
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
const { getContext } = require('./context');
const { messageGroupId } = require('./constants');

const pushError = async (type, error) => {
    const context = getContext();
    const attributes = {
        type: {
            DataType: "String",
            StringValue: type,
        },
        message: {
            DataType: "String",
            StringValue: error.message,
        },
        name: {
            DataType: "String",
            StringValue: error?.name
        },
        stack: {
            DataType: "String",
            StringValue: error?.stack
        },
        functionName: {
            DataType: "String",
            StringValue: context.functionName
        },
        logGroup: {
            DataType: "String",
            StringValue: context.logGroupName
        },
        logStream: {
            DataType: "String",
            StringValue: context.logStreamName
        }
    };
    const hash = crypto.createHash("sha256");
    hash.update(JSON.stringify(attributes));
    const deduplicationId = hash.digest("hex");
    const params = {
        MessageBody: error.message,
        QueueUrl: process.env.sqsUrl,
        MessageAttributes: attributes,
        MessageDeduplicationId: deduplicationId,
        MessageGroupId: messageGroupId
    }

    const sendMessageCommand = new SendMessageCommand(params);
    try {
        const sqsClient = new SQSClient({ region: process.env.region , accessKeyId: process.env.accessKey, secretAccessKey: process.env.secretKey});
        console.log("SQS Client : ", JSON.stringify(sqsClient));
        console.log('before pushing message : ',process.env.region );
        console.log('Remaining time: ', context.getRemainingTimeInMillis());
       
        const response = await sqsClient.send(sendMessageCommand);
         
        console.log("Message sent successfully. Message ID:" , response.MessageId);
        console.log('after pushing message : ', process.env.sqsUrl);
    }
    catch (error) {
        console.error("An error occurred while alerting error: ", error);
    }
}


module.exports = { pushError };

控制台直到打印出剩余时间:,但之后 lambda 就会停止。还剩很多时间,lambda 没有超时。我已经在 lambda 配置中给出了代码中使用的所有环境变量。我已经添加了 SQS 对 lambda 的完全访问权限 + 给出的 accessKey 和 SecretKey 也具有对 sqs 的完全访问权限。

调用函数如下:

const EventEmitter = require('events');
const { errorConstants } = require('./constants');
const {pushError} = require('./sqs');

const eventEmitter = new EventEmitter();

const event = () => {
   return eventEmitter;
}

const events = [errorConstants.file, errorConstants.dynamo, errorConstants.rabbitMQ, errorConstants.email, errorConstants.fetch, errorConstants.scheduler, errorConstants.storage, errorConstants.event];

for (const event of events) {
   eventEmitter.on(event, async (error) => {
       try {
           console.log('before pushError function call ', event, error);
          await pushError(event, error);
          console.log('after pushError function call ', event, error);
       } catch (error) {
           console.log(`Error while pushing ${event} `, error);
       }
   });
};

module.exports = { event };

在签入图层和 lambda 时,sqsClient 会以相同的方式正确创建

node.js amazon-web-services aws-lambda amazon-sqs aws-lambda-layers
1个回答
0
投票

问题是我发出了一个事件(fn1)来调用pushError函数。 由于发出事件,我们无法添加等待,因此当代码到达 client.send 行时,它会返回到我发出事件(fn1)的位置,并且该函数即将结束,并且 lambda 被终止。通过在 sqs 消息推送后发出另一个事件并在 fn1 中的 Promise 中捕获该事件来修复此问题。 感谢@jarmod、@Corey 和@mpmcintyre 的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.