In this blog, I will share the Python script that will help you to list the AWS Lambda function details such as Function name, Function ARN, Run time, Description and Version etc...
I will be using Tabulate module to format my output in to table format. If you are new to Tabulate, please visit my previous blog where I have provided details of Tabulate module.
The following Python code is written in to Python 3.8 version.
import boto3
from tabulate import tabulate
After importing the Boto3 and Tabulate module, let's setup our AWS session details by providing region and profile name.
profile = "default"
region = "us-east-1"
session = boto3.Session(profile_name=profile, region_name=region)
lambda_client = session.client('lambda')
response = lambda_client.list_functions() # Get the list of functions
lambda_list = response['Functions']
The following code will help you to setup a Header row in the table
# Setup header row for a table
pending_request = []
pending_request.append(['Lambda Name', 'Platform', 'IAM Role'])
This for loop will check the each lambda name with in the lambda_list list and assign relevant values to the respective variables. You can obtain various information of a lambda function like FunactionName, FunctionArn, Runtime, Handler, CodeSize, Swacription, Timeout, MemorySize and LastModified etc...
# Check all the Lambda function name and collect required information
for lambda_names in lambda_list:
# Get the Lambda function name
lambda_name = lambda_names['FunctionName']
# Get Lambda function platform
lambda_platform = lambda_names['Runtime']
# Get Lambda function IAM role
lambda_role = lambda_names['Role']
print_response = [lambda_name[:50], lambda_platform, lambda_role.split('/')[1][:50]]
pending_request.append(print_response)
# print the output in to Table format.
print(tabulate(pending_request, headers="firstrow",tablefmt='simple'))
If you would like to truncate the Lambda name any number of characters you like. I am using the first 50 characters in my output. Same way, if you just want to display the IAM role name instead of full IAM Arn, please use .split('/')[1][:50] as shown above. This will take the string after "/" and display only first 50 characters.
Hope you find it useful.
Disclaimer: www.TechieTalks.co.uk does not conceal the possibility of error and shortcomings due to human or technical factors. www.TechieTalks.co.uk does not bear responsibility upon any loss or damage arising from conduct or activities related to the use of data and information contained in this blog.
Comments
Post a Comment