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
- A user posts a message containing the image to perform hotdog detection on in a Slack channel monitored by the chat app.
- Slack posts the message to an Amazon API Gateway API for the chat app.
- The API triggers an invocation of the chat app Lambda function which performs the following:
- Amazon Rekognition’s image recognition and labelling feature scans the image and returns a list of detected labels.
- The list of labels is checked for those corresponding to hotdogs to determine if the image contained a hotdog.
- A message is posted to the chat channel detailing whether the image was a hotdog or not.
First make sure you're logged in to Slack, then follow these instructions to prep your app:
- Create an app (Documentation)
- From the
Basic Information
tab underSettings
take note of theVerification Token
as it will be required later - Navigate to the
OAuth & Permissions
tab underFeatures
- Under the
Permissions Scopes
section add the following permission scopes- channels:history
- chat:write:bot
- files:read
- files:write:user
- Click
Save Changes
- Click
Install App to Team
thenAuthorize
then note theOAuth Access Token
as it will be required later
This app can be launched into any region that supports the underlying services from the Serverless Application Repository using the instructions below:
- Navigate to the application details page for the app.
- Click
Deploy
- From the region dropdown in the top right ensure you have the desired region to deploy into selected
- Input the appropriate application parameters under
Configure application parameters
- Scroll to the bottom of the page and click
Deploy
to deploy the chatbot
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
- 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 - Return to the Slack app settings page for the Slack app created earlier
- Navigate to the
Event Subscriptions
tab underFeatures
and enable events - In the
Request URL
field enter theRequestURL
value noted earlier - Click
Add Workspace Event
and selectmessage.channels
- Click
Save Changes
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.
To remove all resources created by this example, do the following:
- Delete the CloudFormation stack.
- Delete the CloudWatch log groups associated with each Lambda function created by the CloudFormation stack.
The following sections explain all of the resources created by the CloudFormation template provided with this example.
- 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.
- 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.
- HotdogDetectorAPI: - API for hotdog detector app
- HotdogDetectorAPIProdStage - Implicitly created production stage for API
- HotdogDetectorAPIDeploymentXXXXXXXXX - Implicitly created deployment for production stage of API
This reference architecture sample is licensed under Apache 2.0.