AWS Lambda is a serverless computing service provided on Amazon Web Services, with the goal of running functions written in programming languages such as NodeJS, Python, Java and many more. Lambda runs your code in the cloud without the need for you to set up machines and environments - it is all fully-managed. And today we are going to give some tips to optimize your Lambda function, so you can get the most out of this amazing service.
-
Follow the programming language guidelines This is quite simple and straightforward: follow the best practices for the programming language you are using to develop your Lambdas. This will help with the maintenance of your code by yourself and even other developers.
-
Select your programming language wisely AWS Lambda allows you to write your code in many programming languages, offering both interpreted and compiled languages. Choosing between those two types is very important for your project, due to the way that Lambda functions run in the cloud.
AWS reuses the Lambda execution context if the next call occurs within a five-minute interval, removing the "cold-start" and making consecutive Lambda calls faster. Due to this factor, you need to select your programming language type according to how often the function is going to be called: interpreted languages are faster when the execution container "cold-starts"; but compiled languages tend to be faster for consecutive calls. Also, provisioned concurrency may help with the "cold-start" issue for compiled languages.
-
Memory provisioning When creating a Lambda function, you need to set the maximum memory that is going to be provisioned to the Lambda function. The cost and run time is proportional to the amount of memory you designate. If you set the minimum amount, Lambda calls will be cheaper and slower. Although, setting a big amount of memory for a function that uses almost no memory at all is not optimal; it is actually a way to throw away your money.
The best way to optimize the Lambda memory provisioning is to analyse how much time it takes to run and how much memory it uses on average. With this data - and having in mind that AWS charges you rounding the execution time to the next 100 ms - spending more on memory to get your Lambda executions 50 ms faster might not be ideal.
Let's take some hypothetical examples:
Memory Time to execute Billed time 128 MB 490 ms 500 ms 192 MB 440 ms 500 ms 256 MB 380 ms 400 ms As you can see, the first and second setups are going to have the same billed time, but the second will be more expensive due to the fact you are spending more on memory. This is the least optimized of the cases. Although, increasing the memory a little more will reduce the execution time in a way the billed time will be reduced, making the function faster and cost-optimized. Read more about it in the Lambda pricing page on AWS.
-
Use environment variables for AWS resources Using environment variables is highly recommended when dealing with other AWS resources. It also makes it easier to deploy multiple stacks of applications using the same code, whilst otherwise you would hard-code those values in your functions. If you want to read a little bit more about it, check out the Lambda environment variables documentation.
-
Optimize your dependencies The size and quantity of dependencies affect the time your Lambda takes to start up, so it is very important to wisely choose which dependencies you are going to use. Always prefer smaller dependencies, so the package may be smaller and therefore the Lambda will run faster. Also, removing unused dependencies is crucial, as they are an unnecessary weight.
-
Avoid using recursive code Recursive code means functions that call themselves repeatedly until they meet some criteria that makes them end the loop. Using that kind of function may result in big spendings, due to the factor that the criteria could fail and the function could endlessly call itself.
Those are our six tips to optimize your Lambda function, and if you want to get a deeper look into AWS Lambda, open the Best Practices page on the Lambda documentation. We hope those tips can help you get the best out of your Lambda functions!


