Ref: https://learn.cantrill.io/courses/1820301/lectures/41301393
Lambda - Synchronous Invocation
Diagram: https://github.com/acantril/aws-dev-associate/blob/main/22_Lambda/00_LearningAids/Lambda6.png
- 🔧 CLI/API invokes function, passing data & waiting for response
- Function responds with data, or fails
- ❗ Result (success or failure) returned during request → Client WAITS for response!
- ❗ Errors or retries handled client-side!
- Synchronous invocation generally used when humans invoke function
- Common paradigm when humans access endpoints from API Gateway (APIGW) that trigger Lambda functions (a common serverless architecture)
Lambda - Asynchronous Invocation
Diagram: https://github.com/acantril/aws-sa-associate-saac03/blob/main/1600-SERVERLESS_and_APPLICATION_SERVICES/00_LEARNINGAIDS/Lambda-6.png
- 🔧 Generated event triggers Lambda function invocation, response is NOT tracked
- Typical pattern for AWS services that invoke functions on your behalf (e.g. S3)
- AWS service generates event & stops tracking (it's NOT waiting for response!)
- ❗ Lambda responsible for error handling!
- Function can be reprocessed up to 2 times (configurable value between 0-2)
- ‼️ Important requirement for reprocessing: function needs to be IDEMPOTENT!!
- Idempotent operations are those that can be run many times and the result will be the same (you reach a desired state)
- 💡 Example: a transaction needs to add $10 to your bank account, which has $20. Two possible solutions: (a) add $10, or (b) explicitly set your account to $30. (b) is idempotent (retrying it until it works will give expected result). (a) is NOT idempotent → retrying it multiple times can give you more money than you should. Lambda just reruns the function, so (a) will lead to problems often.
- Successful or failed events can be sent destinations (SQS, SNS, EventBridge, other Lambda functions)
Lambda - Event Source Mapping
Diagram: https://github.com/acantril/aws-sa-associate-saac03/blob/main/1600-SERVERLESS_and_APPLICATION_SERVICES/00_LEARNINGAIDS/Lambda-7.png
- 🔧 Lambda continuously polls a queue or stream, generates source/data batches that are processed by Lambda functions
- Some queues or streams (e.g. telemetry) don't generate events → polling is required
- Supported producers include SQS queues, Kinesis streams, DynamoDB streams, Kafka streams from Amazon Managed Streaming for Apache Kafka (MSK)…
- Source batches broken down as required based on batch size, then sent to Lambda functions as event batches
- 💡 Since Lambda has 15min timeout, a function should be able to finish processing all data in its batch in that time → adjust batch size accordingly