How to use AWS Lambda Triggers with SQS
Tags: aws, community, tutorial, python
Published: 2022-12-08 13:21:33

Amazon Web Services (AWS) Simple Queue Service (SQS) is a cloud-based message queuing service that enables applications to communicate and exchange messages in a scalable and reliable manner. One of the key features of SQS is the ability to trigger serverless functions, such as AWS Lambda, when certain events occur within the queue.

In this article, we will explore how to use SQS Lambda triggers to automate and streamline message processing within a python application. 

## Getting Started
To get started, you will need to login with your IAM user credentials and create an SQS queue in the AWS Management Console.


![Creating a message queue on aws management console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf2hvurc8svw5r071jnt.png)

Next, you create a lambda function that will act as a trigger on the queue and process messages in the queue. In our own case, this lambda function will save transaction details into the database coming from the queue. Below is a code snippet of what the lambda function updating a mongodb database could look like:

```python
# lambda_function.py
from os import getenv
from dotenv import load_dotenv
from pymongo import MongoClient

load_dotenv()

def lambda_handler(event, context):
    url = getenv("MONGO_URL")
    client = MongoClient(url)
    db = client["psql_dump"]
    print(db)
    for record in event["Records"]:
        payload = record["body"]
        db.psql_dump.insert_one(payload)

```

Once these are set up, you can enable Lambda triggers on the SQS queue by selecting the "Triggers" tab and clicking on "Add trigger".


![lambda triggers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvs91x6io7qbmxl67585.png)

Next, select the Lambda function that you want to trigger and specify the events that should trigger the function. For example, you can choose to trigger the function when a new message arrives in the queue, or when a message is deleted or changed in any way.


![selecting the lambda function from the triggers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfiwlldo69jjifhwpa7x.png)


When a triggering event occurs, the SQS queue will automatically invoke the Lambda function and pass the relevant message data as input. The Lambda function can then process the message and perform any desired actions, such as sending an email or updating a database.

One of the key advantages of using SQS Lambda triggers is the ability to scale and process messages in a highly efficient and cost-effective manner. Since the Lambda function is only triggered when needed, it can automatically scale up or down based on the workload, without any need for manual intervention.

Additionally, SQS Lambda triggers allow you to decouple and isolate different components of your application, making it easier to manage and maintain. This can help improve the overall reliability and performance of your system, as well as reduce the risk of downtime or errors.

## Conclusion 
Overall, using SQS Lambda triggers can provide a powerful and flexible way to automate and streamline message processing within your application. By leveraging the scalability and reliability of AWS, you can quickly and easily integrate SQS into your existing system, and gain the benefits of cloud-based messaging and serverless computing.


## References
[AWS Lambda docs](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)