Skip to content

Commit

Permalink
Merge pull request awslabs#44 from nick-from-aws/lambda-check-concurr…
Browse files Browse the repository at this point in the history
…ency

Added Check Function Concurrency script
  • Loading branch information
joshua-at-aws authored Oct 28, 2018
2 parents 04110f4 + 965d7ea commit 550eb8f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Lambda/CheckFunctionConcurrency/CheckFunctionConcurrency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python3

# This script will check if your AWS account makes use of per-function concurrency in the current region. If so, it will display concurrency used per function.
# IAM Permissions required:
# Lambda: GetAccountSettings, ListFunctions, GetFunction
# STS: getCallerIdentity

import os
import sys
import boto3
import logging
logging.basicConfig(format='%(asctime)s - [%(levelname)s] %(message)s')
logger = logging.getLogger('simple_example')
# logger.setLevel(logging.DEBUG)
# logger.setLevel(logging.INFO)

# Get Credentials and Region
if os.path.exists(os.path.join(os.path.expanduser("~"), ".aws", "credentials")) or os.path.exists(
os.path.join(os.path.expanduser("~"), ".aws", "config")):
profile_name = input("Enter your AWS profile name [Default]: ") or "default"
session = boto3.Session(profile_name=profile_name)
default_region = session.region_name
region = input(f"Enter the AWS Region to check [Default: {default_region}]: ") or default_region
if region != default_region:
session = boto3.Session(profile_name=profile_name, region_name=region)
client = session.client('lambda')

else:
access_key = input("Enter your AWS access key ID: ")
secret_key = input("Enter your AWS secret key: ")
region = input("Enter the AWS Region to check")
client = boto3.client("lambda", aws_access_key_id=access_key,
aws_secret_access_key=secret_key, region_name=region)

response = client.get_account_settings()
logger.debug(response)

ConcurrentExecutions = response.get('AccountLimit', {}).get('ConcurrentExecutions')
UnreservedConcurrentExecutions = response.get('AccountLimit', {}).get('UnreservedConcurrentExecutions')

accountid = boto3.client('sts').get_caller_identity().get('Account')

print(f"""
Account: {accountid}
Region: {region}
Concurrent Execution Limit: {ConcurrentExecutions}
Unreserved Concurrent Execution Limit: {UnreservedConcurrentExecutions}
For more information on managing Lambda concurrency, see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html
""")

diff = int(ConcurrentExecutions) - int(UnreservedConcurrentExecutions)

if diff == 0:
print("Your account does not have any functions with reserved concurrency in this region")
sys.exit(0)
else:
print(f"A sum of {diff} concurrent executions are reserved by functions in this region.\nI will now gather which functions have function level concurrent execution limits set...\n\n")


# Get the first 100 functions
response = client.list_functions()
logger.debug(f'list_functions response: {response}\n\n')

functionList = []

for function in response['Functions']:
logger.debug(f"Function Name: {function['FunctionName']}")
functionList.append(function['FunctionName'])

print(f"Found {len(functionList)} functions\n")
logger.debug(f"Function List: {functionList}")

functionReservationDict = {}

print("The Following functions have per-function concurrency reservations:")
print("{:<30} {:<30}".format("FunctionName:", "ReservedConcurrentExecutions:"))

for function in functionList:
response = client.get_function(FunctionName=function)

logger.debug(f"Function: {function}")

reservationStatus = None

if response.get("Concurrency"):
reservationStatus = response['Concurrency']['ReservedConcurrentExecutions']
logger.debug(f"reservationStatus: {reservationStatus}")
print("{:<30} {:<30}".format(function, reservationStatus))

print("Done.")
33 changes: 33 additions & 0 deletions Lambda/CheckFunctionConcurrency/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Lambda - Check Function Concurrency

This script will check the concurrency configuration for a given region in your AWS account. The Account Level Concurrent Execution Limit and Function Level Concurrent Execution Limits will be displayed. No changes to your account will be made.

Setting the per function concurrency can impact the concurrency pool available to other functions, and this script is helpful to quickly determine which functions are making use of per function concurrency.

For more information on managing Lambda concurrency, see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html

## Requirements
This script requires Python 3.6+

The following IAM permissions are required in your AWS Account:

Lambda
* GetAccountSettings
* ListFunctions
* GetFunction

STS
* getCallerIdentity

## Usage

`python3 CheckFunctionConcurrency.py`

This script will use the credentials configured with the AWS CLI if available. If not, it will ask for an AWS access key and secret key.







0 comments on commit 550eb8f

Please sign in to comment.