Proactivity

Getting started with aws lambda

Source

IaaS, PaaS, SaaS and now FaaS ?

A Function would probably be the smallest possible unit of execution in any functional natured programming language. A stateless function has existed as a stateless web service for quite some time, and mapping functional logic to REST API endpoints also achieves a similar goal as Web Services. Eventually a URL is mapped to a piece of logic. A FaaS stands for Function as a Service which can be thought of as the new fad. My opinion though is that it’s here to stay. With AWS lambda jumping to the fore and lots of technical setups and solutions already jumping on the bandwagon, newer players like webtasks by Auth0 the offering seems like one that has it’s takers. In simple terms - A FaaS refers to a function deployed as a service (which has features applicable to cloud like elasticity, security, auto-scaling etc.) which is bound to a callable URL and which is stateless in nature. so multiple calls with same inputs result in same behaviour from the logic that runs.

Why it makes sense ?

Not all portions of codebase are equallyt important or equally prominently used. SOme are more critical and everyone travells through them example - authentication and authorization. There are other portions like Advanced Search which are available but not used in each workflow. Obviously it makes sense to have higher availability and security for the more important bits. And if you associate with finances if noone’s using anything, Does it make sense to be having just the server running ? No ! Right ? That’s what FaaS enables to achieve.

AWS Lambda

Any offering from AWS asks for a serious look. This one is my favourite (after EC2).

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration.

That was how AWS describes Lambda

In case you need a good framework for FaaS framework like AWS lambdas, refer to this post - _serverless framework getting started & example_

Beginning your AWS Setup

The best place to head is AWS Documentation. Since It’s an entire topic in itself - I’d not mistake trying to cover that here

  • setup AWS IAM role
    – make sure you select Lambda service access for this role
    – create a new ~/.aws/credentials file (if one is not already present)
    – Put in you access key and security key in there (like below)

    1
    2
    3
    4
    5
    6
    [default]
    aws_access_key_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
    aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

– Copy the path starting with arn:// (role url) for your newly created role into your lambdaenv.json file in the root directory of the example project. It will look like below.

1
2
3
4
5
6
7
{
"Role" : "arn:aws:iam::XXXXXXXXXX:role/XXXXXXX",
"Region" : "eu-west-1"
}
  • Now the example project has the lambda function logic contained within src/index.js file. Feel free to change it once it is seen working
  • What is now needed is to push the code sitting on your system onto AWS cloud (to be available to be executed as desired, on demand). gulp deploy does that. The gulpfile.js is the gulp file which has a lot of tasks. the deploy one meant for this purpose only(sending code over to AWS lambda infrastructure)
  • Running gulp deploy has done what you wanted. To make sure go to your AWS dev console - You’ll see the function you uploaded available to you. You could infact have created it right from this place also :-). Now you have the choice. Being a developer the way explained is recommended and preferred by/for me.
    AWS Lambda Screen Listing Functions Created

  • Go ahead run the function against test data you can change from this screen. Now your logic is in cloud (Lambda). You can now call this from any mobile app or hook it up with any webhook or any event to be called. Lambda does not care.

In the next part, We’ll look at integrating this with a Riot App runnable on your mobile device or web.

The Code I use to explain the setup

The code I’m using is at github
Feel free to extend the chain

Any Limitations ?

Well, AWS Lambda is pretty flexible and allows you to explore any code capable logic uploaded and running on AWS. However, it expects the functions to be under a specific size

AWS Lambda Deployment Limits

  • Lambda function deployment package size (.zip/.jar file) 50 MB
  • Size of code/dependencies that you can zip into a deployment package (uncompressed zip/jar size) 250 MB
    AWS limitations

More References