Skip to main content

How to avoid inactive Lambda

· 4 min read
Ivan Barlog
AWS Solutions Architect @ BeeSolve

In this short article you will learn what does it mean when your Lambda transitions to inactive state and how to deal with it.

TL;DR

You can use our CDK construct @beesolve/lambda-keep-active which invokes your Lambda functions regularly so they won't transition into inactive state.

First of all, what even is inactive Lambda?

A function becomes inactive when it has been idle long enough for Lambda to reclaim the external resources that were configured for it. When you try to invoke a function that is inactive, the invocation fails and Lambda sets the function to pending state until the function resources are recreated. If Lambda fails to recreate the resources, the function returns to the inactive state. You might need to resolve any errors and redeploy your function to restore it to the active state.

Lambda function states

What "long enough" means has already been discussed here. For our purposes let's say it is around 14 days.

What happens when your Lambda function transitions to inactive state? As stated in the documentation it means that AWS might reclaim the external ressources that were configured for the Lambda. This mostly means ENI (Elastic Network Interface). Initialization of ENI (after your Lambda was inactive and you hit it for the first time) can take up to 90 seconds which means that for that period of time your Lambda is not accessible and you will get 5xx error.

This mostly happens if you run your Lambdas in VPC but can happen outside of VPC as well. This issue is very annoying but fortunatelly it is easily fixable.

If you don't want your Lambda to transition to inactive state all you need to do is to ensure that it is invoked before it transitions.

You might not come across this issue when your application is used regularly but for seasonal applications like quarterly reporting applications this issue might occur each quarter. You probably don't want to invoke your lambdas manually every few days so the best thing to do would be to automate this process.

At BeeSolve we've already done that and we want to share our solution with you so you don't need to waste your time and reinvent the wheel. You can use our solution with CDK by installing our npm package:

npm i @beesolve/lambda-keep-active

How it works

Behind the scenes our construct tags your Lambda functions with special tag. Then when you deploy LambdaKeepAlive construct into your infrastructure special Lambda funcion is deployed which is invoked every three days. This lambda searches for tagged resources and invokes them with arbitrary data.

Most of the time you would like to keep active Lambda functions which handles your web traffic or authorization. These handlers expect events from various services for example API Gateway. Whenever our Lambda invokes your handler it sends just simple JSON payload. In order to avoid getting errors (as your handler probably expects API Gateway Event) you should wrap your handlers with our wrapper which detects our special "keep-active" invocations.

Every 3 days when our handler runs it cycles through tagged Lambda functions and invokes them - this is very quick and in general depending on how many Lambda functions you tag will run in few milliseconds. By default our handler is allocated just 128MB memory so it should be cheap enough to be worth it. On the other hand invocation of your tagged Lambda functions should be cheap enough as well as with using our wrapper the handler exits as soon as it detects the "keep-active" invocation. The cost of this solution shouldn't affect your workloads.

If for any reason you cannot use our CDK construct you can check the source code at GitHub as it is open source.

That's it for today. Keep building!