All Projects → aws-samples → Serverless Hotdog Detector

aws-samples / Serverless Hotdog Detector

Licence: apache-2.0
This example shows you how to build a serverless hotdog detecting application on AWS using Amazon Rekognition, AWS Lambda, and Amazon API Gateway.

Programming Languages

python
139335 projects - #7 most used programming language

Serverless Hotdog Detector

This example shows you how to build a serverless hotdog detecting app on AWS using Amazon API Gateway, AWS Lambda, and Amazon Rekognition.

This example is intended to integrate with Slack as the application frontend but could easily be modified to work with your own web or mobile application frontend.

This repository contains sample code the Lambda function depicted in the diagram below as well as an AWS CloudFormation template for creating the function and related resources.

To see some of the other real-world examples of Rekognition in action check out the Image Recognition and Processing Backend Serverless reference architecture

testing of example gif

Walkthrough of the Architecture

  1. A user posts a message containing the image to perform hotdog detection on in a Slack channel monitored by the chat app.
  2. Slack posts the message to an Amazon API Gateway API for the chat app.
  3. The API triggers an invocation of the chat app Lambda function which performs the following:
  4. Amazon Rekognition’s image recognition and labelling feature scans the image and returns a list of detected labels.
  5. The list of labels is checked for those corresponding to hotdogs to determine if the image contained a hotdog.
  6. A message is posted to the chat channel detailing whether the image was a hotdog or not.

screenshot for instruction

Running the Example

Preparing Slack

First make sure you're logged in to Slack, then follow these instructions to prep your app:

  1. Create an app (Documentation)
  2. From the Basic Information tab under Settings take note of the Verification Token as it will be required later
  3. Navigate to the OAuth & Permissions tab under Features
  4. Under the Permissions Scopes section add the following permission scopes
    • channels:history
    • chat:write:bot
    • files:read
    • files:write:user
  5. Click Save Changes
  6. Click Install App to Team then Authorize then note the OAuth Access Token as it will be required later

Launching the App Backend on AWS

Option 1: Launch from Serverless Application Repository

This app can be launched into any region that supports the underlying services from the Serverless Application Repository using the instructions below:

  1. Navigate to the application details page for the app.
  2. Click Deploy
  3. From the region dropdown in the top right ensure you have the desired region to deploy into selected
  4. Input the appropriate application parameters under Configure application parameters
  5. Scroll to the bottom of the page and click Deploy to deploy the chatbot

Option 2: Launch the CloudFormation Template Manually

If you would like to deploy the template manually, you need a S3 bucket in the target region, and then package the Lambda functions into that S3 bucket by using the aws cloudformation package utility.

Set environment variables for later commands to use:

S3BUCKET=[REPLACE_WITH_YOUR_BUCKET]
REGION=[REPLACE_WITH_YOUR_REGION]
STACKNAME=[REPLACE_WITH_DESIRED_NAME]
VTOKEN=[REPLACE_WITH_VERIFICATION_TOKEN]
ATOKEN=[REPLACE_WITH_OAUTH_ACCESS_TOKEN]

Then go to the cloudformation folder and use the aws cloudformation package utility

cd cloudformation

aws cloudformation package --region $REGION --s3-bucket $S3BUCKET --template hotdog_detector.serverless.yaml --output-template-file hotdog_detector.output.yaml

Last, deploy the stack with the resulting yaml (hotdog_detector.output.yaml) through the CloudFormation Console or command line:

aws cloudformation deploy --region $REGION --template-file hotdog_detector.output.yaml --stack-name $STACKNAME --capabilities CAPABILITY_NAMED_IAM --parameter-overrides VerificationToken=$VTOKEN AccessToken=$ATOKEN

Finalize Slack Event Subscription

  1. Navigate to the created stack in the CloudFormation console and note the value for the RequestURL output from the created stack as it will be required later
  2. Return to the Slack app settings page for the Slack app created earlier
  3. Navigate to the Event Subscriptions tab under Features and enable events
  4. In the Request URL field enter the RequestURL value noted earlier
  5. Click Add Workspace Event and select message.channels
  6. Click Save Changes

Testing the Example

To test the example open your Slack app and navigate to a channel for the team you configured the Slack app on, then try uploading pictures of different foods to see the app in action.

Cleaning Up the Stack Resources

To remove all resources created by this example, do the following:

  1. Delete the CloudFormation stack.
  2. Delete the CloudWatch log groups associated with each Lambda function created by the CloudFormation stack.

CloudFormation Template Resources

The following sections explain all of the resources created by the CloudFormation template provided with this example.

AWS Lambda

  • HotdogDetectorFunction - Lambda function that validates incoming Slack event messages, checks them for images containing explicit content, and orchestrates the removal of images found to contain explicit content from Slack.
  • HotdogDetectorFunctionHotdogDetectorAPIPostPermissionTest - Implicitly created Lambda permission, allows API Gateway Test stage to call Lambda function.
  • HotdogDetectorFunctionHotdogDetectorAPIPostPermissionProd - Implicitly created Lambda permission, allows API Gateway Prod stage to call Lambda function.

AWS IAM

  • HotdogDetectorFunctionRole - Implicitly created IAM Role with policy that allows Lambda function to invoke "rekognition:DetectLabels" and "rekognition:DetectModerationLabels" API calls and write log messages to CloudWatch Logs.

Amazon API Gateway

  • HotdogDetectorAPI: - API for hotdog detector app
  • HotdogDetectorAPIProdStage - Implicitly created production stage for API
  • HotdogDetectorAPIDeploymentXXXXXXXXX - Implicitly created deployment for production stage of API

License

This reference architecture sample is licensed under Apache 2.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].