I'm designing a rate-limiting workflow with an SQS queue. A client durable Lambda places a request on the queue and waits for a callback. A server durable Lambda consumes the queue with maximum concurrency set to 1, checks when the rate-limited resource was last used, waits if necessary, and then performs the operation before invoking the callback.
I'm defining this with the AWS CDK. The following setup worked previously for a regular Lambda consuming SQS:
const mapping = new sources.SqsEventSource(bggQueue, { batchSize: 1, enabled: false });
bggLambda.addEventSource(mapping);
However, creating the event source mapping fails with an error saying that a durable function cannot be invoked using an unqualified ARN. I understand that durable functions require a specific qualified version or alias, 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 to the durable Lambda?
3 Answers
Attach the event source to a qualified function version rather than to the unqualified function itself. For example:
const mapping = new sources.SqsEventSource(bggQueue, { batchSize: 1, enabled: false });
bggLambda.latestVersion.addEventSource(mapping);
The important part is using `latestVersion` (or another explicitly published version or alias), which gives the event source mapping a qualified Lambda ARN. You can also use the lower-level `CfnEventSourceMapping` construct if you need to configure the mapping directly.
If the higher-level CDK construct does not expose everything you need, use the L1 `CfnEventSourceMapping` resource. It lets you provide the event source ARN and a qualified function ARN explicitly. For this case, though, using a published version or alias through the Function object should normally be enough.
Double-check whether a durable Lambda is necessary for the rate limiter. An SQS consumer with reserved or maximum concurrency of one, combined with visibility timeout and retry handling, may be sufficient depending on how long the wait and callback workflow lasts. If the function genuinely needs durable execution semantics, make sure the event source targets a qualified version or alias.

That appears to be the missing piece. I can replace `bggLambda.addEventSource(mapping)` with `bggLambda.latestVersion.addEventSource(mapping)` and test whether the mapping is created successfully.