Serverless Backend with AWS Lambda & API Gateway · Lezione

Trigger Lambda con SQS/SNS

Configuri le funzioni Lambda affinché vengano attivate da code SQS e topic SNS, abilitando potenti flussi di lavoro basati sugli eventi.

Lezione 3 di 412 passaggi

Trigger Lambda con SQS/SNS è una lezione Serverless Backend with AWS Lambda & API Gateway gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Serverless Backend with AWS Lambda & API Gateway, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Event-Driven Lambda Triggers

Lambda functions are at the heart of serverless, but how do they know when to run? They respond to events!

An event is anything that happens in your AWS environment, like a file upload to S3, a new item in DynamoDB, or in this lesson, a message arriving in SQS or SNS.

Using SQS and SNS to trigger Lambda enables powerful event-driven architectures. This means services communicate through events, making systems more flexible and scalable.

SQS Triggers for Lambda

Amazon SQS (Simple Queue Service) is a managed message queue service. When you configure an SQS queue as a Lambda trigger, Lambda polls the queue for messages.

Here's how it works:

  • Lambda continuously checks the SQS queue.
  • When messages are available, Lambda retrieves a batch of them.
  • It then invokes your function for each batch or for individual messages, depending on configuration.
  • After successful processing, Lambda deletes the messages from the queue.

Setting Up SQS Trigger

To connect an SQS queue to a Lambda function, you typically do it through the AWS Management Console, AWS CLI, or Infrastructure as Code (like AWS SAM or CloudFormation).

Key configurations include:

  • Batch size: How many messages Lambda fetches at once (e.g., 1 to 10).
  • Batch window: How long Lambda waits to gather a batch (up to 5 minutes).
  • Polling frequency: Lambda manages this automatically.

This setup allows your function to process messages asynchronously, decoupling the message producer from the consumer.

Processing SQS Messages

Your Lambda function receives SQS messages in an event object. This object contains a Records array, where each item is an SQS message.

Let's see a simple Python example that processes messages from an SQS queue. Imagine these messages contain user IDs for processing.

import json

def lambda_handler(event, context):
    print("Received SQS event:")
    for record in event['Records']:
        message_body = record['body']
        print(f"Processing message: {message_body}")
        
        # Example: parse JSON body if expected
        try:
            data = json.loads(message_body)
            user_id = data.get('userId')
            print(f"Extracted userId: {user_id}")
            # Add your business logic here
        except json.JSONDecodeError:
            print(f"Message body is not valid JSON: {message_body}")
        
    return {
        'statusCode': 200,
        'body': json.dumps('Messages processed successfully!')
    }

# --- For local testing (simulated event) ---
if __name__ == '__main__':
    # Simulate an SQS event
    simulated_sqs_event = {
        "Records": [
            {
                "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
                "receiptHandle": "AQEBwJnKyrgtLvC5...",
                "body": '{"userId": "user123", "action": "signup"}',
                "attributes": {
                    "ApproximateReceiveCount": "1",
                    "SentTimestamp": "1523232000000",
                    "SenderId": "AIDAIY234234234234234",
                    "ApproximateFirstReceiveTimestamp": "1523232000001"
                },
                "messageAttributes": {},
                "md5OfBody": "098f6bcd4621d373cade4e832627b4f6",
                "eventSource": "aws:sqs",
                "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:my-queue",
                "awsRegion": "us-east-1"
            },
            {
                "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb79",
                "receiptHandle": "AQEBwJnKyrgtLvC5...",
                "body": '{"userId": "user456", "action": "login"}',
                "attributes": {
                    "ApproximateReceiveCount": "1",
                    "SentTimestamp": "1523232000000",
                    "SenderId": "AIDAIY234234234234234",
                    "ApproximateFirstReceiveTimestamp": "1523232000001"
                },
                "messageAttributes": {},
                "md5OfBody": "098f6bcd4621d373cade4e832627b4f6",
                "eventSource": "aws:sqs",
                "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:my-queue",
                "awsRegion": "us-east-1"
            }
        ]
    }
    lambda_handler(simulated_sqs_event, None)

SQS Event Details

Let's break down the key parts of an SQS event passed to your Lambda function:

  • event['Records']: A list of SQS messages. Your function will iterate through this.
  • record['body']: The actual message content, usually a JSON string that you'll need to parse.
  • record['messageId']: A unique ID for the message. Useful for logging and idempotency.
  • record['attributes']: Contains metadata like SentTimestamp.
  • record['eventSource']: Always "aws:sqs" for SQS triggers.

Always assume body is a string and parse it if you expect JSON.

SNS Triggers for Lambda

Amazon SNS (Simple Notification Service) is a fully managed pub/sub messaging service. Unlike SQS, SNS uses a push model to deliver notifications.

When an SNS topic receives a message, it immediately pushes that message to all its subscribed endpoints, which can include Lambda functions.

This is perfect for:

  • Fan-out scenarios (one message triggers multiple actions).
  • Broadcasting alerts or notifications.
  • Decoupling publishers from multiple subscribers.

Setting Up SNS Trigger

Connecting an SNS topic to a Lambda function is straightforward. You subscribe your Lambda function to the SNS topic.

Steps:

  1. Create an SNS topic.
  2. Create your Lambda function.
  3. Add an SNS trigger to your Lambda function, selecting the topic.
  4. Ensure your Lambda's execution role has permission to be invoked by SNS.

Once configured, any message published to the SNS topic will automatically invoke your Lambda function.

Processing SNS Notifications

When triggered by SNS, your Lambda function receives an event object that contains details about the notification. The most important part is the Message field within the Sns object.

Here's a Python example that logs an SNS notification.

import json

def lambda_handler(event, context):
    print("Received SNS event:")
    for record in event['Records']:
        sns_message = record['Sns']
        message_id = sns_message['MessageId']
        topic_arn = sns_message['TopicArn']
        subject = sns_message.get('Subject', 'No Subject')
        message_body = sns_message['Message']
        timestamp = sns_message['Timestamp']
        
        print(f"Message ID: {message_id}")
        print(f"Topic ARN: {topic_arn}")
        print(f"Subject: {subject}")
        print(f"Message: {message_body}")
        print(f"Timestamp: {timestamp}")
        
        # Add your business logic here, e.g., send an email, update a database
        
    return {
        'statusCode': 200,
        'body': json.dumps('SNS notification processed!')
    }

# --- For local testing (simulated event) ---
if __name__ == '__main__':
    # Simulate an SNS event
    simulated_sns_event = {
        "Records": [
            {
                "EventSource": "aws:sns",
                "EventVersion": "1.0",
                "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:my-topic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                "Sns": {
                    "Type": "Notification",
                    "MessageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                    "TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic",
                    "Subject": "New Order Confirmation",
                    "Message": "{\"orderId\": \"ORD-789\", \"customer\": \"Jane Doe\"}",
                    "Timestamp": "2023-10-27T10:00:00.000Z",
                    "SignatureVersion": "1",
                    "Signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    "SigningCertUrl": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pem",
                    "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:my-topic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                    "MessageAttributes": {
                        "EventType": {"Type": "String", "Value": "OrderCreated"}
                    }
                }
            }
        ]
    }
    lambda_handler(simulated_sns_event, None)

SNS Event Details

Similar to SQS, the SNS event also comes with a Records array, but the structure inside is different:

  • event['Records']: A list, typically containing one SNS notification per invocation.
  • record['Sns']: This object holds the core notification details.
  • record['Sns']['MessageId']: Unique ID for the SNS message.
  • record['Sns']['TopicArn']: The ARN of the SNS topic that published the message.
  • record['Sns']['Subject']: The subject of the notification (optional).
  • record['Sns']['Message']: The actual content of the notification, often a JSON string.

Remember to parse Message if you expect structured data.

Trigger Best Practices

When using SQS and SNS to trigger Lambda, keep these best practices in mind:

  • Idempotency: Design your Lambda to handle duplicate messages without unintended side effects, as messages can sometimes be delivered more than once.
  • Error Handling: Configure a Dead-Letter Queue (DLQ) for your SQS-triggered Lambda. Failed messages will be sent there for later inspection.
  • Batch Processing: For SQS, process messages in batches efficiently. If one message in a batch fails, the entire batch might be retried.
  • Permissions: Ensure the Lambda execution role has permissions to access SQS/SNS and other services it interacts with.

Triggering Lambda Quiz

Test your knowledge on SQS and SNS triggers for Lambda!

Recap & Next Steps

Great job! You've learned how to harness event-driven power by integrating Lambda with SQS and SNS.

  • SQS Triggers: Enable asynchronous, decoupled message processing with Lambda polling queues.
  • SNS Triggers: Facilitate real-time, fan-out notifications by pushing messages to Lambda.
  • You've also seen the different event structures and key best practices for building resilient event-driven serverless applications.

Mastering these integrations is crucial for building scalable and robust serverless backends!

Gratis per iniziare

Impara Serverless Backend with AWS Lambda & API Gateway con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Trigger Lambda con SQS/SNS» è gratuita?

Sì — il testo completo di «Trigger Lambda con SQS/SNS» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Serverless Backend with AWS Lambda & API Gateway, passa a CoddyKit PRO. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.

Cosa imparerò in «Trigger Lambda con SQS/SNS»?

Configuri le funzioni Lambda affinché vengano attivate da code SQS e topic SNS, abilitando potenti flussi di lavoro basati sugli eventi. Eserciti Serverless Backend with AWS Lambda & API Gateway con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Serverless Backend with AWS Lambda & API Gateway?

Non è richiesta alcuna esperienza precedente. Serverless Backend with AWS Lambda & API Gateway su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Trigger Lambda con SQS/SNS»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Serverless Backend with AWS Lambda & API Gateway?

Sì. Ogni lezione Serverless Backend with AWS Lambda & API Gateway include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. SQS per disaccoppiare i servizi
  2. SNS per la messaggistica Pub/Sub
  3. Trigger Lambda con SQS/SNS
  4. Dead-letter queue e gestione dei fallimenti
← Torna a Serverless Backend with AWS Lambda & API Gateway