When less AWS Lambda function code is better
In the complex business landscape, sometimes composing AWS Lambda functions with the mindset of doing one thing at a time will mean writing the input/output transformation. For that, one will need to add function code to the repository, add rules for provisioning it, and ensure it is working.
There are multiple ways of not writing any implementation code for achieving your business goals with the software application. Consider using these ways of decreasing the amount of code written with the traditional programming language or eliminating the need for it.
VTL (Velocity Template Language) for the rescue. If you are already using API Gateway for standard REST API, consider applying request/response mapping of the triggering event. It will help to strip the payload even before starting lambda. If there is a GraphQL with the AppSync - do the VTL resolvers with the data sources like DynamoDB (for NoSQL) or HTTP to call anything you want with no code.
Let's consider the situation where the user will need to get an item from DynamoDB. There is a choice to write the AWS Lambda function (on the left) or do it via VTL (on the right). Even the fastest runtime of Lambda will take some time to initialize. The template version will dramatically decrease your sending request and response latency. It is a small change that will improve the application's usability in just nine lines of request transformation.
Do not hesitate to subscribe to the newsletter or contact me concerning serverless-related questions. Follow me on Twitter for content like this below.
Igor Soroka I was curious about this for simpler tasks like fetching an item from DynamoDB and tested both scenarios with a Lambda 256MB runtime. Excluding Lambda cold start, surprisingly, performance was slighly in favor of Lambda solution. I think, under the hood, API Gateway does sth similar to what you do with Lambda. So, I think it has to be used with caution with the systems that has high load and performance concerns, as you have no control with API Gateway VTL. I'm not a big fan of writing VTL for an API for the following reasons: - It is a new language to be introduced in your development landscape. - Most likely, you end up with hybrid Lambda/VTL for complex business logics. - Unit/component testing your business logic is not possible. - You miss the possibility of logging. - There is not performance gain as one would expect. - At first look there is a cost gain. However, one can save more by switching to HTTP API from REST API. In AppSync case, there is indeed some cost gain as there is no alternative. I'd consider VTL in case: - Implementing a simple CRUD API for a low utilized environment which suffers from Lambda cold starts. - For an endpoint that is highly used and this is the only way to save some costs.