简体   繁体   English

为什么我的 AWS SQS 消息即使不符合筛选条件也会被删除?

[英]Why are my AWS SQS messages being deleted even though they don't match the filter criteria?

I'm using an AWS Lambda function with an SQS trigger to process messages in a queue.我使用带有 SQS 触发器的 AWS Lambda 函数来处理队列中的消息。

I've set up a filter pattern on the trigger to only allow messages with a specific value in the body to be processed.我在触发器上设置了一个过滤器模式,只允许处理正文中具有特定值的消息。 However, I've noticed that some messages that don't match the filter criteria are being deleted from the queue without being processed.但是,我注意到一些不符合过滤条件的消息未经处理就从队列中删除。

Here is my trigger configuration:这是我的触发器配置:

import type { AWS } from '@serverless/typescript'
import { handlerPath } from '@libs/handler-resolver'

import { QueueArn } from '@libs/sqs'

export default {
  handler: `${handlerPath(__dirname)}/handler.main`,
  events: [
    {
      sqs: {
        arn: QueueArn,
        enabled: true,
        batchSize: 10,
        maximumBatchingWindow: 1,
        functionResponseType: 'ReportBatchItemFailures',
        filterPatterns: [
          {
            body: {
              type: ['EMAIL'],
              action: [{ exists: true }],
              payload: [{ exists: true }]
            }
          }
        ]
      }
    }
  ]
} as AWS['functions'][keyof AWS['functions']]

I want to keep the messages that don't match the filter criteria in the queue, so they can be processed at a later time or by a different function.我想将不符合过滤条件的消息保留在队列中,以便稍后处理或由不同的函数处理。

Is there a way to configure the trigger to not delete these messages?有没有办法将触发器配置为不删除这些消息?

I have tried the following to solve this problem:我已尝试以下方法来解决此问题:

  • Checked the SQS queue policy to ensure that it allows the Lambda function to receive and delete messages检查 SQS 队列策略以确保它允许 Lambda 函数接收和删除消息
  • Checked the Lambda function execution role to ensure that it has the necessary permissions to access the SQS queue检查 Lambda 函数执行角色以确保它具有访问 SQS 队列的必要权限
  • Verified that the filter pattern is correctly set in the trigger configuration.已验证过滤器模式是否在触发器配置中正确设置。
  • Verified that the Lambda function is correctly handling and deleting messages that match the filter criteria已验证 Lambda 函数是否正确处理和删除符合筛选条件的消息
  • Confirmed that the visibility timeout of the queue is set to a sufficient amount of time.确认队列的可见性超时设置为足够的时间。
  • Tested sending messages with different body attributes to the queue and confirmed that the filter pattern is working as expected for some messages.测试了将具有不同正文属性的消息发送到队列,并确认过滤器模式对某些消息按预期工作。

However, the issue persists and I am not sure what could be causing it.但是,问题仍然存在,我不确定是什么原因造成的。 Any help or suggestions would be greatly appreciated.任何帮助或建议将不胜感激。

Thanks @luk2302, for your suggestion of using SNS to fanout to other SQS queues.感谢@luk2302,感谢您建议使用 SNS 扇出到其他 SQS 队列。 I ended up going with this approach and it worked out well for me.我最终采用了这种方法,并且对我来说效果很好。

Instead of using a filter pattern on a single SQS queue, I created a separate SQS queue for each of my AWS Lambdas and set up a subscription from the SNS topic to each of those queues.我没有在单个 SQS 队列上使用过滤器模式,而是为我的每个 AWS Lambda 创建了一个单独的 SQS 队列,并设置了从 SNS 主题到每个队列的订阅。 This way, I can route the messages to the appropriate Lambda based on the message content, rather than relying on a filter pattern to do that for me.这样,我可以根据消息内容将消息路由到适当的 Lambda,而不是依赖过滤器模式来为我做这件事。

I appreciate your help and I'm glad I was able to solve my issue.感谢您的帮助,我很高兴能够解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM