List AWS Lambda Functions with Provisioned Concurrency & Reserved Concurrency
Background
I was testing Provisioned Concurrency (PC) feature on AWS Lambda when it first released in 2019 and created a bunch of functions with PC. Today, I don’t even remember the names of these functions. As an active user of AWS Lambda for the past 4 1/2 years, I have accumulated more than 400 functions in my AWS account. Now, the challenge is to identify the functions with Provisioned concurrency.
Implementation
Since there isn’t an API (as of writing this article) to list the Lambda functions with Provisioned concurrency or an option available in the AWS console, the only option is to iterate through the list of functions and get the Provisioned Concurrency configurations. Initially, I thought it was simple, because it’s just two APIs:
A/ “listFunctions”
B/ “getProvisionedConcurrencyConfig”
However, I had a few challenges as I was hitting the API limits while getting the configurations and had to implement retry mechanism. The code is written in Node JS, however, using the same API calls, can be re-written in any programming language.
Additional Perk: The code also has the logic to identify the Reserved Concurrency. Here is the complete CODE for reference. I’ve used Cloud9 for debugging and AWS Lambda to run the script.
A/ ListFunctions
Out of the two main APIs, let’s take a closer look into the “listFunctions” API call:
List Functions method has two versions — using AWS SDK V2 (Commented) and AWS SDK V3 in Index JS file. AWS SDK V3 became Generally Available on December 15, 2020 and has amazing Pagination features. Check out the “References” section for additional code samples.
1/ First, using AWS SDK V3, we get the list of function names in the specified region and store it in an array
2/ We need to loop the function and use the “NextMarker” field to iterate through the list of functions. Using AWS SDK V3, traversing is much simpler as we get a next marker if there is more content available.
B/ GetProvisionedConcurrencyConfig
“ProvisionedConcurrency.js” generates the list of functions with PC enabled. AWS SDK provides an API call “listProvisionedConcurrencyConfigs” which takes a function name as a parameter.
3/ Iterate the FunctionList array and get the ProvisionedConcurrency Configuration on each of the functions
4/ While performing “listProvisionedConcurrencyConfigs” , if there is an API throttle/error, it will generate an error response and we add those list of failed functions to PCRetryList Set object for further retry.
On a test run, the script was able to pass 237 functions, however there were more than 150 functions in the PCRetryList.
Retry Logic
Once we get the PCRetryList, retry the list for 3 times and wait for 30 seconds in between the retry intervals
If there are no more functions to retry, the list will terminate and generate any list of functions with Provisioned Concurrency.
C/ Reserved Concurrency
The same logic as Provisioned Concurrency applies to Reserved Concurrency. We iterate the functions list and perform “getFunctionConcurrency” API call, followed by the similar retry logic.
One advantage of both “getProvisionedConcurrencyConfig” and “getFunctionConcurrency” AWS SDK API calls are, this works irrespective of the number of versions or aliases on the account and we don’t need to iterate them separately.
Future Enhancements
- Rather than waiting for 30 seconds within the Lambda function, we can replace the flow with a Step Functions
- Generate a PDF Report with the list of functions with PC and Reserved Concurrency and store it in S3
Feel free to share your thoughts and comments. Thanks for reading :)
Special Thanks
I would like to thank Heitor Lessa for graciously accepting to help review my Medium post and provide value suggestions.