Friday 15 December 2017

Micro-Services with AWS Lambda and API Gateway

This is just a little tutorial I’ve started putting together, I’ve struggled to find much in the way of good resources for Lambda and the latest AWS offering API Gateway so my overall target is to help other develops look at what’s a great step forward for ‘Back-end’ developers.

The real big news about AWS API Gateway is you can marry these two services together to create ‘Micro-Services’ which means having a HTTP endpoint without the creation of servers or the headache of provisioning the requirements for them. The concept is for those who want to execute small ideas where you’re only paying for when your code is in use instead of running a server 24/7 that might be only be used for less than half of that time.








Getting started

If you haven’t tried Amazon Web Service’s Lambda service yet, it’s really good but one of the real pains is testing locally and deploying remotely in that same manner with what we’re used to. A great little tool for simplifying this is grunt-aws-lambda which will give us a work flow for testing code with events. In this guide I’m making the assumption that you have node.js already installed and know a tiny amount of JavaScript to understand what we’re working with.

So lets start with this, first make a folder for your project and then open up a terminal in that folder and run:
npm init

You’ll get asked some questions, the default answers should be fine. There’s nothing particularly important you need to do when generating your package.json file but you can always edit it later if needed.

Then run:
npm install -g grunt-cli
npm install grunt-aws-lambda grunt-pack --save-dev
This will install the grunt command line tool and the packages which will help make running our lambda function locally a lot easier.
Now create the file Gruntfile.js with the contents:
module.exports = function(grunt) {
 grunt.loadNpmTasks(‘grunt-aws-lambda’);
 grunt.initConfig({
  lambda_invoke: {
   default: {
    options: {
    }
   }
  },
 });
};
Create your basic index.js file:
console.log(‘Loading function’);
exports.handler = function(event, context) {
 context.done(null, {“Hello”:”World”}); // SUCCESS with message
};
And create a blank event.json file (for now):
{}

Now if you’ve done things right you can now run the following command and you should get a successful output as shown below.
grunt lambda_invoke
Command Output:
Running “lambda_invoke:default” (lambda_invoke) task
Loading function
Success! Message:
 — — — — — — — — —
[object Object]
Done, without errors.

The Results so far…
So now we’ve created the simplest of Lambda functions for handling API requests and returning a json response. As you can see you won’t be able to see the output of the response from running the test, just simply that an object is returned (we can do more verbose logging later on). The important thing is we now have a good workflow for testing what we develop without constantly playing with AWS and incurring the costs to go with it.


If You learn more vist AWS Lambda


Explore more training courses here Mindmajix technologies


Resources
Grunt AWS Lamba NPM Page
AWS Lambda Docs — Node.js
AWS API Gateway



No comments:

Post a Comment