提问人:Vaidhyaprakash I 提问时间:9/26/2023 最后编辑:Vaidhyaprakash I 更新时间:9/29/2023 访问量:120
@aws-sdk/client-sqs 在 lambda 层中不起作用
@aws-sdk/client-sqs is not working in lambda layer
问:
我正在尝试使用 @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 };
控制台直到 Remaining time: 被打印出来,但在那之后 lambda 被停止。还有很多剩余时间,lambda 不会超时。我已经给出了 lambda 配置中代码中使用的所有 env 变量。我添加了 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 以相同的方式正确创建
答:
0赞
Vaidhyaprakash I
9/29/2023
#1
问题是我发出了一个事件(fn1)来调用pushError函数。 由于发出事件我们无法添加 await,当代码到达 client.send 行时,它会回到我发出 event(fn1) 的地方,并且该函数结束,lambda 被终止。通过在 sqs 消息推送后发出另一个事件并在 fn1 的 Promise 中捕获该事件来修复此问题。 感谢@jarmod、@Corey和@mpmcintyre的帮助。
评论
new SQSClient({ region, credentials: { accessKeyId, secretAccessKey, sessionToken } })