AWS Lambda integrates with other AWS services to invoke functions. You can configure triggers to invoke a function in response to resource lifecycle events, respond to incoming HTTP requests, consume events from a queue, or run on a schedule, and so on.
In this post, we’re discussing two event sources of Lambda:
- AWS SNS events.
- Asynchronously calls by other Lambda.
And comparing them to the APIGW (API Gateway).
SNS (Simple Notifications Service)
When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message.
- Amazon SNS allows applications to send time-critical messages to multiple subscribers through a “push” mechanism, eliminating the need to periodically check or “poll” for updates. (this is like notifications, alerts, etc.)
- Amazon SNS allows implementing an event processing pipeline with potentially multiple consumers and producers.
Does SNS Provide Error Recovery?
In one case which is when the system that hosts the subscribed endpoint becomes unavailable, in this case, Amazon SNS retries the delivery of messages.
In general, message delivery fails when Amazon SNS can’t access a subscribed endpoint due to a client-side or server-side error. When the SNS continues to receive a server-side error for a message beyond the number of retries specified by the corresponding retry policy it stops retrying the delivery and discards the message unless a dead-letter queue is attached to the subscription.
A dead-letter queue is an Amazon SQS queue that an Amazon SNS subscription can target for messages that can’t be delivered to subscribers successfully. for further analysis or reprocessing.
A Common Architecture is Combining SNS & SQS (Simple Queue Service)
Both services provide different benefits for developers. The benefits for using Amazon SNS are stated above. On the other hand, Amazon SQS is a message queue service used by distributed applications to exchange messages through a polling model, and can be used to decouple sending and receiving components, without requiring each component to be concurrently available. Using Amazon SNS and Amazon SQS together, messages can be delivered to applications that require immediate notification of an event, and also persisted in an Amazon SQS queue for other applications to process at a later time. Also, SQS provides a full error recovery since the consumer owns the decision to delete the message from the queue if it was processed correctly or not, if a message was returned to the queue the delivery will be retried according to the delivery policy.
You pay based on the number of notifications you publish, the number of notifications you deliver, and any additional API calls for managing topics and subscriptions. Where, the delivery pricing varies by endpoint type.
Includes publishing, topic owner operations, and subscriber operations, but not deliveries
- First 1 million Amazon SNS requests per month are free.
- $0.50 per 1 million Amazon SNS requests thereafter.
- Amazon SNS currently allows a maximum size of 256 KB for published messages. Each 64KB chunk of published data is billed as 1 request. For example, a single API call with a 256KB payload will be billed as four requests.
- No charge for deliveries to SQS Queues. Normal SQS pricing applies.
- No charge for deliveries to Lambda. Normal Lambda pricing applies.
SNS VS. APIGW
|Free tier||Unlimited. First 1 million messages free every month.||Limited. Free first 1 million messages every month for 12 months after sign-up.|
|Small requests cost||cheaper more than 50% for requests <= 128KB||more expensive for requests <= 128KB|
|Big requests cost||more expensive for requests > 128 KB||cheaper for requests > 128 KB|
|Payload size limit||256KB||15MB|
|Invoked Lambda timeout||15 mins||30 s|
When a Lambda function is invoked synchronously, it will keep the connection open until the execution is finished. Once it’s done, it returns the response provided by the function’s code (or an error, if that’s the case.)
When you invoke a function asynchronously, Lambda sends the event to a queue. A separate process reads events from the queue and runs your function. When the event is successfully added to the queue, Lambda returns a success message to the client. This is called Async Lambda.
Async Lambda Pricing
The Lambda cost for the invoker and the invoked functions applies. No additional cost for the requests or payload size.
Async Lambda VS. APIGW
|cheaper 100%||more expensive|
|Payload size limit||256KB||15MB|
|Invoked Lambda timeout||15 mins||30s|
We talked about SNS and Async Lambda as Lambda event sources and compared them to APIGW.
There is no absolute best event source that should be used. In practice, we need to find the best solution for the problem in hand taking the pros and cons of each source in the specified situation into our consideration.