Name: lambda-refarch-imagerecognition
Owner: Amazon Web Services - Labs
Owner: AWS Samples
Description: The Image Recognition and Processing Backend reference architecture demonstrates how to use AWS Step Functions to orchestrate a serverless processing workflow using AWS Lambda, Amazon S3, Amazon DynamoDB and Amazon Rekognition.
Created: 2017-01-25 19:26:55.0
Updated: 2018-01-13 18:31:38.0
Pushed: 2018-01-10 16:52:34.0
Homepage: null
Size: 9420
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
The Image Recognition and Processing Backend demonstrates how to use AWS Step Functions to orchestrate a serverless processing workflow using AWS Lambda, Amazon S3, Amazon DynamoDB and Amazon Rekognition. This workflow processes photos uploaded to Amazon S3 and extracts metadata from the image such as geolocation, size/format, time, etc. It then uses image recognition to tag objects in the photo. In parallel, it also produces a thumbnail of the photo.
This repository contains sample code for all the Lambda functions depicted in the diagram below as well as an AWS CloudFormation template for creating the functions and related resources. There is also a test web app that you can run locally to interact with the backend.
PhotoRepo
S3 bucket under the “Incoming/” prefixImageProcStartExecution
Lambda function, which kicks off an execution of the ImageProc
state machine in AWS Step Functions, passing in the S3 bucket and object key as input parameters.ImageProc
state machine has the following sub-steps:NotSupportedImageType
error and end execution.ImageMetadata
DynamoDB tableImageMetadata
DynamoDB tablePhotoRepo
S3 bucketYou can use the test web app to upload images and see the result of the image recognition and processing workflow.
The backend infrastructure can be deployed in US West - Oregon (us-west-2) using the provided CloudFormation template. Click Launch Stack to launch the template in the US West - Oregon (us-west-2) region in your account:
(In the last page of the wizard, make sure to:
If you would like to deploy the template to a different region (must be a region that supports Amazon Rekognition and AWS Step Functions, e.g. US East (N.Virginia) or EU (Ireland), 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.
First, In the terminal, go to the lambda-functions
folder. Then prepare npm dependencies for the following Lambda functions:
ambda-functions
reate-s3-event-trigger-helper && npm install && cd ../thumbnail && npm install && cd ../extract-image-metadata && npm install && cd ..
Set environment variables for later commands to use:
ON=[YOUR_TARGET_REGION]
CKET=[REPLACE_WITH_YOUR_BUCKET]
Then go to the cloudformation
folder and use the aws cloudformation package
utility
./cloudformation
on inject_state_machine_cfn.py -s state-machine.json -c image-processing.serverless.yaml -o image-processing.complete.yaml
cloudformation package --region $REGION --s3-bucket $S3BUCKET --template image-processing.complete.yaml --output-template-file image-processing.output.yaml
Last, deploy the stack with the resulting yaml (image-processing.output.yaml
) through the CloudFormation Console or command line:
cloudformation deploy --region $REGION --template-file image-processing.output.yaml --stack-name photo-sharing-backend --capabilities CAPABILITY_IAM
You can use the test web app to see the backend working in action.
The web app needs references to the resources created from the CloudFormation template above. To do so, follow these steps:
This web app is built using Angular2 and TypeScript, which relies heavily on node and npm.
Verify that you are running at least node v4.x.x
and npm 3.x.x
by running node -v
and npm -v
in a terminal/console window.
Get it now if it's not already installed on your machine.
In a terminal, go to the webapp
folder, then type
install
start
This compiles the application, starts a local server, and opens a browser that loads the test web application.
Pick any username to log in (This is a test app to showcase the backend so it's not using real user authentication. In an actual app, you can use Amazon Cognito to manage user sign-up and login.)
The username will be used in storing ownership metadata of the uploaded images.
Create new or select existing albums to upload images to.
Upload images and see status updates when:
A sample set of extracted image metadata and recognized tags, along with the thumbnail generated in the Step Function execution is displayed for each uploaded image.
Below is the diagram of the state machine being executed every time a new image is uploaded (you can explore this in the Step Functions Console):
To remove all resources created by this example, do the following:
The following sections explain all of the resources created by the CloudFormation template provided with this example.
ImageMetadataDDBTable
ImageMetadataDDBTable
ImageProcStateMachine
every time a new object is uploaded into S3 bucket under “Incoming/” prefixPhotoRepoS3Bucket
This CloudFormation template chose not to create one IAM role for each Lambda function and consolidated them, simply to reduce the number of IAM roles it takes up in your account. When developing your application, you might instead create individual IAM roles for each Lambda function to follow the Least Privilege principle.
ImageProcStateMachine
and the ImageProcStartExecutionFunction
which kicks off the state machine execution. This role provides logging permissions and access to read/write the PhotoRepoS3Bucket
, the ImageMetadataDDBTable
, call the DetectLabels
Amazon Rekognition API and start state machine execution in Step Functions.CreateS3EventTriggerFunction
for creating custom resources in the CloudFormation templateImageProcStateMachine
during execution. It has permission to invoke Lambda functions.DescribeExecutionFunction
. It has permission to DescribeExecution
API in Step Functions.TestClientIdentityPool
Cognito Identity poolThis reference architecture sample is licensed under Apache 2.0.