AWS Image Object Detection Project

Neeraj Somani
5 min readMar 24, 2021
Image taken from AWS CDK official page

I recently attended an AWS training that explained pretty well how to utilize AWS Cloud Development Kit (CDK) to leverage the Infrastructure as Code (IAC) capabilities to next level.

I am completely blown away by the experience and ready to share with you all in a hope to help someone along the way.

If you have written AWS “cloudformation” template ever you can understand the pain of building 1000 lines of text file. That’s where CDK comes as remedy. Even if you have not, this is a great starting point to try AWS.

So, Whats AWS CDK? In a layman terms, I would say just think about it as a “packaged project” in which you are going to write application code in multiple files that talk to each other in a way that they can configure AWS services the way you instruct them and in a language you are most comfortable (java, python, .net, typescript, etc). You can consider you no need to learn “cloudformation” language anymore (just to relieve some pain, cloudformation is not going anywhere). More details about CDK here. Please refer above high level picture for the same.

Enough about AWS CDK. I almost became fan of it and I can write this complete blog on it, but will do that some other time.

What is this project all about? and how can you also implement the same? To understand, Follow it all the way till the end…..

In a nutshell this project is “Image Object Detection on AWS”. Also, I am from DevOps and cloud Engineering background so, I will be explaining you backend infrastructure development part, but you can even utilize the same backend code and connect it with front-end to have a great full stack application experience. Even, AWS provided a lot of code for same.

This project originally created in “typescript” language but due to popularity of python, I created the same in python & going to explain it.

Concepts that you are going to leverage are, “AWS + python + IAC”.

python Backend code: https://github.com/neeraj-somani/TwitchAWS-image-rekognition

typescript Backend code: https://github.com/aws-samples/aws-dev-hour-backend

JS Frontend code: https://github.com/aws-samples/aws-dev-hour-frontend

  • React-JS framework used for frontend
  • AWS Amplify library is used to make this website application

Note: python backend code and typescript backend code is same in-terms of functionality.

Ready to begin the project….

So what are we going to build using AWS CDK: Below is the architecture

Overall application architecture diagram
  1. Three S3 buckets for different purposes
  • image_bucket — where user will upload photos
  • resized_bucket — where each user uploaded image get stored in compressed form to enhance system performance.
  • website_bucket — to host our front-end website

2. API Gateway and Cognito

  • AWS Cognito gives user registration and sign-in functionality
  • User connects to website application using API gateway
  • API Gateway connects to lambda function and gives functionality to upload, delete and fetch images as per user request

3. DynamoDB table

  • AWS rekognition service will provide labels to images and that labels will be stored in DynamoDB table.

4. SQS

  • This was added in the application to make application more robust and scalable.
  • It allows multiple users to use application at the same time. Images wouldn’t get drop because SQS can buffer it until application wouldn’t process them.

5. Two Lambda functions

  • rekognitionLambda — this function fetch user uploaded images and connects to AWS Rekognition service to perform object detection task.
  • servicelambda — this function allows users to fetch those keywords detected in an image and user can even delete an image

Set-up Process to use github repo:

  1. system requirements — aws cli, aws cdk, python, node-js

can be easily done by following this link https://cdkworkshop.com/

2. clone or download backend repository first on your system, make sure you have all AWS account setup properly.

  • I would highly recommend to go through code properly and understand the functionality behind it.
  • Even, its better to comment-out few section of the code and deploy code in parts.

3. run below cdk commands in sequence to launch application

pip install -r requirements.txt
cdk synth
cdk bootstrap
cdk deploy

4. clone or download this front-end github repository

5. Once you have launched your backend application, you need to make changes to “aws-dev-hour-frontend/src/config.json” file for below details. Replace these details with your environment details from backend application

{
"api": {
"invokeUrl": "https://123456789.execute-api.ap-southeast-2.amazonaws.com/prod"
},
"cognito": {
"REGION": "ap-southeast-2",
"USER_POOL_ID": "ap-southeast-2_abcdefgh",
"APP_CLIENT_ID": "abcdefghijklmnopqrstuvwqyz",
"IDENTITY_POOL_ID": "ap-southeast-2:ab12345-a123-b123-c123-123456789"
},
"s3": {
"bucket": "devhourseries1stack-cdkreknimagebucket12345-123456789",
"thumbBucket": "devhourseries1stack-cdkreknimagebucketresized12345-123456789"
}
}

6. run "npm run build" command in frontend folder and copy all files from newly created “build” folder to backend “public” folder. This public folder is used for S3 hosted website.

7. please run these commands again to deploy website related changes:

cdk synth
cdk bootstrap
cdk deploy

8. Go you AWS console and under S3 services section, copy S3 website hosted URL to access your web application.

Hopefully, you should be able to access website and ready to upload images, deleted images and understand the capabilities of AWS CDK.

9. Run below command to clean up environment link

Cdk destroy ## to destroy the application

Disclaimer: The reason I decided to write about this project because there was no article to give a high level overview about the project. This blog will give you an idea what you can achieve from this project. If you are interested you can use AWS official video series to understand it in more details, the video series is going to take around 8+ hours of your time, thats why this blog can save some of your time. It will be very long blog if i needed to write all about it. Hope you can make benefit from combination of these resources.

video series : https://www.twitch.tv/search?term=AWS%20Dev%20Hour%3A%20Building&type=videos

--

--

Neeraj Somani

Data Analytics Engineer, crossing paths in Data Science, Data Engineering and DevOps. Bringing you lots of exciting projects in the form of stories. Enjoy-Love.