we write about the things we build and the things we consume
Dragos Pitica

taste some AWS Lambda curry

AWS Lambda is a compute service that lets you run serverless applications. The service executes your code only when needed and this makes it a very economic solution as you only pay for the computing time you use – there is no charge when your code is not running. It also removes all need for server maintenance. Basically, all you need to do is to provide your code in one of the languages that AWS Lambda supports (currently Node.js, Java, and Python).

the paradigm

One of the main characteristics of AWS Lambda is that it follows an Event-Driven Programming paradigm, in which the execution of the program is determined by external events. Nowadays, most desktop applications are event-driven – a click or a key press usually trigger a specific behaviour of the application. Also, database triggers where updating a row in a table can trigger massive changes, but let’s not get bogged down into this. AWS Lambda could be exposed to several external events such as:

  • changes to data in an Amazon S3 bucket or an Amazon DynamoDB table
  • run your code in response to HTTP requests using Amazon API Gateway
  • or even invoke the code by API calls made using AWS SDKs

the function

Once you wrap up your code, you need to create a deployment package for your Lambda function. In Java, you can easily do that with the Maven command `mvn clean package`. Depending on the size of your standalone jar, you can upload it directly to AWS Lambda (<10MB) or import it from an S3 bucket otherwise. I'm not going to show you how to create a Lambda function as AWS Lambda documentation can guide you easily throughout the process.

A very import aspect about AWS Lambda is that it might choose to use the same instance of your function for other subsequent requests, rather than creating a new one every time it is invoked. Therefore, you should write your code in a stateless style in order to avoid race conditions or using wrong data because there is no mechanism for the developer to manage containers.

Once your code is in production, AWS Lambda monitors your functions automatically by providing metrics through Amazon CloudWatch such as the number of invocations, duration and errors. Moreover, in order to help you debug the failures of your function, Lambda logs all requests handled by your function and the logs generated by your code.

the model

Irrespective of the language you use for writing your application, every Lambda function includes some core concepts. The handler is the function that AWS Lambda calls to start execution of your code. This function is called every time the Lambda function is invoked. The first entry point of a Lambda function is any event data which is passed as a first parameter to the handler function and processed by your code. Also, the second parameter passed to the handler function by AWS Lambda is a context object which allows you to interact with Lambda during runtime.

As a first time experience with AWS Lambda, it was pretty confusing as their UI is pretty bad. Also, it was time-consuming because the workflow was cumbersome: change code, pass local tests, re-build the project, re-upload the jar, re-fire an S3 event, navigate to CloudWatch, refresh logs, re-sort the logs by timestamp, find the log that just fired, read and react to your logs, change your code, do it again. However, after you understand the general pattern around it, it’s pretty straightforward to complete your intended task.

If you enjoyed the read, drop us a comment below or share the article, follow us on Twitter or subscribe to our #MetaBeers newsletter. Before you go, grab a PDF of the article, and let us know if it’s time we worked together.

blog comments powered by Disqus
sign up to #metabeers
slideshow