This sample code helps you learn how to build and deploy applications to AWS Lambda using AWS Cloudformation by providing
- A simple Express.js web service to index and search faces by calling AWS Rekoginition service
- A Lambda function triggered by S3 file upload to analyze facial image by calling AWS Rekoginition service
- AWS CLI If you want to use a particular AWS CLI credential profile. Run following command to set it as default profile for this workshop.
Windows
C:\> setx AWS_DEFAULT_PROFILE yourprofile
Mac
$ export AWS_DEFAULT_PROFILE=yourprofile
We will need to create a new face colleciton named skooldio
in Rekognition service. Run a command
$ aws rekognition create-collection --collection-id skooldio
- Fork this repo to your account
- Run
git clone <your git URL>
to clone your repo to local.
- By default, AWS SDK for Javascript will use credentials from your AWS CLI. You can set
AWS_PROFILE
env variable to use a particular profile in your credential.
Windows
C:\> setx AWS_PROFILE yourprofile
Mac
$ export AWS_PROFILE=yourprofile
- Create S3 bucket to store face image. You will need to uncheck "Block all public access" and add following CORS configuation in Permissions tab.
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
- Change default S3 bucket config in
api/app.js
accordingly. - Run following commands to start Express.js server on http://localhost:3001
$ npm install
$ node api/local.js
- Open
client/index.html
to test your API through web client. By default, it will connect to your API on localhost.
- Create a Lambda handler for Express.js by using a lib aws-serverless-express.
- Put the handler in a file
api/lambda.js
- Put the handler in a file
- Modify
template.yml
by adding a Lambda function and S3 resources. We can pass S3 bucket name as an env variable to function. For references, see- https://github.com/awslabs/serverless-application-model
- You will need to give appropriate permissions to your Lambda to interact with other AWS resources by specifying policies. See Lambda Managed Policies and SAM Policy templates. You will at least need
AWSLambdaBasicExecutionRole
so your Lambda can upload logs to CloudWatch.
- Deploy using commands in section below. Once your stack has been created, find your endpoint of API Gateway in the AWS Console to start testing your API.
- Clean up your Rekognition collection before testing since it might contain images in your old bucket.
$ aws rekognition delete-collection --collection-id skooldio
$ aws rekognition create-collection --collection-id skooldio
- Update
API_HOST
inclient/client.js
and useclient/index.html
to test your API.
The js code for S3 event processing is in /s3
- Create a Lambda handler
s3/handler.js
and modifytemplate.yml
based on example
- Modify
template.yml
to include Lambda event config and destinations based on example
First, you will need an S3 bucket for AWS Cloudformation to upload your code for deployment. To create a bucket, run following command with your own unique bucket name.
$ aws s3 mb s3://{your-code-bucket}
Then run following commands to package and deploy your CloudFormation stack. Replace {some-bucket}
and {stack-name}
with proper name.
$ aws cloudformation package \
--template-file template.yml \
--output-template-file template-export.yml \
--s3-bucket {your-code-bucket}
$ aws cloudformation deploy \
--template-file template-export.yml \
--capabilities CAPABILITY_IAM \
--stack-name {stack-name}
The template should also create an S3 bucket for you and pass a bucket name an env variable to your Lambda functions.
After finishing the workshop, you can delete all the resources using following commands. But before deleting CloudFormation stack, you need to empty s3 buckets created by CloudFormation first.
$ aws rekognition delete-collection --collection-id skooldio
$ aws cloudformation delete-stack --stack-name {stack-name}
$ aws s3 rb s3://{your-code-bucket}