Serverless image labelling with Rekognition and CDK
S3 uploads trigger a Lambda that labels images with Rekognition and stores labels in DynamoDB.
What it does
An AWS CDK stack in JavaScript defines an S3 image bucket, an IAM role, a DynamoDB table keyed on image name and a Python Lambda function. Each object-created event in the bucket invokes the function, which calls Rekognition detect_labels and writes up to ten labels above 60 percent confidence as attributes on the image's DynamoDB item. The stack is bootstrapped, synthesised to CloudFormation, deployed and destroyed with the CDK CLI.
What was built
- 01
CDK stack creates the S3 bucket, DynamoDB table, IAM role and Python 3.9 Lambda with table and bucket environment variables.
- 02
S3EventSource wires OBJECT_CREATED events on the bucket to the Lambda function.
- 03
Lambda calls Rekognition detect_labels with MaxLabels 10 and MinConfidence 60.
- 04
Each detected label is written to the image's DynamoDB item as object1, object2 and so on.
- 05
cdk bootstrap, synth, deploy and destroy manage the full stack lifecycle.
The pipeline, layer by layer
Five layers, one pipeline.
L1Infrastructure
- AWS CDK stack
- CloudFormation template
L2Trigger
- S3 image upload
- Object-created event
L3Compute
- Python Lambda handler
L4AI service
- Rekognition detect_labels
L5Storage
- DynamoDB labels table