Headstart Microservices Architecture using AWS Lambda in your favorite programming language (Node.js or Python or JAVA)

The way we build software has changed stupendously from Monolithic Architecture (MLA) using single programming language to a Microservices Architecture (MSA) where pieces of code developed in multiple different programming language that fits the problem in hand. Microservice has numerous benefits and encourages Single Responsibility Principle (SLP) with quick deployment and easy to change lifecycle without impacting the fatty software.

AWS offers easy way to implement microservices through RESTful APIs wrapped against AWS Lambda functions in a server less backend that can auto scale with built in security via its IAM. AWS offers 12 months free tier which enable you to get hands-on experience with AWS Cloud Services.

To demonstrate the power of AWS API Gateway & Lambda, I quickly dropped my flicker photo search node.js code as lambda function wrapped in AWS API Gateway and exposed it as REST API Endpoint.

CODE

var Client = require('node-rest-client').Client;
var _ = require("underscore");
var json2xls = require('json2xls');
var fs = require('fs');
//API Explorer : https://www.flickr.com/services/api/explore/flickr.photos.search
var flickr_api_endpoint = "https://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&nojsoncallback=1";
var flickerPhotos = [];
exports.handler = function (request, context) {
//1) Resolve input and execute REST calls
try {
var flickr_api_key = request.api_key;
var flickr_search_tags = request.search_tags;
var flickr_search_text = request.search_text;
var flickr_search_limit = request.search_limit;
var flickr_search_currentpage = request.search_currentpage;
flickr_api_endpoint = flickr_api_endpoint + "&api_key=" + flickr_api_key + "&tags=" + flickr_search_tags + "&text=" + flickr_search_text + "&per_page=" + flickr_search_limit + "&page=" + flickr_search_currentpage
console.log(flickr_api_endpoint);
var client = new Client();
client.registerMethod("flickrSearch", flickr_api_endpoint, "GET");
}
catch (e) {
context.fail("Something wrong in the input! please check. [" + e.message + "]");
}
//2) Process REST call response and send it back to the client.
var args = { headers: { "Content-Type": "application/json", "Cache-Control": "no-cache" } };
client.methods.flickrSearch(args,function (res, rawdata) {
try {
console.log("Length = [" + res.photos.photo.length + "]");
_.each(res.photos.photo, function (item, index, list) {
//console.log("http://farm" + item.farm + ".staticflickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_b.jpg");
flickerPhotos[index] = "http://farm" + item.farm + ".staticflickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_b.jpg";
});
context.succeed(flickerPhotos);
}
catch (e) {
context.fail(e.message);
}
}
)
};
view raw flickerphotos.js hosted with ❤ by GitHub

AWS lambda does not support 3rd party npm so make sure to upload all of your 3rd party node_modules. AWS Lambda & API Gateway Getting Started guide will be your good head start.

API END POINT

POST https://www.getpostman.com/collections/9f4773dd73a512b92e7a

REQUEST BODY

{
"api_key":"4a9d265192929f69631c55b9a5bdf9bc",
"search_tags":"Raspberrypi",
"search_text":"Raspberrypi",
"search_limit":10,
"search_currentpage":1
}

POSTMAN SCREENSHOT

image

 

One thought on “Headstart Microservices Architecture using AWS Lambda in your favorite programming language (Node.js or Python or JAVA)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s