AWS Machine Learning Use Case using Serverless…
In this article, we are going to run recognize the picture uploaded in S3 Bucket is of a cat using lambda function and also send the mail if the picture is of CAT otherwise ignore. The Article will be based on Practical so you can follow up with me… so let's Begin the FUN!!
Before we begin let us Know about the services used in this practical —
- Amazon Rekognition: Amazon Rekognition offers pre-trained and customizable computer vision (CV) capabilities to extract information and insights from your images and videos.
- Amazon S3: Amazon Simple Storage Service (Amazon S3) is an object storage service offering industry-leading scalability, data availability, security, and performance.
- Lambda: Lambda is a compute service that lets you run code without provisioning or managing servers. When using Lambda, you are responsible only for your code.
- SNS: Amazon Simple Notification Service (Amazon SNS) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.
- IAM: AWS Identity and Access Management (IAM) provides fine-grained access control across all of AWS. With IAM, you can specify who can access which services and resources, and under which conditions. With IAM policies, you manage permissions to your workforce and systems to ensure least-privilege permissions.
So now as you are familiar with the services let's start our project implementation for a better understanding of these services…
Step 1:- Setup Serverless
Install node.js https://nodejs.org/en/download/
On AWS console Search IAM >> Then click on “Users” >> Add users >> enter “user name” >> Select aws credential type as “Access key — Programmatic access” >> then “Next:Permission” >> click on “Attach existing policies directly” >> tick on “AdministratorAccess” >> click Next and Review.
You will get “Access key” and “Secret key” copy them safe.
now…
On your windows command prompt run the following commands
npn install -g serverless
serverless config credentials — provider aws — key {Access key} — secret { secret key} — overwrite
aws-nodejs-project
serverless
Now look at the Directory structure
Step 2: editing the Serverless.yaml
service: cat-rekognationframeworkVersion: '3'provider:
name: aws
runtime: nodejs14.xiamRoleStatements:
- Effect: Allow
Action:
- s3:ListBucket
- s3:GetObject
- s3:DeleteObject
Resource: arn:aws:s3:::cat-rekognation/*
- Effect: Allow
Action:
- rekognition:DetectLabels
Resource: "*"functions:
validateImage:
handler: handler.validateImage
events:
- s3:
bucket: cat-rekognation
event: s3:ObjectCreated:*
destinations:
onSuccess: arn:aws:sns:us-east-1:247513377090:success
Step 3: Edit handler.js
'use strict';const AWS = require('aws-sdk');
const rekognition = new AWS.Rekognition();
const s3 = new AWS.S3();module.exports.validateImage = async (event) => {
const s3Record = event.Records[0].s3;
const bucket = s3Record.bucket.name;
const key = s3Record.object.key;console.log(`A file named ${key} was put in a bucket ${bucket}`);// Detect the labels
return detectLabels(bucket, key).then(labels => {
console.log(labels);
const isACat = checkIsACat(labels);console.log(isACat);
if (!isACat) {
return removeImage(bucket, key).then(() => {
console.log('The image was not a cat and was removed');
return;
});
}
}).catch(error => {
return error;
})};function detectLabels(bucket, key) {
const params = {
Image: {
S3Object: {
Bucket: bucket,
Name: key
}
},
MaxLabels: 5,
MinConfidence: 85
};return rekognition.detectLabels(params).promise().then(data => {
return data.Labels;
}).catch(error => {
console.log(error);
return error;
});
}function checkIsACat(labels) {
return labels
.map(label => {
return label.Name === 'Cat' ? true : false
}).some( val => {
return val === true;
});
}function removeImage(bucket, key) {
const params = {
Bucket: bucket,
Key: key
};return s3.deleteObject(params).promise(); }
Step 4: create SNS
On aws console
- Search >> Simple Notification Service
- Create topic >> “Standard”
- Name >> “success” >> “create topic”
- Click on “Create Subscription”
- select protocol >> Email
- End point >> { your email id } # verify it
Step 5: Its time to deploy our code
let’s see what happens
Output:
Buckets are created and look at the lambda function S3 is added to Trigger and SNS at destination..
It's time to upload the image in S3……
I have uploaded a cat image in S3
Let’s monitor CloudWatch logs and email -