AWS Lambda Rnutime and Functions name and CloudWatch Dashboard Setup
๐ฆ ๐ผ ๐ธ Lambda Rnutime and Functions name and CloudWatch Dashboard Setup ๐ชง โ๏ธ ๐ ๐
This document provides step-by-step instructions to set up a custom CloudWatch Dashboard that shows Lambda Runtime with version and Lambda Function name and how many on our AWS Account
Step 1: Create a Custom Policy for Lambda Function
Create a Custom Policy ๐งพ
-
Go to IAM in the AWS Management Console.
-
Click on Create Policy.
-
Select the JSON tab and paste the following policy:
For custom policy GitHub repo here
json
Copy code
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:ListFunctions",
"cloudwatch:PutMetricData"
],
"Resource": "*"
}
]
}
-
Click on Next.
-
Provide a suitable name for the custom policy and click on Create policy.
Step 2: Create a Lambda Function โ๏ธ
-
Create the Lambda Function
-
Go to Lambda in the AWS Management Console.
-
Click on Create function.
-
Provide a suitable function name.
-
Select runtime (Python 3.8).
-
Click on Create function.
Assign IAM Policy to Lambda Function Role ๐
-
Click on Configuration > Permissions.
-
Click on the Role name to open the IAM Role page.
-
Click on Add permissions > Attach policies.
-
Click on Filter policies and select Customer managed.
-
Search for the policy created in Step 1, select it, and click on Add permissions.
โ Add Lambda Function Code
-
Go back to the Lambda function.
-
Click on Code.
-
In the Lambda function section, paste the following code
-
After pasting the code click on Deploy.
-
Lambda Code for Bucket Versioning Status
python Copy code
import boto3
import datetime
lambda_client = boto3.client('lambda')
cloudwatch_client = boto3.client('cloudwatch')
def lambda_handler(event, context):
paginator = lambda_client.get_paginator('list_functions')
response_iterator = paginator.paginate()
runtime_counts = {}
function_runtimes = {}
for response in response_iterator:
for function in response['Functions']:
runtime = function['Runtime']
function_name = function['FunctionName']
function_runtimes[function_name] = runtime
if runtime in runtime_counts:
runtime_counts[runtime] += 1
else:
runtime_counts[runtime] = 1
# Publish metrics to CloudWatch
metric_data = []
for runtime, count in runtime_counts.items():
metric_data.append({
'MetricName': 'LambdaRuntimeUsage',
'Dimensions': [
{
'Name': 'Runtime',
'Value': runtime
},
],
'Timestamp': datetime.datetime.utcnow(),
'Value': count,
'Unit': 'Count'
})
# Publish function names and runtimes to CloudWatch as logs
for function_name, runtime in function_runtimes.items():
cloudwatch_client.put_metric_data(
Namespace='Lambda/FunctionRuntimes',
MetricData=[
{
'MetricName': function_name,
'Dimensions': [
{
'Name': 'Runtime',
'Value': runtime
}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': 1,
'Unit': 'Count'
}
]
)
if metric_data:
cloudwatch_client.put_metric_data(
Namespace='Lambda/RuntimeUsage',
MetricData=metric_data
)
return {
'statusCode': 200,
'body': 'Metrics published successfully.'
}
Test the Lambda Function โ
-
Click on Test > Configure test event.
-
Provide an event name.
-
Click on Save.
-
Click on Test to run the Lambda function.
Step 3: Create a CloudWatch Dashboard ๐ชง
-
Create a Dashboard
-
Go to CloudWatch in the AWS Management Console.
-
Click on Dashboards > Create dashboard.
-
Provide a name and click on Create dashboard.
Add Widgets to the Dashboard
Widget: Lambda Runtime
-
Click on Add widget.
-
Select Data table and click on Next.
-
Select the namespace Lambda/FunctionRuntimes and metric Runtime
-
Select all Runtime and click on Create widget.
-
Click on the three dots of the widget and select Edit.
-
Click on Data table and select Only display summary columns in the table.
-
In Summary, select Average and click on Update widget.
๐ Schedule for Lambda Trigger ๐
-
Go to AWS CloudWatch.
-
Click on Rules.
-
Amazon EventBridge will open. Select Schedules.
-
Click on Create Schedule.
-
Provide a name.
-
Schedule group should be (default).
-
Schedule pattern (Recurring schedule).
-
Schedule type (Rate-based schedule).
-
Rate expression (days).
-
Flexible time window (15 minutes).
-
Leave the timeframe empty and click on Next.
For Target ๐ฏ
-
Select AWS Lambda.
-
Invoke the Lambda function created earlier and click on Next.
-
Leave the next page settings as default and click on Next.
-
Review all settings and click on Create schedule. This schedule will run once a day.