How can I make SQS send message as a batch instead of seprately

0

I have a lambda function called service A in 5 different region, and when they are sending a message at the same time together, my lambda function called service B cannot receieve the message as a batch, but it received the message in seprate call , How can I achieve if service B can surely get the 5 message from lambda service A at one time instead of seprately getting them

Below is my code of SQS and lambda code set up

   const queue = new Queue(this, 'SqsQueue', {

            receiveMessageWaitTime: Duration.seconds(20),
            visibilityTimeout: Duration.seconds(300)

        })


        queue.grantSendMessages(role)


        const readSqsFn = new NodejsFunction(this, 'send', {
            timeout: Duration.seconds(30),
            reservedConcurrentExecutions: 1
        })



        const sesPolicy = new aws_iam.PolicyStatement({
            actions: ["ses:SendEmail",
                "ses:SendRawEmail"],
            effect: aws_iam.Effect.ALLOW,
            resources: ['*']

        })

        readSqsFn.addToRolePolicy(sesPolicy)


        queue.grantConsumeMessages(readSqsFn)

        readSqsFn.addEventSource(new SqsEventSource(queue, {
            batchSize: 10
        }))
export const handler = async (
    event: SQSEvent,
    context: any = {}
): Promise<any> => {


 

    console.log(event.Records)
    for (const record of event.Records) {

        console.log('sqs record:', record?.body);




    }




};
RECJ
질문됨 6달 전309회 조회
1개 답변
0
수락된 답변

If you configure a Lambda trigger from SQS, you can configure a batch size, and you can also configure a window size. The batch size indicates what is the maximum number of messages that your function will receive in each invocation and the window size indicates how long to wait until the batch gets full. If you do not specify a window size, your function will be invokes as soon as there will be messages in the queue.

profile pictureAWS
전문가
Uri
답변함 6달 전
profile picture
전문가
검토됨 한 달 전
  • Thank you for your help!! it works! I set the window to 60 seconds and batch = 5 in my read sqs lambda function(Service B), since my lambda(Service A) is deployed in 5 different regions and when the message need to be sent from the SQS to the service B, now 4 records will be group together and send, and then the left 1 will be sent seprately , and sometimes 3 records will be grouped together and then the another 2 records are also group together then send from the SQS to the Service B, I wonder if it is impossible to guarantee that exactly 5 records, do not seprate any of those, can get from SQS and send to the Service B

  • Because of the distributed nature of SQS and the fact that you have very low number of messages, you can't guarantee this. If you will send a larger number of messages, most invocations will include the 5 records.

    Did not try it, but if you switch to a FIFO queue, and use the same messgae group ID for all messages you send into the queue, it may also work.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠