Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a custom metric watching EFS metered size in AWS Cloudwatch?

Title pretty much says it all - Since the EFS metered size (usage) ist not a metric that I can use in Cloudwatch, I need to create a custom metric watching the last metered file size in EFS.

Is there any possiblity to do so? Or is there maybe a even better way to monitore the size of my EFS?

like image 971
David Avatar asked Mar 26 '19 13:03

David


People also ask

Which of the following is a custom metric in CloudWatch which you have to manually set up?

Which of the following is a custom metric in CloudWatch which you have to manually set up? Memory Utilization of an EC2 instance.

Which of the following requires a custom CloudWatch metrics to monitor?

The correct answer is option A (Memory Utilization of an EC2 instance).


1 Answers

I would recommend using a Lambda, running every hour or so and sending the data into CloudWatch.

This code gathers all the EFS File Systems and sends their size (in kb) to Cloudwatch along with the file system name. Modify it to suit your needs:

import json
import boto3

region = "us-east-1"

def push_efs_size_metric(region):

    efs_name = []
    efs = boto3.client('efs', region_name=region)
    cw = boto3.client('cloudwatch', region_name=region)

    efs_file_systems = efs.describe_file_systems()['FileSystems']

    for fs in efs_file_systems:
        efs_name.append(fs['Name'])
        cw.put_metric_data(
            Namespace="EFS Metrics",
            MetricData=[
                {
                    'MetricName': 'EFS Size',
                    'Dimensions': [
                        {
                            'Name': 'EFS_Name',
                            'Value': fs['Name']
                        }
                    ],
                    'Value': fs['SizeInBytes']['Value']/1024,
                    'Unit': 'Kilobytes'
                }
            ]
        )
    return efs_name

def cloudtrail_handler(event, context):
    response = push_efs_size_metric(region)
    print ({
        'EFS Names' : response
    })

I'd also suggest reading up on the reference below for more details on creating custom metrics.

References

  • https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html
like image 185
kenlukas Avatar answered Sep 21 '22 13:09

kenlukas