Skip to content

Commit 024612e

Browse files
authored
Merge pull request #2568 from abilashkumar/rekognition-s3-detectlabels-python
new serverless pattern - rekognition-s3-detectlabels-python
2 parents 67db4e3 + 8699fa3 commit 024612e

File tree

5 files changed

+331
-0
lines changed

5 files changed

+331
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Amazon S3 to Amazon Rekognition through AWS EventBridge
2+
3+
This pattern demonstrates how to create an Amazon S3 bucket that triggers an AWS Lambda function via Amazon EventBridge upon object upload. The Lambda function detects labels in an image using Amazon Rekognition. The Lambda function is built using Python.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/rekognition-s3-detectlabels-python
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
1. Change directory to the pattern directory:
23+
```
24+
cd rekognition-s3-detectlabels-python
25+
```
26+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
27+
```
28+
sam deploy --guided
29+
```
30+
1. During the prompts:
31+
* Enter a stack name
32+
* Enter the desired AWS Region
33+
* Allow SAM CLI to create IAM roles with the required permissions.
34+
35+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
36+
37+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
38+
39+
## How it works
40+
41+
The CloudFormation template creates two Amazon S3 buckets (source and destination) along with an AWS Lambda function (written in Python) and an Amazon EventBridge event. The Lambda function is triggered by EventBridge, which listens for object uploads in the S3 source bucket. The Lambda function makes a `DetectLabels` API call to Amazon Rekognition to detect labels in an image and stores the output in the destination S3 bucket.
42+
43+
## Testing
44+
45+
Upload the file (document/image) to the input S3 `<STACK_NAME>-input-bucket-<AWS_ACCOUNTID>` bucket via the console or use the `PutObject` API call below:
46+
47+
```
48+
aws s3api put-object --bucket <INPUT_BUCKET_NAME> --key <IMAGE_FILE> --body /path/to/your/<IMAGE_FILE>
49+
```
50+
The output of the operation can be downloaded from the output S3 bucket <STACK_NAME>-output-bucket-<AWS_ACCOUNTID>.
51+
52+
Replace the parameters in the above command appropriately.
53+
54+
## Cleanup
55+
56+
1. Delete the stack
57+
```bash
58+
sam delete
59+
```
60+
----
61+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
62+
63+
SPDX-License-Identifier: MIT-0
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"title": "S3 to Rekognition using EventBridge",
3+
"description": "SAM template for S3 trigger to Lambda for detecting labels in an image using Rekognition and EventBridge",
4+
"language": "python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates how to creates two S3 buckets (source and destination) which when uploaded with an object invokes a Lambda function through EventBridge and detects labels in an image using Amazon Rekognition.",
11+
"Once a file is uploaded to an S3 bucket, it is listened by the EventBridge which further invokes the lambda function",
12+
"The lambda function writes the output of the detected labels using Rekognition to the destination S3 bucket."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/rekognition-s3-detectlabels-python",
18+
"templateURL": "serverless-patterns/rekognition-s3-detectlabels-python",
19+
"projectFolder": "rekognition-s3-detectlabels-python",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Detecting Labels using Rekognition",
27+
"link": "https://docs.aws.amazon.com/rekognition/latest/dg/labels-detect-labels-image.html"
28+
},
29+
{
30+
"text": "Amazon Rekognition",
31+
"link": "https://aws.amazon.com/rekognition/"
32+
}
33+
]
34+
},
35+
"deploy": {
36+
"text": [
37+
"sam deploy"
38+
]
39+
},
40+
"testing": {
41+
"text": [
42+
"See the GitHub repo for detailed testing instructions."
43+
]
44+
},
45+
"cleanup": {
46+
"text": [
47+
"Delete the stack: <code>cdk delete</code>."
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Abilashkumar P C",
53+
"image": "https://drive.google.com/file/d/1bxOh_WBw8J_xEqvT-qRezH8WXqSBPI24/view?usp=sharing",
54+
"bio": "Sr. Cloud Support Engineer @ AWS",
55+
"linkedin": "abilashkumar-p-c"
56+
}
57+
]
58+
}
59+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"title": "S3 to Rekognition using EventBridge",
3+
"description": "SAM template for Amazon S3 trigger to AWS Lambda for detecting labels in an image using Amazon Rekognition and Amazon EventBridge",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates how to creates two S3 buckets (source and destination) which when uploaded with an object invokes a Lambda function through EventBridge and detects labels in an image using Amazon Rekognition.",
11+
"Once a file is uploaded to an S3 bucket, it is listened by the EventBridge which further invokes the lambda function",
12+
"The lambda function writes the output of the detected labels using Rekognition to the destination S3 bucket."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/rekognition-s3-detectlabels-python",
18+
"templateURL": "serverless-patterns/rekognition-s3-detectlabels-python",
19+
"projectFolder": "rekognition-s3-detectlabels-python",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Detecting Labels using Rekognition",
27+
"link": "https://docs.aws.amazon.com/rekognition/latest/dg/labels-detect-labels-image.html"
28+
},
29+
{
30+
"text": "Amazon Rekognition",
31+
"link": "https://aws.amazon.com/rekognition/"
32+
}
33+
]
34+
},
35+
"deploy": {
36+
"text": ["sam deploy"]
37+
},
38+
"testing": {
39+
"text": ["See the GitHub repo for detailed testing instructions."]
40+
},
41+
"cleanup": {
42+
"text": ["Delete the stack: <code>cdk delete</code>."]
43+
},
44+
"authors": [
45+
{
46+
"name": "Abilashkumar P C",
47+
"image": "https://drive.google.com/file/d/1bxOh_WBw8J_xEqvT-qRezH8WXqSBPI24/view?usp=sharing",
48+
"bio": "Sr. Cloud Support Engineer @ AWS",
49+
"linkedin": "abilashkumar-p-c"
50+
}
51+
],
52+
"patternArch": {
53+
"icon1": {
54+
"x": 20,
55+
"y": 50,
56+
"service": "s3",
57+
"label": "Amazon S3"
58+
},
59+
"icon2": {
60+
"x": 50,
61+
"y": 50,
62+
"service": "lambda",
63+
"label": "AWS Lambda"
64+
},
65+
"icon3": {
66+
"x": 80,
67+
"y": 50,
68+
"service": "rekognition",
69+
"label": "Amazon Rekognition"
70+
},
71+
"line1": {
72+
"from": "icon1",
73+
"to": "icon2",
74+
"label": ""
75+
},
76+
"line2": {
77+
"from": "icon2",
78+
"to": "icon3",
79+
"label": ""
80+
}
81+
}
82+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import boto3
2+
import json
3+
import os
4+
s3 = boto3.client('s3')
5+
rekognition = boto3.client('rekognition')
6+
7+
def handler(event, context):
8+
# Extract bucket and key from the EventBridge event
9+
detail = event['detail']
10+
bucket = detail['bucket']['name']
11+
key = detail['object']['key']
12+
13+
# Call Rekognition to detect faces
14+
response = rekognition.detect_labels(
15+
Image={
16+
'S3Object': {
17+
'Bucket': bucket,
18+
'Name': key
19+
}
20+
},
21+
MaxLabels=15
22+
)
23+
24+
# Write the Rekognition output to the output bucket
25+
output_key = f"rekognition-output-{key}"
26+
output_key = output_key[:output_key.rfind('.')]
27+
output_key = output_key + ".json"
28+
print(output_key)
29+
30+
labelDetection=response['Labels']
31+
print(response)
32+
33+
s3.put_object(
34+
Bucket=os.environ['OUTPUT_BUCKET'],
35+
Key=output_key,
36+
Body=json.dumps(labelDetection)
37+
)
38+
39+
return {
40+
'statusCode': 200,
41+
'body': json.dumps('Label detection successfully')
42+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: 'SAM template for S3 trigger to Lambda for detecting labels in an image using Rekognition and EventBridge'
4+
5+
Resources:
6+
# Input S3 bucket
7+
InputBucket:
8+
Type: AWS::S3::Bucket
9+
Properties:
10+
BucketName: !Sub '${AWS::StackName}-input-bucket-${AWS::AccountId}'
11+
NotificationConfiguration:
12+
EventBridgeConfiguration:
13+
EventBridgeEnabled: true
14+
15+
# Output S3 bucket
16+
OutputBucket:
17+
Type: AWS::S3::Bucket
18+
Properties:
19+
BucketName: !Sub '${AWS::StackName}-output-bucket-${AWS::AccountId}'
20+
21+
# Lambda function
22+
RekognitionFunction:
23+
Type: AWS::Serverless::Function
24+
Properties:
25+
FunctionName: !Sub '${AWS::StackName}-rekognition-function'
26+
Handler: lambda_function.handler
27+
Runtime: python3.8
28+
Timeout: 30
29+
Environment:
30+
Variables:
31+
OUTPUT_BUCKET: !Ref OutputBucket
32+
Policies:
33+
- S3ReadPolicy:
34+
BucketName: !Ref InputBucket
35+
- S3WritePolicy:
36+
BucketName: !Ref OutputBucket
37+
- Statement:
38+
- Effect: Allow
39+
Action:
40+
- rekognition:DetectLabels
41+
Resource: '*'
42+
CodeUri: src/
43+
44+
45+
# EventBridge Rule
46+
S3ObjectCreatedRule:
47+
Type: AWS::Events::Rule
48+
Properties:
49+
Description: "Rule to capture S3 object created events"
50+
EventPattern:
51+
source:
52+
- aws.s3
53+
detail-type:
54+
- Object Created
55+
detail:
56+
bucket:
57+
name:
58+
- !Ref InputBucket
59+
State: "ENABLED"
60+
Targets:
61+
- Arn: !GetAtt RekognitionFunction.Arn
62+
Id: "RekognitionFunctionTarget"
63+
64+
# Permission for EventBridge to invoke Lambda
65+
RekognitionFunctionPermission:
66+
Type: AWS::Lambda::Permission
67+
Properties:
68+
FunctionName: !Ref RekognitionFunction
69+
Action: "lambda:InvokeFunction"
70+
Principal: "events.amazonaws.com"
71+
SourceArn: !GetAtt S3ObjectCreatedRule.Arn
72+
73+
Outputs:
74+
InputBucketName:
75+
Description: 'Name of the input S3 bucket'
76+
Value: !Ref InputBucket
77+
OutputBucketName:
78+
Description: 'Name of the output S3 bucket'
79+
Value: !Ref OutputBucket
80+
RekognitionFunctionName:
81+
Description: 'Name of the Rekognition Lambda function'
82+
Value: !Ref RekognitionFunction
83+
RekognitionFunctionArn:
84+
Description: 'ARN of the Rekognition Lambda function'
85+
Value: !GetAtt RekognitionFunction.Arn

0 commit comments

Comments
 (0)