AWS Lambda Best Practices

Praveen Sambu
CloudInDetail
Published in
3 min readJan 31, 2021

--

Here I would like to discuss about the best practices I have observed by working on real time with lambda in serverless environment . Nevertheless I found sharing them with you might help in your Serverless development.

Always use the programming best practices

That means if you are using node.js, Follow the best practices recommended by node.js like runtime, JavaScript recommendations.

Keep declarations/Instantons outside the lambda handler

This allows the lambda handler to reuse the objects when container get reused. Incase with your database connections this would be even matter more.

Keep the lambda hander lean

Move the core logic of your lambda function out side the handler. specially when you are writing large code inside lambda function. Move the logic to separate functions and call them from lambda handler when needed. This allows keep code easier to maintain in long run.

Avoid hardcoding, Use Environment variables.

Instead of hardcoding, use Environment variables. If you are writing to s3 bucket. instead of giving bucket name in handler, configure that in env variable and use it from there so that we can change it in run time.

One Function should do One task.

This is kind of micro service approach and it is recommended to write lambda as small as possible and have it do one task. So that cost can be saved as it consumes less memory.

Watch the deployment package size, remove unused packages.

Watch the package.json file and see there are any unused installs as this would reduce the size of deployment package. also certain packages are available at node run time. we can exclude them.

lambda keeps some bake in libraries ready. some times if your code depends on certain versions of package, then its better to package that version in package.json , so that the update in package does not break your lambda.

Keep an eye on lambda logs

Monitor duration and memory consumption of lambda, this would needed to optimize and increase performance for your lambda.

Grant only necessary IAM Permissions to Lambda function.

This would protect your access to some of the not required services and access to administrator access should be avoided.

Unlink any temporary files used in lambda

Make sure the tmp files created in container are unlink before exiting the handler.

Delete unused lambda.

there are restrictions on how many lambda functions you can create in your account. good idea to remove the functions not used.

Exception handling

Make use of error handling mechanism. use DLQs where ever appropriate.

Use VPC only when necessary

If your functions uses resources like RDS with in VPC, then put that lambda with in VPC. other wise there is no need to use VPC. using VPC is also little added latency to function.

Be mindful to use reserved concurrency

Make sure other functions has enough concurrency to work, because every account get about1000 concurrent lambda executions across functions, if you reserve then the limit for remaining functions reduce by that amount.

Keep Containers warm.

So that they can be reused and latency gets decreased by cold start.

Make use of CI/CD for deployments

it is suggested to use the benefits of CI/CD by either sam cli or serverless. this would help in stream line the development process and reduce mistakes done through deployments.

--

--

Praveen Sambu
CloudInDetail

Software Engineer |AWS Community Builder |Technical Blogger | Trainer . Founder of Cloud In Detail (https://cloudindetail.com/) still working on building blog…