Mish Nia
Mish Nia

Reputation: 79

Is there a way to find out the number of functions present in AWS Lambda?

I'm using Boto3 python module to communicate with AWS Lambda. I want to find out how many functions are present in the account from code. There are functions that list the functions, create paginators, and get a particular function. But is there a function that returns the total count of functions that are present in Lambda? I'm writing a code that parses through every Lambda function. I want to show a progress bar in the terminal that shows how many functions have been covered so far, so that the user gets a rough estimate about how much longer it will take to finish execution.

Upvotes: 0

Views: 1916

Answers (3)

Mish Nia
Mish Nia

Reputation: 79

Found that we can get the number of Lambda functions in an account using Boto3. Although this answer by Mr John Rotenstein is one way to get the list of all the accounts, when you have tens of thousands of such functions, it gets very slow to do that. If you're looking for just the total number of Lambda functions, you can use get_account_settings() function that returns:

{
    'AccountLimit': {
        'TotalCodeSize': 123,
        'CodeSizeUnzipped': 123,
        'CodeSizeZipped': 123,
        'ConcurrentExecutions': 123,
        'UnreservedConcurrentExecutions': 123
    },
    'AccountUsage': {
        'TotalCodeSize': 123,
        'FunctionCount': 123
    }
}

You can find the FunctionCount here.

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53535

Using aws-cli and jq you can do something like:

aws lambda list-functions --max-items=10000 | jq -r '.Functions' | jq length

which will get all the functions in the account and return them in the response (as a json array), then piping the result through jq can extract the functions-array and count the number of items in it.

Note that the order of magnitude of the number of lambdas in the account is probably pretty static and if you want to provide a feedback of the % of function you finished processing, it's good enough to make this call only once in a while and cache the result, since it's going to take a few seconds to read all these resources!

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269500

No, there is no function that simply returns a count of AWS Lambda functions in an account. (In fact, I don't recall seeing any AWS API calls that simply return counts in any of the AWS services.)

You would need to use list_functions(), but it only returns a maximum of 50 functions. If the list_functions() call returns a NextMarker value, then call the function again with that value in the Marker parameter.

The ListFunctions paginator can do this for you, but it will still involve an API call for every 50 results.

Upvotes: 4

Related Questions