Reputation: 1091
The steps my app needs to take are:
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
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:
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:
You should design it as an asynchronous process (not waiting) rather than a synchronous process (waiting for a result).
Upvotes: 2
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