How do I attach an SQS event source to a durable Lambda with CDK?

0
8
Asked By MellowPine47 On

I'm designing a rate-limiting workflow with an SQS queue. A client-side durable Lambda places requests on the queue and waits for a callback. A server-side durable Lambda consumes the queue with maximum concurrency set to 1, checks when the rate-limited resource was last used, waits if necessary, then performs the operation and calls back.

I'm defining this in AWS CDK with an SQS event source:

const mapping = new sources.SqsEventSource(bggQueue, { batchSize: 1, enabled: false });
bggLambda.addEventSource(mapping);

However, deployment fails while creating the event source mapping with this message: "You cannot invoke a durable function using an unqualified ARN."

I understand that a durable Lambda probably needs a qualified ARN pointing to a specific function version, but I'm not sure how to express that through the CDK Function object. What is the correct way to attach the SQS event source mapping?

3 Answers

Answered By QuietHarbor8 On

A durable Lambda needs to be referenced through a qualified version rather than the unqualified function ARN. In CDK, create the event source mapping as usual, but attach it to the function’s version object instead of the function itself:

bggLambda.latestVersion.addEventSource(mapping);

That should cause the mapping to target a qualified function ARN. Depending on how you deploy versions, you may also want to use an explicitly published version or alias so the event source remains tied to the intended version.

MellowPine47 -

That looks like the missing piece. I can change the attachment to bggLambda.latestVersion.addEventSource(mapping), and I’m going to verify the deployment and invocation behavior.

Answered By AmberCedar29 On

If the higher-level CDK construct doesn’t expose the exact target configuration you need, use the lower-level CloudFormation construct, CfnEventSourceMapping. It lets you specify the function name or qualified ARN and all event-source-mapping properties directly. That’s a useful fallback when the L2 SqsEventSource abstraction doesn’t handle a newer Lambda feature cleanly.

Answered By SilverLark63 On

It may also be worth checking whether a durable Lambda is necessary for the rate limiter. A regular Lambda consuming the queue with batch size 1 and reserved or maximum concurrency of 1 might be enough, with the waiting and callback behavior implemented using the surrounding workflow. Durable execution adds the qualified-version requirement and some extra deployment complexity.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.