How do I list Lambda functions with provisioned and reserved concurrency in an AWS Region?

5 minute read
0

I want to list the AWS Lambda functions that have reserved and provisioned concurrency within my AWS Region.

Resolution

The Lambda console doesn't display which Lambda functions are configured with reserved and provisioned concurrency. Use the Lambda Python script to list functions with provisioned and reserved concurrency as a target in a specific AWS Region.

Prerequisite

Make sure that the AWS Identity and Access Management (IAM) role for your Lambda function has the following permissions:

  • lambda:GetFunctionConcurrency
  • lambda:ListFunctions
  • lambda:ListProvisionedConcurrencyConfigs

Create your IAM role for the Lambda function

Complete the following steps:

  1. Make sure that the IAM role the user has the following permissions:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "iam:CreateRole",
            "iam:PassRole"
          ],
          "Resource": "*"
        }
      ]
    }
  2. Log in to the IAM console.

  3. In the left navigation pane, choose Roles.

  4. Choose the Create role button.

  5. In the Select type of trusted entity section, choose AWS service, and then select Lambda as the use case. Choose the Next: Permissions button.

  6. Search for the AWSLambdaBasicExecutionRole policy, and then select it. This policy grants your Lambda function basic permissions to run.

  7. Attach the required custom permissions as follows:

    Choose Create policy in a new browser tab or window to keep your role creation process intact.

    In the policy creation tool, switch to the JSON tab and enter the following policy document:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "lambda:GetFunctionConcurrency",
            "lambda:ListFunctions",
            "lambda:ListProvisionedConcurrencyConfigs"
          ],
          "Resource": "*"
        }
      ]
    }
  8. Choose Review policy, name the policy (for example, LambdaConcurrencyPermissions), and choose Create policy.

  9. Return to your role creation tab or window. Refresh the list of policies (if necessary), and then attach the policy that you just created by searching for its name and selecting it.

  10. (Optional) Proceed to Next: Tags. Add tags if required, and then choose Next: Review.

  11. Name your role (for example, LambdaExecutionRoleWithConcurrencyPermissions). Review the role details, and then choose Create role.

Create the Lambda function

Complete the following steps:

  1. Open the AWS Lambda console.
  2. Choose the Create function button.
  3. Go to the Author from scratch section.
  4. Enter a name for your Lambda function in the Function name field.
  5. Choose Python 3.10 as the runtime.
  6. In the Permission section, under Change default execution role, choose Use an existing role.
  7. In the Existing role dropdown list, select the IAM role that you created in the previous steps.
  8. After you select the role, choose Create Function.

Use the following code to replace the default Lambda function code:

import json
import boto3

def lambda_handler(event, context):
    # Select the region you would like to list function concurrency
    region = "region"

    lambda_client = boto3.client('lambda', region_name=region)
    count = 0

    # Create a paginator for the 'list_functions' operation.
    paginator = lambda_client.get_paginator('list_functions')
    lambda_iterator = paginator.paginate()

    # For loop to iterate through listed functions and return function name and concurrency response
    for items in lambda_iterator:
        for function in items['Functions']:
            function_name = function['FunctionName']

            try:
                response = lambda_client.get_function_concurrency(
                    FunctionName=function_name
                )

                # If reserved concurrency is configured, print the details
                if 'ReservedConcurrentExecutions' in response:
                    count += 1
                    reserved_concurrency = response['ReservedConcurrentExecutions']
                    print(f"{count}. Function Name: {function_name}, Reserved Concurrency: {reserved_concurrency}")

            except lambda_client.exceptions.ResourceNotFoundException:
                # If the function is not found, skip it
                pass
            except Exception as e:
                print(f"Error retrieving concurrency for {function_name}: {e}")

            try:
                response = lambda_client.list_provisioned_concurrency_configs(
                    FunctionName=function_name
                )

                # If provisioned concurrency is configured, print the details
                if 'ProvisionedConcurrencyConfigs' in response and response['ProvisionedConcurrencyConfigs']:
                    provisioned_concurrency = response['ProvisionedConcurrencyConfigs'][0]['RequestedProvisionedConcurrentExecutions']
                    count += 1
                    print(f"{count}. Function Name: {function_name}, Provisioned Concurrency: {provisioned_concurrency}")

            except lambda_client.exceptions.ResourceNotFoundException:
                # If the function is not found, skip it
                pass
            except Exception as e:
                print(f"Error retrieving provisioned concurrency for {function_name}: {e}")

The code iterates through each Lambda function. The function calls the get_function_concurrency method to retrieve details about the reserved or provisioned concurrency configuration for each function in the specified AWS Region.

Update the function code to the desired target AWS Region

Before you use the Lambda function, update the Region to where you want to list the functions with provisioned and reserved concurrency.

For example, to see the reserved and provisioned concurrency for Lambda functions in us-east-1 AWS Region, update the region variable as shown below:

region = "us-east-1"

Run the Lambda function

To run the Lambda function that you created, navigate to the Test tab. Then, follow the steps shown below.

  1. In the Lambda function, select the Test tab.
  2. In the Configure test event dialog, use the example event template to run the test.
  3. Enter an event name for the test, such as Test.
  4. To create the test event, choose Save.
  5. When you are done, choose Test to run the test.

You can view the output, which is a list, in the functions logs. Or, you can see the list within the Test tab of the Lambda function with configured reserved or provisioned concurrency. The list includes the following data:

  • Function name
  • Type of concurrency configured
  • The number of concurrency configured

Example output:

1. Function Name: ProvisionedConcurrencyTest, Provisioned Concurrency: 10
2. Function Name: ReservedConcurrencyTest, Reserved Concurrency: 50

Related information

Create a Lambda function with the console

AWS OFFICIAL
AWS OFFICIALUpdated 25 days ago