Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Explain events that trigger the AWS Lambda

Question

Name the events that trigger the AWS Lambda function from block diagram
Why Aws lambda is called event driven computing and serverless architecture
Give some example when aws lambda function is triggered
How to add new object in s3 bucket
how to list object s3 bucket

The block diagram that explains the working of AWS Lambda in five easy steps is shown below −

Image description

Step 1 − Upload AWS lambda code in any of languages AWS lambda supports, that is NodeJS, Java, Python, C# and Go.

Step 2 − These are few AWS services on which AWS lambda can be triggered.

Step 3 − AWS Lambda which has the upload code and the event details on which the trigger has occurred. For example, event from Amazon S3, Amazon API Gateway, Dynamo dB, Amazon SNS, Amazon Kinesis, CloudFront, Amazon SES, CloudTrail, mobile app etc.

Step 4 − Executes AWS Lambda Code only when triggered by AWS services under the scenarios such as −

  1. User uploads files in S3 bucket
  2. http get/post endpoint URL is hit
  3. data is added/updated/deleted in dynamo dB tables
  4. push notification
  5. data streams collection
  6. hosting of website
  7. email sending
  8. mobile app, etc.

AWS Lambda functions can be triggered by various events, enabling serverless architectures and event-driven computing. Here are some common events that can trigger AWS Lambda functions:

API Gateway:

Example: A Lambda function can be triggered by API Gateway when an HTTP request is made to a specified endpoint. This is commonly used for building RESTful APIs.
Amazon S3:

Example: An AWS Lambda function can be triggered when an object is created, modified, or deleted in an S3 bucket. For instance, you can resize images automatically when they are uploaded to a specific S3 bucket.
Amazon DynamoDB:

Example: Lambda can be triggered by DynamoDB streams when there are changes to a DynamoDB table. This allows you to respond to changes in real-time.
Amazon SNS (Simple Notification Service):

Example: Lambda can be triggered when a message is published to an SNS topic. This is useful for implementing pub/sub patterns and handling notifications.
CloudWatch Events:

Example: AWS CloudWatch Events can trigger Lambda functions based on scheduled cron-like expressions or in response to various AWS events. For example, you can trigger a Lambda function every hour to perform specific tasks.
list-out-checklist-of-real-time-application-of-corn-expression-to-trigger-aws-lambda-function
CloudWatch Logs:

Example: Lambda can be triggered in response to log events in CloudWatch Logs. For instance, you can process and analyze logs in real-time.
Amazon Kinesis Data Streams:

Example: Lambda functions can be triggered by events in a Kinesis Data Stream, allowing you to process and analyze streaming data.
Alexa Skills Kit:

Example: Lambda can be used to build Alexa skills. When a user interacts with an Alexa-enabled device, the Lambda function is triggered to handle the user's request.
Alexa Smart Home:

Example: Similar to Alexa Skills Kit, Lambda functions can be triggered by Alexa Smart Home events, allowing you to control smart home devices.
IoT (Internet of Things):

Example: Lambda functions can be triggered by AWS IoT events, responding to device messages or changes in the IoT environment.
Step Functions:

Example: AWS Step Functions can trigger Lambda functions as part of a workflow, allowing you to create serverless workflows with multiple step

Entry into a S3 object

To trigger an AWS Lambda function when a new object is added to an S3 bucket, you can use AWS S3 event notifications. Here's a step-by-step example using Django and AWS Lambda:

Step 1: Create an S3 Bucket

  1. Log in to the AWS Management Console.
  2. Navigate to the S3 service.
  3. Create a new S3 bucket where your objects will be stored
    .
    Step 2: Set Up S3 Event Notification

  4. In the S3 bucket settings, go to the "Properties" tab.

  5. Scroll down to the "Event notifications" section.

  6. Click "Create event notification."

  7. Configure the event notification:

  8. Event name: Give it a name.

  9. Events: Choose the event type (e.g., "All object create events").

  10. Send to: Choose "Lambda function" and select your Lambda function from the dropdown.

  11. Prefix and Suffix: Add filters if needed.

  12. Click "Add destination" and choose "Lambda function" again.

  13. Save the configuration
    .
    Step 3: Create the Lambda Function

  14. Log in to the AWS Management Console.

  15. Navigate to the Lambda service.

  16. Click "Create function" and choose "Author from scratch."

  17. Configure the function:

  18. Name: Provide a name for your function.

  19. Runtime: Choose the appropriate runtime (e.g., Python 3.8).

  20. Role: Create a new role with basic Lambda permissions.

  21. Click "Create function
    ."
    Step 4: Add S3 Trigger to Lambda

  22. In the Lambda function configuration, click "Add trigger."

  23. Choose "S3" as the trigger type.

  24. Configure the trigger:

  25. Bucket: Select the S3 bucket you created.

  26. Event type: Choose the relevant event type (e.g., "All object create events").

  27. Prefix and Suffix: Add filters if needed.

  28. Click "Add
    ."
    Step 5: Write Lambda Function Code

import json

def lambda_handler(event, context):
    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Your custom logic here
        print(f"New object added to S3: {key} in bucket {bucket}")

        # Add your additional processing logic
        # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode
import json
import boto3

def lambda_handler(event, context):
    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Your custom logic here
        print(f"New object added to S3: {key} in bucket {bucket}")

        # List objects in the same bucket
        s3_client = boto3.client('s3')
        response = s3_client.list_objects(Bucket=bucket)
        objects = response.get('Contents', [])

        # Print the list of objects
        print("Objects in the bucket:")
        for obj in objects:
            print(f" - {obj['Key']}")

        # Add your additional processing logic
        # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode

output

New object added to S3: your_object_key in bucket your_bucket_name
Objects in the bucket:
 - object1.txt
 - object2.jpg
 - object3/subfolder/file.pdf
 - your_object_key
Enter fullscreen mode Exit fullscreen mode
{
  "s3": {
    "s3SchemaVersion": "1.0",
    "configurationId": "your_configuration_id",
    "bucket": {
      "name": "your_bucket_name",
      "ownerIdentity": "your_owner_identity",
      "arn": "your_bucket_arn"
    },
    "object": {
      "key": "your_object_key",
      "size": "your_object_size",
      "eTag": "your_object_eTag",
      "versionId": "your_object_versionId"
    }
  },
  "objects": [
    "object1.txt",
    "object2.jpg",
    "object3/subfolder/file.pdf",
    "your_object_key"
  ]
}
Enter fullscreen mode Exit fullscreen mode
import json
import boto3

def lambda_handler(event, context):
    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Your custom logic here
        print(f"New object added to S3: {key} in bucket {bucket}")

        # List objects in the same bucket
        s3_client = boto3.client('s3')
        response = s3_client.list_objects(Bucket=bucket)
        objects = response.get('Contents', [])

        # Print the list of objects
        print("Objects in the bucket:")
        for obj in objects:
            print(f" - {obj['Key']}")

        # Insert a new object into the same bucket
        new_object_key = 'new_object.txt'  # Provide a key for the new object
        new_object_content = 'Hello, new object!'  # Provide content for the new object
        s3_client.put_object(Body=new_object_content, Bucket=bucket, Key=new_object_key)

        print(f"New object '{new_object_key}' added to the bucket.")

        # Add your additional processing logic
        # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode

output

New object 'new_object.txt' added to the bucket.
Enter fullscreen mode Exit fullscreen mode
import json
import boto3

def lambda_handler(event, context):
    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Your custom logic here
        print(f"New object added to S3: {key} in bucket {bucket}")

        # List objects in the same bucket before adding the new object
        s3_client = boto3.client('s3')
        response_before = s3_client.list_objects(Bucket=bucket)
        objects_before = response_before.get('Contents', [])

        # Print the list of objects before adding the new object
        print("Objects in the bucket before adding the new object:")
        for obj in objects_before:
            print(f" - {obj['Key']}")

        # Insert a new object into the same bucket
        new_object_key = 'new_object.txt'  # Provide a key for the new object
        new_object_content = 'Hello, new object!'  # Provide content for the new object
        s3_client.put_object(Body=new_object_content, Bucket=bucket, Key=new_object_key)

        print(f"New object '{new_object_key}' added to the bucket.")

        # List objects in the same bucket after adding the new object
        response_after = s3_client.list_objects(Bucket=bucket)
        objects_after = response_after.get('Contents', [])

        # Print the list of objects after adding the new object
        print("Objects in the bucket after adding the new object:")
        for obj in objects_after:
            print(f" - {obj['Key']}")

            # If the current object is the newly added one, display its content
            if obj['Key'] == new_object_key:
                obj_content = s3_client.get_object(Bucket=bucket, Key=new_object_key)['Body'].read()
                print(f"Content of '{new_object_key}': {obj_content.decode('utf-8')}")

        # Format the output similar to the provided example
        formatted_output = [{
            "eventVersion": "2.1",
            "eventSource": "aws:s3",
            "awsRegion": context.invoked_function_arn.split(":")[3],
            "eventTime": record['eventTime'],
            "eventName": record['eventName'],
            "userIdentity": record['userIdentity'],
            "requestParameters": record['requestParameters'],
            "responseElements": record['responseElements'],
            "s3": {
                "s3SchemaVersion": "1.0",
                "configurationId": record['s3']['configurationId'],
                "bucket": {
                    "name": bucket,
                    "ownerIdentity": record['s3']['bucket']['ownerIdentity'],
                    "arn": record['s3']['bucket']['arn']
                },
                "object": {
                    "key": key,
                    "size": record['s3']['object']['size'],
                    "eTag": record['s3']['object']['eTag'],
                    "versionId": record['s3']['object'].get('versionId', '')
                }
            }
        }]

        print("Formatted output:")
        print(json.dumps(formatted_output, indent=2))

        # Add your additional processing logic
        # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode

JSON snippet of the list of S3 objects, you can modify the print statements to include the JSON representation of the list. Here's the updated code:

import json

def lambda_handler(event, context):
    # List to store S3 object information
    s3_objects = []

    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Add the S3 object information to the list
        s3_objects.append({'bucket': bucket, 'key': key})

    # Print the JSON representation of the list of S3 objects
    print("List of S3 objects:")
    print(json.dumps(s3_objects, indent=2))  # indent for pretty printing

    # Add your additional processing logic
    # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode

This modification uses json.dumps to convert the s3_objects list into a JSON-formatted string with an indentation of 2 spaces for better readability. The result will be printed in the Lambda function's logs when the function is executed.

The example you posted appears to be a sample event structure that AWS Lambda receives when an S3 event triggers the Lambda function.

Here's a modified version of your code to generate an output similar to the example:

import json

def lambda_handler(event, context):
    # Print the entire event received by the Lambda function
    print("Received event:")
    print(json.dumps(event, indent=2))  # indent for pretty printing

    # List to store S3 object information
    s3_objects = []

    # Process the event triggered by S3
    for record in event['Records']:
        # Extract relevant information
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        # Add the S3 object information to the list
        s3_objects.append({
            "eventVersion": "2.1",
            "eventSource": "aws:s3",
            "awsRegion": record['awsRegion'],
            "eventTime": record['eventTime'],
            "eventName": record['eventName'],
            "userIdentity": record['userIdentity'],
            "requestParameters": record['requestParameters'],
            "responseElements": record['responseElements'],
            "s3": {
                "s3SchemaVersion": "1.0",
                "configurationId": record['s3']['configurationId'],
                "bucket": {
                    "name": bucket,
                    "ownerIdentity": record['s3']['bucket']['ownerIdentity'],
                    "arn": record['s3']['bucket']['arn']
                },
                "object": {
                    "key": key,
                    "size": record['s3']['object']['size'],
                    "eTag": record['s3']['object']['eTag'],
                    "versionId": record['s3']['object'].get('versionId', '')
                }
            }
        })

    # Print the JSON representation of the list of S3 objects
    print("List of S3 objects:")
    print(json.dumps(s3_objects, indent=2))  # indent for pretty printing

    # Add your additional processing logic
    # For example, call other functions or APIs

    return {
        'statusCode': 200,
        'body': json.dumps('Lambda function executed successfully!')
    }
Enter fullscreen mode Exit fullscreen mode

This code appends each S3 object's information to the s3_objects list in a format similar to the provided example. It also prints the entire received event for debugging purposes. The output will be visible in the CloudWatch Logs when the Lambda function is executed.
Step 6: Deploy and Test

  1. Save the Lambda function code.
  2. Deploy the Lambda function.
  3. Upload an object to the S3 bucket to trigger the Lambda function.
  4. Check the CloudWatch Logs for the Lambda function to view the output . This example demonstrates how to set up an S3 event trigger for an AWS Lambda function using the AWS Management Console. You can adapt the Lambda function code to include your specific processing logic based on the S3 event.

aws lambda that Insertion, updation and deletion of data in Dynamo DB table

To trigger an AWS Lambda function upon Insertion, Updation, and Deletion of data in a DynamoDB table in Django, you can use AWS services such as Amazon API Gateway and AWS Lambda. Below is a step-by-step example:

Set Up AWS Lambda Function:

Create an AWS Lambda function using the AWS Management Console or AWS CLI. Your Lambda function should have the necessary permissions to access DynamoDB. You can set up environment variables for DynamoDB table names, etc.

Create an API Gateway:

Create an API Gateway that will act as a trigger for your Lambda function. Configure the API Gateway to handle the required HTTP methods (POST, PUT, DELETE) for data insertion, updation, and deletion.

Django Configuration:

Install the boto3 library to interact with AWS services and requests library to make HTTP requests. You can install them using:

pip install boto3 requests
Enter fullscreen mode Exit fullscreen mode

Django Models:

Assume you have a Django model for your data. Let's say you have a Book model:

# models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    # other fields...
Enter fullscreen mode Exit fullscreen mode

Django Signals:

Use Django signals to trigger the Lambda function upon Insertion, Updation, and Deletion. Below is a sample code snippet:

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
import requests
import json

@receiver(post_save, sender=Book)
def on_book_save(sender, instance, **kwargs):
    # Trigger Lambda function on data insertion/update
    lambda_url = "YOUR_API_GATEWAY_URL"
    data = {
        "operation": "insert_update",
        "data": {
            "title": instance.title,
            "author": instance.author,
            # other fields...
        }
    }
    requests.post(lambda_url, data=json.dumps(data))

@receiver(pre_delete, sender=Book)
def on_book_delete(sender, instance, **kwargs):
    # Trigger Lambda function on data deletion
    lambda_url = "YOUR_API_GATEWAY_URL"
    data = {
        "operation": "delete",
        "data": {
            "title": instance.title,
            "author": instance.author,
            # other fields...
        }
    }
    requests.post(lambda_url, data=json.dumps(data))
Enter fullscreen mode Exit fullscreen mode

Make sure to replace "YOUR_API_GATEWAY_URL" with the actual URL of your API Gateway.

Signal Registration:

In your Django app, make sure to import and connect these signals. You can do this in the apps.py file or in your init.py file.


# apps.py or __init__.py
default_app_config = 'yourapp.apps.YourAppConfig'
Enter fullscreen mode Exit fullscreen mode

And in apps.py:

# apps.py
from django.apps import AppConfig

class YourAppConfig(AppConfig):
    name = 'yourapp'

    def ready(self):
        import yourapp.signals
Enter fullscreen mode Exit fullscreen mode

Testing:

Test the setup by creating, updating, and deleting records in your Django app. The signals should trigger the Lambda function through the API Gateway.

Make sure to replace placeholders like "YOUR_API_GATEWAY_URL" with your actual API Gateway URL and update the fields accordingly based on your model. Also, ensure that your AWS Lambda function is set up to handle the incoming data correctly.

aws lambda that GET/POST calls to API Gateway

To trigger an AWS Lambda function with GET/POST calls to API Gateway in Django, you can follow these steps. This assumes you have already set up an API Gateway and an AWS Lambda function. If not, please create these resources in the AWS Management Console.

Django Configuration:

Install the requests library to make HTTP requests. You can install it using:

pip install requests
Enter fullscreen mode Exit fullscreen mode

AWS Lambda Function:

Create an AWS Lambda function that handles the GET/POST requests. Your Lambda function code might look like this:

import json

def lambda_handler(event, context):
    # Handle GET request
    if event['httpMethod'] == 'GET':
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda GET!')
        }
    # Handle POST request
    elif event['httpMethod'] == 'POST':
        body = json.loads(event['body'])
        # Your logic to process the POST request data
        response_data = {'message': 'Data received successfully'}
        return {
            'statusCode': 200,
            'body': json.dumps(response_data)
        }
    else:
        return {
            'statusCode': 400,
            'body': json.dumps('Invalid HTTP method')
        }
Enter fullscreen mode Exit fullscreen mode
import json

def lambda_handler(event, context):
    # Handle POST request
    if event['httpMethod'] == 'POST':
        try:
            # Load the JSON data from the request body
            body = json.loads(event['body'])

            # Example: Extract values from the JSON body
            name = body.get('name', '')
            age = body.get('age', 0)

            # Your custom logic to process the POST request data
            # For demonstration purposes, let's just create a response message
            response_data = {
                'message': f'Data received successfully. Name: {name}, Age: {age}'
            }

            # Return a JSON response
            return {
                'statusCode': 200,
                'body': json.dumps(response_data)
            }

        except json.JSONDecodeError:
            # Handle JSON decoding error
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'Invalid JSON format in the request body'})
            }
        except Exception as e:
            # Handle other exceptions
            return {
                'statusCode': 500,
                'body': json.dumps({'error': str(e)})
            }
    else:
        # Return a 400 Bad Request response for other HTTP methods
        return {
            'statusCode': 400,
            'body': json.dumps('Invalid HTTP method')
        }
Enter fullscreen mode Exit fullscreen mode
import json

def lambda_handler(event, context):
    # Handle POST request
    if event['httpMethod'] == 'POST':
        try:
            # Load the JSON data from the request body
            body = json.loads(event['body'])

            # Example: Extract values from the JSON body
            name = body.get('name', '')
            age = body.get('age', 0)

            # Your custom logic to process the POST request data
            # For demonstration purposes, let's just create a response message
            if name and age:
                # Assuming you have valid 'name' and 'age' fields
                response_data = {
                    'message': f'Thank you, {name}! You are {age} years old.'
                }
            else:
                response_data = {
                    'message': 'Invalid or missing data in the request.'
                }

            # Return a JSON response
            return {
                'statusCode': 200,
                'body': json.dumps(response_data)
            }

        except json.JSONDecodeError:
            # Handle JSON decoding error
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'Invalid JSON format in the request body'})
            }
        except Exception as e:
            # Handle other exceptions
            return {
                'statusCode': 500,
                'body': json.dumps({'error': str(e)})
            }
    else:
        # Return a 400 Bad Request response for other HTTP methods
        return {
            'statusCode': 400,
            'body': json.dumps('Invalid HTTP method')
        }
Enter fullscreen mode Exit fullscreen mode
import json
from django.contrib.auth.models import User
from django.http import JsonResponse

def lambda_handler(event, context):
    # Handle POST request
    if event['httpMethod'] == 'POST':
        try:
            # Load the JSON data from the request body
            body = json.loads(event['body'])

            # Example: Extract values from the JSON body
            username = body.get('username', '')
            email = body.get('email', '')
            password = body.get('password', '')

            # Your custom logic to process the POST request data
            if username and email and password:
                # Assuming you have valid 'username', 'email', and 'password' fields
                # Create a new user in the database
                new_user = User.objects.create_user(username=username, email=email, password=password)

                response_data = {
                    'message': f'Thank you, {username}! User registered successfully.',
                    'user_id': new_user.id
                }
            else:
                response_data = {
                    'message': 'Invalid or missing data in the request.'
                }

            # Return a JSON response
            return {
                'statusCode': 200,
                'body': json.dumps(response_data)
            }

        except json.JSONDecodeError:
            # Handle JSON decoding error
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'Invalid JSON format in the request body'})
            }
        except Exception as e:
            # Handle other exceptions
            return {
                'statusCode': 500,
                'body': json.dumps({'error': str(e)})
            }
    else:
        # Return a 400 Bad Request response for other HTTP methods
        return {
            'statusCode': 400,
            'body': json.dumps('Invalid HTTP method')
        }
Enter fullscreen mode Exit fullscreen mode

Ensure that the Lambda function has the necessary IAM permissions to be triggered by API Gateway.

API Gateway Configuration:

Create an API Gateway in the AWS Management Console and set up a resource with a method (GET/POST). Configure the integration to use the Lambda function you created.

Django Integration:

In your Django application, you can use the requests library to make GET/POST requests to your API Gateway endpoint.

# views.py
import requests
from django.http import JsonResponse

def make_get_request_to_api_gateway(request):
    api_gateway_url = 'YOUR_API_GATEWAY_URL'

    response = requests.get(api_gateway_url)

    return JsonResponse({'status': 'success', 'response': response.text})

def make_post_request_to_api_gateway(request):
    api_gateway_url = 'YOUR_API_GATEWAY_URL'
    data = {'key': 'value'}

    response = requests.post(api_gateway_url, json=data)

    return JsonResponse({'status': 'success', 'response': response.text})
Enter fullscreen mode Exit fullscreen mode

Replace 'YOUR_API_GATEWAY_URL' with the actual URL of your API Gateway.

Testing:

Test the setup by calling the make_get_request_to_api_gateway and make_post_request_to_api_gateway views or functions in your Django app. This should trigger the AWS Lambda function through API Gateway.

Make sure to replace placeholders such as 'YOUR_API_GATEWAY_URL' with your actual API Gateway URL. Additionally, ensure that your Lambda function is correctly configured to handle the incoming data and has the necessary IAM permissions to be triggered by API Gateway.

Top comments (0)