简体   繁体   中英

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.

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
  • Checked the Lambda function execution role to ensure that it has the necessary permissions to access the SQS queue
  • 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
  • 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. 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. 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.

I appreciate your help and I'm glad I was able to solve my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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