Contents

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": "*"
    }
]
}

https://github.com/user-attachments/assets/dd0c2b03-fb37-46db-acba-2e5c305e7235

https://github.com/user-attachments/assets/13b8b3ec-a162-4a9b-abc8-c4c7211d2f11

  • 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.

https://github.com/user-attachments/assets/e4070ad6-078a-4259-b6ff-ddaf97d00618

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.

https://github.com/user-attachments/assets/cb345303-5102-457c-91f2-8c1c7986fd2f

https://github.com/user-attachments/assets/7f43b7b9-2e54-40f2-83fd-457cda8e199c

โž• 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

Access the GitHub repo here

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.'
}

https://github.com/user-attachments/assets/bb9b5f1a-a4f1-48ec-83fb-1ca013040993

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.

https://github.com/user-attachments/assets/d9ef905a-6b80-4082-90b9-b8c37d78e6e9

https://github.com/user-attachments/assets/30396551-2a0d-456b-815d-dbfea39221ae

https://github.com/user-attachments/assets/b85a8648-d869-4622-80f5-c2bd51d4fe83

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.

https://github.com/user-attachments/assets/cf68d3ca-40b2-49e5-a904-6953dbdfa14d

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.

https://github.com/user-attachments/assets/55b5f902-69a1-4a2c-b787-488cc1631ecf

https://github.com/user-attachments/assets/8792738f-66bd-4e0c-9160-1fc536424086

https://github.com/user-attachments/assets/f2835fc7-093b-47a0-ad37-027e6b234d62

https://github.com/user-attachments/assets/9f7d9f1c-2ec7-4d26-a75a-937c8c1ab3f3

https://github.com/user-attachments/assets/d9e35ddf-bea0-4bef-a756-8485e8ec2ed2

https://github.com/user-attachments/assets/16ee9fe2-6b5e-4245-ab83-abb0d52982ad

๐Ÿ“† 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.

https://github.com/user-attachments/assets/7d5cab50-7652-421d-a96b-670c5a80715a

https://github.com/user-attachments/assets/dcac9de9-32f0-4a96-b6e8-db83af498cfa

https://github.com/user-attachments/assets/b3370350-d1e6-4b93-bdac-d9529364d0f4

https://github.com/user-attachments/assets/7900d7a4-1821-4f56-90d7-b397694cd92e

https://github.com/user-attachments/assets/80b3dd22-a0e8-4a93-bf7c-bf70594b409a

https://github.com/user-attachments/assets/f34c72d0-1442-4e52-8db6-ecb94f834e49

https://github.com/user-attachments/assets/c491abc3-b441-46ab-89b8-04184caa6d9d

https://github.com/user-attachments/assets/624c8c79-b531-4bb6-8a73-5d15049125e5