This is a basic introduction to Lambda triggers that uses DynamoDB as an event source example.
We talk a lot about the more advanced level of Lambda triggers in our popular two-part series: Complete Guide to Lambda Triggers.
We’re going back to the basics this time because skipping some steps when learning something new might get you confused. It tends to get annoying, or it can even make you frustrated. Why?
To understand how something works appropriately and later on to know how to apply your knowledge practically without stress, you must master the particular subject in a particular order.
Let me put it this way: you can’t calculate how fast is a subatomic particle moving in space if it’s 300 lightyears away from the closest planet if you don’t know the basic math, like 3.14 times 42, right?
So, back to our topic here, what are AWS Lambda triggers?
DynamoDB is an AWS product just like Lambda, and therefore you’re able to create triggers with ease. Triggers are pieces of code that will automatically respond to any events in DynamoDB Streams.
Triggers allow you to build applications that will then react to any data modification made in DynamoDB tables. By enabling DynamoDB Streams on a table, you will be able to associate an ARN with your Lambda function. Instantly after an item in the table is modified, a new record will appear in the table’s stream. When AWS Lambda detects a new stream record, it will invoke your Lambda function synchronously.
Lambda functions can perform any actions you specify, like sending notifications or a workflow initiation.
An example: suppose you have a mobile gaming app that’s writing on a GameScores table. Each time the TopScore attribute of the GameScores table is updated, a corresponding stream record will be written to the table’s stream. You can then set a Lambda function to post a message on social media sites once the event is triggered.
For Lambda functions to execute, an event must occur (think If, Then). If an event happens, then it will trigger Lambda. Until recently, our knowledge of triggering Lambda functions was based on API Gateway as a trigger, but more AWS resources can trigger your Lambda functions.
Events can be anything happening with the resources within your AWS account. Did somebody write a record to DynamoDB? This can trigger a Lambda function. Did someone upload a file to S3? This can be a Lambda trigger too.
You can even call Lambda functions from other Lambda functions! But this is generally seen as bad practice. If you don’t finish your Lambda before you start the next one, you pay for both running. The first one that is waiting for the second one and the second one. Think about optimizing your Lambdas for performance and cost.
You should always put another service between your Lambda functions and don’t call them directly. SQS, SNS, and Step Functions are good ways to connect your Lambda functions without paying for two invocations at once.
If you ever want to trigger lambdas “manually” then simply create an event using one of the AWS resources.
To trigger a lambda function, you can choose between many different ways. The 3 most common ways are API Gateways, S3, and DynamoDB table streams.
1. API Gateway
API Gateway event is one way to trigger Lambda. These events are considered synchronous events. Simply put, it means that when somebody is calling an API Gateway, it will trigger your Lambda function. It’s a synchronous event because your Lambda function has to respond to the client directly at the end of its invocation. For Lambda to know which kind of event will trigger it, you need to define it in the configuration or, or serverless.yml if you’re using the Serverless Framework.
S3 events occur when someone (or something) modifies the content of an S3 bucket. Altering the content can be achieved by either creating, removing, or updating a file. While you’re defining an event, you’re able to specify what sort of action will trigger the lambda function, whether it’s creating, removing, or updating a file.
S3 events are asynchronous, which means your Lambda function only has to handle the event and doesn’t have to respond with anything. There is no client involved like with API Gateway, so nobody is waiting for your function to send a response. S3 will send an event that triggers your Lambda function, but it won’t check what it did afterward.
3. DynamoDB Table Streams
DynamoDB events will be explained shortly, but first, let’s start with DynamoDB table streams.
A DynamoDB table stream is like a line or a queue through which the data flows. In this particular case, the data is actually the change made to a specific table. This means that when someone updates a record in a specific DynamoDB table, it will instantly publish all of these changes in a stream, and it further implies that the Lambda function will be triggered because there is data in the stream.
This way is a little bit more complicated since we need to connect the Lambda function to a DynamoDB stream. When there is data in the stream, there are two different ways Lambda will get triggered by it.
First, when there is any kind of data in the stream, which means a single change to the database at a certain time, the lambda will be executed only once.
The second way table streams trigger Lambda functions is when there is a batch of events in the stream, all processed together. This way saves the execution time a lot since processing streams are pretty fast.
Like S3 events, table stream events are asynchronous, so you don’t have to send a response from your Lambda function. They differ from S3 events in their second way to trigger Lambda functions, which allows you to batch multiple DynamoDB changes into one Lambda event.
Here is a small list of AWS services that can trigger events for AWS Lambda.
Let’s start with the synchronous event sources. These require your Lambda function to return a response when it’s finished. This is because the service that triggered the event sends the response to a client or uses it to check if the event was handled successfully.
- Elastic Load Balancing (Application Load Balancer)
- Amazon Cognito
- Amazon connect
- Amazon Lex
- Amazon Alexa
- Amazon API Gateway
- Amazon CloudFront ([email protected])
- Amazon Kinesis Data Firehose
- Amazon Simple Storage Service Batch
And now the services that trigger asynchronous events. These are simply fire-and-forget; the services that triggered the events don’t care if your Lambda function handled them correctly. If you need retry logic, you have to implement it yourself with the help of SQS or Step Functions.
- Amazon Simple Storage Service
- Amazon Simple Notification Service
- Amazon Simple Email Service
- AWS CloudFormation
- Amazon CloudWatch Logs
- Amazon CloudWatch Events
- AWS CodeCommit
- AWS Config
- AWS IoT
- AWS IoT Events
- AWS CodePipeline
AWS Lambda triggers are merely actions caused by specific AWS resources; these actions generate events that will further execute Lambda functions that listen to them.
AWS Lambda is an event-based system. Lambda functions are associated with events that are triggered by other AWS resources, like API Gateway, S3, or DynamoDB. Lambda functions will always react to these events.
Once you have finished reading about triggers, you will probably start to wonder about the observability aspect of your serverless app or, to put it better, the lack thereof. You wouldn’t be the first one to think like that, but luckily several services can help you in that regard.