Three tips for creating functions in AWS Lambda

Three tips for creating functions in AWS Lambda

 3 min read

 

Metal building and sky

AWS Lambda is a fantastic tool for saving time, energy, and money. With server management off your plate, you can focus on writing great code. Even though you’re charged only per 100ms your code runs, it’s important to work smart. Adhering to best practices will ensure you maximize what Lambda can do for you.

 

1. Work cleanly

 

AWS Architect George Mao discusses Lambda best practices on the official Amazon blog. One element covered is the importance of managing concurrency. Setting a function level concurrent execution limit requires you to be aware of your entire Lambda ecosystem, both upstream and downstream. Keeping your entire process in mind as you write your functions will encourage you to write succinctly and in a way that scales properly. The official AWS Lambda best practices documentation also recommends deleting Lambda functions you’re no longer using; keeping your repository clear of unused functions prevents accidental recycling.

 

2. Watch the clock

 

In a blog post on A Cloud Guru, Mitchell Harris of Fourthcast suggests careful management of your timing. In his article about using Lambda for Alexa skills, Harris details the value of keeping instances “warm,” letting processes finish properly, and restricting timeout length: they all reduce latency and result in faster function deployments. Mao also suggests running functions faster by using memory to your advantage via over-provisioning.

 

Lambda timing diagram from Mitchell Harris on Cloud Guru
Function response time comparison for warm and cold functions (Source: Mitchell Harris, Fourthcast for A Cloud Guru)

 

3. Stay flexible and agile

 

Your code in Lambda should be as flexible and agile as possible, since this minimizes risk in the face of errors and saves time by allowing you to repurpose code for future projects. The importance of using environmental variables, compartmentalizing, and placing repeatable code in Lambda layers are discussed in the official Amazon documents. The smaller your functions and simpler your dependencies, the faster your code will execute and easier it will be to troubleshoot.

 

 

 

 

AWS logo