I am trying to push a message from lambda layer to sqs using the @aws-sdk/client-sqs package, but this does'nt work and also does'nt throws any error. It is working in local and in lambda, but error occurs when trying from lambda layer.
The code in the layer is as follows :
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 };
The console till Remaining time: gets printed, but after that the lambda is getting stopped. There are lot of time remaining , the lambda is not getting timed out. I have given all the env variables used in code in lambda configuration. I have added SQS full access to lambda + the accessKey and secretKey given also has the full access to sqs.
The calling function is as follows:
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 };
The sqsClient is properly getting created the same way when checked in layer and lambda
The problem is I was emitting a event(fn1) for calling the pushError function. Since emitting event we cannot add await, when the code hits the client.send line , it then goes back to the place where i was emitting the event(fn1) and that function is getting ended, and lambda gets terminated. Fixed this issue by emitting another event after sqs message push and catching that event inside a Promise in fn1. Thankyou @jarmod, @Corey, and @mpmcintyre for your help.