SQS-triggered Lambda¶
A single resource that connects a queue to a function.
def sqs_event_source(function_logical: str, queue_arn: Expr[str], batch_size: int = 10) -> lambda_.EventSourceMapping:
"""Trigger the Lambda `function_logical` from an SQS queue."""
return lambda_.event_source_mapping(
BatchSize=batch_size,
EventSourceArn=queue_arn,
FunctionName=get_att(function_logical, "Arn"),
)
Things worth noticing¶
- The function is named, not passed.
function_logicalis a logical ID, and the pattern turns it intoget_att(function_logical, "Arn"). That keeps the call site short, at the cost of a string the checker cannot verify. It is the standard trade-off of plain dicts, and the next page shows how to guard the related duplicate-ID mistake. - The queue is an
Expr[str]. Pass aget_attfor a queue in the same template, or a literal ARN orimport_value(...)for one in another stack. - It is deliberately small. A pattern that wraps one resource is still worth
having when the wiring (here,
FunctionNamefrom aGetAtt) is what you keep retyping.