Harry
Harry

Reputation: 1091

How to trigger the same lambda function with multiple triggers?

The steps my app needs to take are:

  1. In the frontend, the user triggers a lambda function with API gateway which sends a file to s3.
  2. When the file arrives in s3, trigger the same lambda function to apply video recognition and send jobId to SNS.
  3. When SNS receives a message, trigger the same lambda function to get the label data and return the data back to the user with the API gateway

All the steps work when I test them individually but, but I don't know how to make the code work together as described above. Should I create multiple lambda functions, use one lambda or try another option.

Note: the label data needs to return back to the user via API

Should be something like this:

rekognition = boto3.client("rekognition")
sns = boto3.client("sns")

def lambda_handler(event, context):
    # should be triggered when s3 recives file after API call
    response = rekognition.start_label_detection(
        Video = {
            "S3Object": {
                "Bucket": BUCKET,
                "Name": KEY
            }
        },
        NotificationChannel = {
            "SNSTopicArn": SNS_TOPIC_ARN,
            "RoleArn": ROLE_ARN
        }
    )

    # should be triggerd when sns message has arrived
    if "Records" in event:
        message = event["Records"][0]["Sns"]["Message"]
        #perform get lables here from jobId...



    # should return labels back to the user
    return {
        "statusCode": 200,
        "body": json.dumps(lables),
        "headers": {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json"

        }
    }

Upvotes: 3

Views: 24068

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 270224

As per Error when trying to read AWS SNS message, you should create three separate Lambda functions. There is no benefit in doing it all from one function. In fact, it would just make things harder.

The steps would be:

  1. In the frontend, the user triggers a lambda function with API gateway which sends a file to s3.
  2. When the file arrives in s3, it will trigger another lambda function to apply video recognition. This call will include an Amazon SNS topic that will receive a notification when the recognition is complete.
  3. When SNS receives a message, it will trigger a third lambda function to get the label data.

The problem is, running Amazon Rekognition Video can take several minutes, depending upon the length of the video. You should not design your system to wait around and respond to the original API call with the result because it is likely to time-out.

Rather, your front-end should:

  1. Upload the video (which triggers the above)
  2. Receive a notification when everything is finished (or poll for completion).

You should design it as an asynchronous process (not waiting) rather than a synchronous process (waiting for a result).

Upvotes: 2

iammehrabalam
iammehrabalam

Reputation: 1335

If you want to add multiple event source triggers to lambda then you must have to identify the event type using the event parameter (first argument in handler) first.

Aws event parser GitHub repo https://github.com/iammehrabalam/awseventparser

AWS Event samples from different source https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html

Upvotes: 2

Related Questions