Automating Image Analysis with AWS Step Functions, Lambda, SQS, SNS, and Rekognition
Introduction
Hello! I'm Hemanth from the Alliance Department. In this blog, I will walk you through the process of building an automated image analysis workflow using AWS Step Functions, Lambda, SQS, SNS, and Amazon Rekognition. We'll be working with images uploaded to an S3 bucket and using AWS services to identify objects and faces in those images automatically.
AWS
Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking , Database, Management Tools, and Security.
AWS Lambda
A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.
S3
Simple and popular AWS Service for storage. Replicates data by default across multiple facilities. It charges per usage. It is deeply integrated with AWS Services. Buckets are logical storage units. Objects are data added to the bucket. S3 has a storage class on object level which can save money by moving less frequently accessed objects to a colder storage class.
Amazon Rekognition
Amazon Rekognition is an AWS service that makes it easy to add powerful image and video analysis to your applications. It uses machine learning to detect objects, scenes, activities, and faces, and even recognize celebrities in images.
AWS Step functions
AWS Step Functions is a fully managed service that helps orchestrate the execution of microservices and functions in a workflow. It integrates seamlessly with other AWS services like Lambda, S3, and SQS to automate tasks and build robust applications.
SQS
It is a web service that enables message exchange between software components at any volume without message loss or the need for additional services to be accessible. It contains standard building blocks like cost allocation tags and dead-letter queues and offers a general web services API that may be used through any programming language that the AWS SDK supports.
SNS
Amazon SNS is a highly scalable notification service that allows you to send messages to a large number of subscribers via multiple communication channels, such as email and SMS.
Demo
We will begin by creating two SQS queues: one for processing images containing faces and another for images with objects. Navigate to the AWS Management Console and search for SQS.
Click on "Create Queue."
First queue we will be for images of faces, give queue name and leave the rest of the settings as default. Then click "Create Queue."
Repeat the process to create another queue for images of objects, give a queue name and leave the rest of the settings as default. Then click "Create Queue."
We'll create an SNS topic to send email notifications when a famous personality's image is uploaded to the S3 bucket. Search for "SNS" in the AWS Console and click on "Create Topic."
Select "Standard," give the topic a name, and leave other settings as default.
After creating the topic, click on "Create Subscription" and choose "Email" as the protocol. Enter your email address and confirm the subscription.
Subscription Confirmation email.
Next, create an S3 bucket where images will be uploaded. In the AWS Console, search for "S3" and click on "Create Bucket."
Give a bucket name, keep others as default and click on create bucket.
Now, let's create the workflow to orchestrate the processing of images. In the AWS Console, search for "Step Functions" and click on "Create State Machine."
Choose the "Blank" template and then click "Select."
Select "Code".
Copy the following Step Functions code.
If you go to design you should see the following workflow design.
Now click on config , give a state-machine name, select express as type, let the step function create the role, click on confirm after checking the roles and hit on confirm.
In the state machine, copy the arn.
Now, let's create the Lambda function that will process the images and trigger the Step Function workflow. In the AWS Console, search for "Lambda" and click on "Create Function." Name your function choose Python 3.12 as the runtime, and leave other settings as default.
Paste the following code in the Lambda editor and hit "Deploy".
In configuration select environment variables, then click edit.
Enter the below Key and value the ARN of earlier copied stepfunctions workflow arn and hit save.
Now adding S3 as trigger to our Lambda, click on add trigger.
Give a bucket name, toggle on the acknowledgement and click add. This way, every time a file is uploaded to S3, the Lambda function will be triggered, process the information and send it to Step Functions. However, there is an additional step we need to take before testing.
The Lambda function needs permission to call Step Functions, Rekognition, and S3. Also, the Lambda timeout should be increased as we need to process the image to get the results. First for timeout, go to general configuration and click edit and changing the timeout to 20 seconds to ensure enough time for processing large images.
Go to permissions and click on role.
click on attach policies.
Add all the below shown policies.
First when i upload an image of celebrity and i see stepfunction. In the execution you will get outout of below.
Also corresponding email.
Next when i upload a non celebrity.
Now when i upload an object the output is as below.
Conclusion
By combining AWS Step Functions, Lambda, SQS, SNS, and Rekognition, we've created an automated workflow to analyze images uploaded to an S3 bucket. This workflow can be further expanded by integrating more advanced Rekognition features, custom notifications, or more complex image processing tasks.