What are AWS Lambda triggers?
Tags: aws, devops, serverless, cloud
Published: 2021-04-28 14:05:26

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**. If you want to learn more, read [part one](https://dashbird.io/blog/complete-guide-lambda-triggers-design-patterns-part-1/) and [part two](https://dashbird.io/blog/complete-guide-lambda-triggers-design-patterns-part-2/).

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?

[![AWS Lambda monitoring](https://mk0dashbirdioprthk8x.kinstacdn.com/wp-content/uploads/2020/07/lambda.png)](https://dashbird.io/aws-lambda-monitoring/)

Lambda Triggers Explained with DynamoDB Integration
---------------------------------------------------

[DynamoDB is an AWS product](https://aws.amazon.com/dynamodb/) 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.

Event-Driven Lambda And How To Trigger It
-----------------------------------------

**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](https://dashbird.io/knowledge-base/api-gateway/what-is-aws-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**.

### Lambda Triggering Another Lambda -- A Bad Decision?

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](https://dashbird.io/blog/optimizing-aws-lambda-for-production/) 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](https://dashbird.io/blog/ultimate-guide-aws-step-functions/) are good ways to connect your Lambda functions without paying for two invocations at once.

### How to Trigger a Lambda Manually?

If you ever want to trigger lambdas "manually" then simply create an event using one of the AWS resources.

### How to Trigger a Lambda Function? 3 Common Ways To Trigger Lambda

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.

#### 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

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**.

#### 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.

Bonus: List of Lambda Event Sources
-----------------------------------

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 (Lambda@Edge)
-   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

Conclusion
----------

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.

[Dashbird can help you monitor your AWS Lambdas](https://dashbird.io/aws-lambda-monitoring/) with an all in one easy-to-understand dashboard. Set up alerts, get insights on what to improve, and keep track of costs without adding any strain to your application. You can [sign up](https://dashbird.io/register/) right now to start monitoring your serverless app for free! No code changes and no credit card required.


* * * * *

Further reading:

[The Complete AWS Lambda Handbook for Beginners (part 1/3)](https://dashbird.io/blog/complete-aws-lambda-handbook-beginners-part-1/)

[Using Lambda Layers for Better Serverless Architecture](https://dashbird.io/blog/using-lambda-layers-for-better-serverless-architecture/)

[Monitoring vs Observability: can you tell the difference?](https://dashbird.io/blog/monitoring-vs-observability/)

[AWS Lambda Error Handling](https://dashbird.io/blog/aws-lambda-error-handling-step-functions/)

[Why AWS Console isn't Always the Best for Serverless Debugging?](https://dashbird.io/blog/aws-console-serverless-debugging/)