aws lambda in pictures

A lambda function is a cost saver.
You can save
– some virtual machines (they are managed by the IaaS itself)
– some setup (the API interface is the code itself)
– some maintenance cost (the function will live forever and you control the downtime if you need some)

Do you want to build an API that cost you nothing when not called ?
Write one express-like app in javascript this way :

const doCallback = (data, callback) =>
callback(null, {body: data, isBase64Encoded: false, headers: {'Content-Type': 'application/json'}})

const handler = ({pathParameters, queryStringParameters, myParam}, context, callback) =>
pathParameters && pathParameters.myParam
doCallback(myAction(pathParameters.myParam), callback):
queryStringParameters && queryStringParameters.myParam ?
doCallback(myAction(queryStringParameters.myParam), callback):
typeof myParam === 'string' ?
doCallback(myAction(myParam), callback):

module.exports = {handler}

This polymorphic handler is able to read myParam from several different HTTP verbs.

Use pathParameters in case of GET/POST with path variables
Use queryStringParameters in case of GET + optional queryParameters
Use myParam as request body in a POST call.

I have made a simple lambda function which draws a graph according to a list of coords pair
(example with : [0,5],[1,7][2,4][3,4][4,4][5,1])

If you want to start from something rather than from scratch and if you don’t need (yet) the serverless framework, take a look at that repository :

It works standalone, no need to code inside AWS Cloud9, and the build target in the package.json already prepares a zip archive that you can send to the Lambda console.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *