AWS Lambda Functions Best Practices

Let’s write Lambda functions with quality and style

Thilina Ashen Gamage
Platform Engineer

--

Photo by Anthony Riera on Unsplash

This blog post summarizes the guidelines and best practices for writing high-quality Lambda functions. These pieces of advice are highly inspired by the AWS official docs, personal experiences as well as the suggestions of the experts in the community too. Hope you will find these guidelines useful in your journey as well.

Unit-Testable Functions

  • Make the logic inside Lambda services unit-testable. This can be achieved by separating the core logic function from the Lambda handler as below. Ideally, a Lambda handler must be thin and act as a proxy or router to a Lambda-agnostic core logic function.
import {LambdaUtils} from "common-layer";// Lambda handler
exports.handler = function(event, context, callback) {
let appContext = LambdaUtils.getAppContext(event, context);
var result = executeTransaction(appContext);
callback(null, LambdaUtils.wrapResponse(result));
}
// Core logic function
function executeTransaction(appContext) {
// logic goes here
// 1. validate function input
// 2. run business logic
// 3. return response
}
  • Avoid core logic functions from consuming and producing data through…

--

--

Thilina Ashen Gamage
Platform Engineer

Advocate of Cloud, Microservices, & Clean Code | 1.5M+ Reach | For more exciting content, stay in touch: https://medium.com/@ThilinaAshenGamage