Skip to content

Commit e6a27af

Browse files
authored
Merge pull request #2348 from archiev4/archiev4-feature-apigw-lambda-rekognition
New Serverless Pattern - Apigw-Lambda-Rekognition
2 parents 8eddb13 + b371681 commit e6a27af

File tree

7 files changed

+455
-0
lines changed

7 files changed

+455
-0
lines changed

apigw-lambda-rekognition/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Content safety with Image Moderation using Amazon API Gateway and AWS Lambda
2+
3+
Using this sample pattern, users can securely upload images to an Amazon S3 bucket by requesting a pre-signed URL through Amazon API Gateway. This URL allows secure and temporary access for uploading files directly to S3.
4+
5+
Once an image is uploaded, an S3 event invokes another Lambda function to analyze the content using the DetectModerationLabels API. If the image is identified as inappropriate, a notification is sent via Amazon SNS, ensuring automated content moderation and alerting.
6+
7+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-lambda-rekognition
8+
9+
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.
10+
11+
## Requirements
12+
13+
* [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.
14+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
15+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
16+
* [Terraform](https://learn.hashicorp.cxom/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
17+
18+
## Deployment Instructions
19+
20+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
21+
```
22+
git clone https://github.com/aws-samples/serverless-patterns
23+
```
24+
1. Change directory to the pattern directory:
25+
```
26+
cd apigw-lambda-rekognition
27+
```
28+
1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration:
29+
```
30+
terraform init
31+
```
32+
1. From the command line, apply the configuration in the main.tf file:
33+
```
34+
terraform apply
35+
```
36+
1. During the prompts
37+
#var.prefix
38+
- Enter a value: {enter any prefix to associate with resources}
39+
40+
#var.region
41+
- Enter a value: {enter the region for deployment}
42+
43+
## Testing
44+
45+
1. After deploying the stack, create a Subscriber for your Amazon SNS topic (For ex, your email) and confirm the subscription.
46+
https://docs.aws.amazon.com/sns/latest/dg/sns-create-subscribe-endpoint-to-topic.html
47+
48+
1. Make a POST request to the API using the following cURL command:
49+
50+
curl --location 'https://<api-id>.execute-api.<region>.amazonaws.com/dev/generate-presigned-url' --header 'Content-Type: text/plain' --data '{"object_name": "image.png", "content_type": "image/png"}'
51+
52+
Note: Replace 'api-id' with the generated API ID from Terraform, 'region' with the region where the API is deployed (refer to the Terraform Outputs section) 'object_name' with your desired name for the S3 object and 'content_type' with the content type of the image, for ex, png or jpeg
53+
54+
1. Get the pre-signed URL from the previous step and use the following cURL command to upload the object in S3:
55+
56+
curl -v --location --request PUT '<presigned-url>' --header 'Content-Type: image/png' --data '<path-of-the-object>.png'
57+
58+
Note: Replace 'presigned-url' with pre-signed URL generated in the previous step. 'Content-Type' should match the content type used to generate the pre-signed URL in the previous step. Make sure you are passing the correct path of the object in the --data parameter.
59+
60+
Once this command is run successfully and the object is uploaded, HTTP 200 OK should be seen. You can also check the S3 bucket to see if the object is uploaded correctly.
61+
62+
1. Once the object is uploaded successfully, the "process_s3-event" Lambda function is invoked and if the image is inappropriate, a message is sent to the SNS topic, which is then received by the subscriber.
63+
64+
## Cleanup
65+
66+
1. Delete the SNS Subscription:
67+
Go to SNS > Subsciptions > Select your Subscription and choose Delete
68+
69+
https://docs.aws.amazon.com/sns/latest/dg/sns-delete-subscription-topic.html
70+
71+
1. Delete all the objects from S3:
72+
Go to S3 > Select all objects > choose Delete
73+
74+
https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-objects.html
75+
https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-multiple-objects.html
76+
77+
1. Change directory to the pattern directory:
78+
```
79+
cd serverless-patterns/apigw-lambda-rekognition
80+
```
81+
82+
1. Delete all created resources
83+
```
84+
terraform destroy
85+
```
86+
87+
1. During the prompts:
88+
```
89+
Enter all details as entered during creation.
90+
```
91+
92+
1. Confirm all created resources has been deleted
93+
```
94+
terraform show
95+
```
96+
----
97+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
98+
99+
SPDX-License-Identifier: MIT-0
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"title": "Image Moderation using Amazon API Gateway and AWS Lambda",
3+
"description": "Securely upload images to Amazon S3 and detect inappropriate content with Amazon Rekognition",
4+
"language": "YAML",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Using this sample pattern, users can securely upload images to an Amazon S3 bucket by requesting a pre-signed URL through Amazon API Gateway. This URL allows secure and temporary access for uploading files directly to S3.",
11+
"Once an image is uploaded, an S3 event invokes an AWS Lambda function to analyze the content using the DetectModerationLabels API. If the image is identified as inappropriate, a notification is sent via Amazon SNS, ensuring automated content moderation and alerting."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-rekognition",
17+
"templateURL": "serverless-patterns/apigw-lambda-rekognition",
18+
"projectFolder": "apigw-lambda-rekognition",
19+
"templateFile": "main.tf"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Uploading objects with presigned URLs",
26+
"link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html"
27+
},
28+
{
29+
"text": "Moderating content",
30+
"link": "https://docs.aws.amazon.com/rekognition/latest/dg/moderation.html"
31+
},
32+
{
33+
"text": "DetectModerationLabels",
34+
"link": "https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectModerationLabels.html"
35+
}
36+
]
37+
},
38+
"deploy": {
39+
"text": [
40+
"terraform init",
41+
"terraform apply"
42+
]
43+
},
44+
"testing": {
45+
"text": [
46+
"See the GitHub repo for detailed testing instructions."
47+
]
48+
},
49+
"cleanup": {
50+
"text": [
51+
"terraform destroy",
52+
"terraform show"
53+
]
54+
},
55+
"authors": [
56+
{
57+
"name": "Archana V",
58+
"image": "https://media.licdn.com/dms/image/D5603AQF_QwVjCkS_UQ/profile-displayphoto-shrink_200_200/0/1670929520771?e=1724284800&v=beta&t=FFJJko4OO8h1tCFrxMyneTyRPAKmyEmIaDOYOeTaFEk",
59+
"bio": "Cloud Support Engineer at AWS",
60+
"linkedin": "archana-venkat-9b80b7184"
61+
}
62+
],
63+
"patternArch": {
64+
"icon1": {
65+
"x": 10,
66+
"y": 50,
67+
"service": "apigw",
68+
"label": "API Gateway"
69+
},
70+
"icon2": {
71+
"x": 30,
72+
"y": 50,
73+
"service": "s3",
74+
"label": "Amazon S3"
75+
},
76+
"icon4": {
77+
"x": 70,
78+
"y": 50,
79+
"service": "rekognition",
80+
"label": "Amazon Rekognition"
81+
},
82+
"icon5": {
83+
"x": 92,
84+
"y": 50,
85+
"service": "sns",
86+
"label": "Amazon SNS"
87+
},
88+
"icon3": {
89+
"x": 47,
90+
"y": 50,
91+
"service": "lambda",
92+
"label": "AWS Lambda"
93+
},
94+
"line1": {
95+
"from": "icon1",
96+
"to": "icon2"
97+
}
98+
,
99+
"line2": {
100+
"from": "icon2",
101+
"to": "icon3"
102+
}
103+
,
104+
"line3": {
105+
"from": "icon3",
106+
"to": "icon4"
107+
},
108+
"line4": {
109+
"from": "icon4",
110+
"to": "icon5"
111+
}
112+
}
113+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"title": "Content safety with Image Moderation using AWS API Gateway and AWS Lambda",
3+
"description": "Create a Lambda function which will send a notification when an inappropriate image is uploaded in Amazon S3",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project utilizes API Gateway to generate an S3 Pre-signed URL via Lambda function, enabling users to upload images to an S3 bucket. Upon successful upload, an S3 event triggers another Lambda function to analyze the image using DetectModerationLabels API. If the image is deemed inappropriate, a notification is sent through SNS. This ensure secure uploads and automated content moderation. Use cases include user-generated content platforms and e-commerce websites, where secure image uploads and automated content moderation are essential."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-rekognition",
16+
"templateURL": "serverless-patterns/apigw-lambda-rekognition",
17+
"projectFolder": "apigw-lambda-rekognition",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Uploading objects with presigned URLs",
25+
"link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html"
26+
},
27+
{
28+
"text": "Moderating content",
29+
"link": "https://docs.aws.amazon.com/rekognition/latest/dg/moderation.html"
30+
},
31+
{
32+
"text": "DetectModerationLabels",
33+
"link": "https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectModerationLabels.html"
34+
}
35+
]
36+
},
37+
"deploy": {
38+
"text": [
39+
"terraform init",
40+
"terraform apply"
41+
]
42+
},
43+
"testing": {
44+
"text": [
45+
"See the GitHub repo for detailed testing instructions."
46+
]
47+
},
48+
"cleanup": {
49+
"text": [
50+
"terraform destroy",
51+
"terraform show"
52+
]
53+
},
54+
"authors": [
55+
{
56+
"name": "Archana V",
57+
"image": "https://media.licdn.com/dms/image/D5603AQF_QwVjCkS_UQ/profile-displayphoto-shrink_200_200/0/1670929520771?e=1724284800&v=beta&t=FFJJko4OO8h1tCFrxMyneTyRPAKmyEmIaDOYOeTaFEk",
58+
"bio": "Cloud Support Engineer at AWS",
59+
"linkedin": "archana-venkat-9b80b7184"
60+
}
61+
]
62+
}
806 Bytes
Binary file not shown.
44.1 KB
Loading

0 commit comments

Comments
 (0)