在我的 nodejs lambda 函数中,aws SQSClient 将批处理请求发送到 dlq 队列
为什么我的发送请求总是超时失败?
const sqs = new SQSClient({ region: process.env.AWS_REGION ?? 'eu-west-3' });
export async function sendFailedEventsToDLQ<T>(messages: T[], context: Context, dlqName: string | undefined): Promise<void> {
if (!messages.length) return;
const entries: SendMessageBatchRequestEntry[] = messages.map((message) => {
return { Id: dlqName, MessageBody: formatSQSMessage<T>(message) };
});
const params = new SendMessageBatchCommand({
QueueUrl: getDLQUrl(context, dlqName),
Entries: entries,
});
logger.info({ params }, 'Sending messages to dlq...');
const response = await sqs.send(params).catch((error) => logger.error(error)); // <-- the line that is failing in timeout
logger.info({ response }, 'Messages has been sent to dlq !');
}
function formatSQSMessage<T>(message: T): string {
const foramtedMessage = {
Message: JSON.stringify(message),
};
return JSON.stringify(foramtedMessage);
}
function getDLQUrl(context: Context, dlqName: string | undefined): string {
if (!dlqName) throw new Error(Errors.ENV_VARIABLE_DLQ_NAME_UNDEFINED);
const region = context.invokedFunctionArn.split(':')[3];
const accountId = context.invokedFunctionArn.split(':')[4];
return `https://sqs.${region}.amazonaws.com/${accountId}/${dlqName}`;
}
我已经在我的 serverless.ts 文件中允许了 sendMessageBatch
{
Effect: 'Allow',
Action: ['SQS:SendMessage', 'SQS:SendMessageBatch', 'SQS:ReceiveMessage', 'SQS:DeleteMessage', 'SQS:GetQueueAttributes'],
Resource: '*',
},
我日志中的确切错误就在参数日志之后
2023-04-16T01:22:31.893+02:00 {"level":30,"time":1681600951855,"pid":8,"hostname":"169.254.55.108","params":{"middlewareStack":{},"input":{"QueueUrl":"https://sqs.eu-west-3.amazonaws.com/907108099392/mailing-webhook-service-update-pros-queue-dlq-prodv1","Entries":[{"Id":"mailing-webhook-service-update-pros-queue-dlq-prodv1","MessageBody":"{\"Message\":\"{\\\"event-data\\\":{\\\"recipient\\\":\\\"[email protected]\\\",\\\"event\\\":\\\"opened\\\"},\\\"dlqError\\\":{\\\"dlqName\\\":\\\"mailing-webhook-service-update-pros-queue-dlq-prodv1\\\",\\\"errorCode\\\":\\\"NotProcessedMessagesError\\\"}}\"}"}]}},"msg":"Sending messages to dlq..."}
2023-04-16T01:22:41.834+02:00 2023-04-15T23:22:41.834Z df91f075-eba4-5606-ad66-acc0d80a7d39 Task timed out after 10.01 seconds
我的 lambda 配置
export default {
handler: `${getCurrentFolderPath(__dirname)}/handler.main`,
timeout: 10,
memorySize: 512,
vpc: {
securityGroupIds: VPC_SECURITY_GROUP_IDS,
subnetIds: VPC_SUBNET_IDS,
},
events: [
{
sqs: {
arn: {
'Fn::GetAtt': ['${self:custom.updateProsQueue}', 'Arn'],
},
batchSize: 10,
maximumBatchingWindow: 60,
},
},
],
};