|
| 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 |
0 commit comments