Prerequisites
You already have configured AWS Credentials in ~/.aws/credentials location
Overview
This blog post provides a Python script that uses the boto3 library to list AWS Lambda functions and check which of these functions are not scheduled by any Amazon EventBridge rules. This is particularly useful for developers and system administrators who manage AWS resources and need to ensure that all necessary Lambda functions are triggered as expected without manual intervention.
Explanation with Inline Comments
import boto3
# Function to list all Lambda functions in the AWS account
def list_lambda_functions():
lambda_client = boto3.client('lambda') # Create a Lambda service client
paginator = lambda_client.get_paginator('list_functions') # Paginator for handling large responses
lambda_functions = []
for page in paginator.paginate(): # Iterate over each page of the response
for func in page['Functions']:
lambda_functions.append(func['FunctionName']) # Collect function names
return lambda_functions
# Function to list all Lambda functions targeted by EventBridge rules
def list_eventbridge_targets():
eventbridge_client = boto3.client('events') # Create an EventBridge service client
paginator = eventbridge_client.get_paginator('list_rules') # Paginator for handling large responses
target_functions = set()
for page in paginator.paginate(): # Iterate over each page of the response
rules = page['Rules']
for rule in rules:
targets_response = eventbridge_client.list_targets_by_rule(Rule=rule['Name'])
targets = targets_response['Targets']
for target in targets:
if 'Arn' in target:
arn_parts = target['Arn'].split(':')
if 'function' in arn_parts:
function_name = arn_parts[-1].split('/')[-1]
target_functions.add(function_name) # Collect unique function names
return target_functions
# Function to find unscheduled Lambda functions
def find_unscheduled_lambdas():
lambda_functions = list_lambda_functions() # Get all Lambda functions
scheduled_functions = list_eventbridge_targets() # Get all targeted functions by EventBridge
unscheduled_functions = [func for func in lambda_functions if func not in scheduled_functions] # Determine unscheduled functions
return unscheduled_functions
# Use the function to get the list of unscheduled Lambda functions
unscheduled_lambdas = find_unscheduled_lambdas()
print("Unscheduled Lambda Functions:", unscheduled_lambdas)
Listing Lambda Functions
The first step in managing Lambda functions is to list all the functions in your AWS account. We can achieve this using Boto3’s list_functions
method.
import boto3
def list_lambda_functions():
lambda_client = boto3.client('lambda')
paginator = lambda_client.get_paginator('list_functions')
lambda_functions = []
for page in paginator.paginate():
for func in page['Functions']:
lambda_functions.append(func['FunctionName'])
return lambda_functions
# Get a list of all Lambda functions
lambda_functions = list_lambda_functions()
print("Lambda Functions:", lambda_functions)
Listing EventBridge Targets
In many cases, Lambda functions are triggered by EventBridge rules. To ensure proper orchestration, it’s essential to list all the Lambda functions targeted by EventBridge rules.
def list_eventbridge_targets():
eventbridge_client = boto3.client('events')
paginator = eventbridge_client.get_paginator('list_rules')
target_functions = set()
for page in paginator.paginate():
rules = page['Rules']
for rule in rules:
targets_response = eventbridge_client.list_targets_by_rule(Rule=rule['Name'])
targets = targets_response['Targets']
for target in targets:
if 'Arn' in target:
arn_parts = target['Arn'].split(':')
if 'function' in arn_parts:
function_name = arn_parts[-1].split('/')[-1]
target_functions.add(function_name)
return target_functions
# Get a list of all Lambda functions targeted by EventBridge rules
eventbridge_targets = list_eventbridge_targets()
print("EventBridge Targets:", eventbridge_targets)
Finding Unscheduled Lambda Functions
One common challenge is identifying Lambda functions that are not scheduled or triggered by any EventBridge rules. We can accomplish this by comparing the lists of all Lambda functions with those targeted by EventBridge rules.
def find_unscheduled_lambdas():
lambda_functions = list_lambda_functions()
scheduled_functions = list_eventbridge_targets()
unscheduled_functions = [func for func in lambda_functions if func not in scheduled_functions]
return unscheduled_functions
# Find unscheduled Lambda functions
unscheduled_lambdas = find_unscheduled_lambdas()
print("Unscheduled Lambda Functions:", unscheduled_lambdas)