A Python-based AWS Lambda function that monitors file counts in directories (particularly EFS mounts) and publishes metrics to Amazon CloudWatch for monitoring and alerting.
This project provides a Lambda function that counts files in a specified directory and sends the count as a custom metric to CloudWatch. It's particularly useful for monitoring inode usage on Amazon EFS (Elastic File System) mounts.
- Count files in specified directories
- Publish custom metrics to CloudWatch
- Support for EFS mount monitoring
- Configurable metric dimensions
- Lambda-ready deployment
FilecountvFinal.py- Main production Lambda functionfilecount.py- Simple file counting utilitytestapp.py/testapp2.py- Test applications for CloudWatch metricsFilecountv1.pytoFilecountv4.py- Development versions
The main function (FilecountvFinal.py) monitors the /mnt/global directory and publishes metrics with the following dimensions:
- Directory: PE1/Global
- APP_VERSION: 1.0
- Namespace: SAP
def lambda_handler(event, context):
cloudwatch = boto3.client('cloudwatch')
dirListing = os.listdir('/mnt/global')
filecount = len(dirListing)
# Publish to CloudWatch
response = cloudwatch.put_metric_data(
MetricData=[{
'MetricName': 'Files',
'Dimensions': [
{'Name': 'Directory', 'Value': 'PE1/Global'},
{'Name': 'APP_VERSION', 'Value': '1.0'}
],
'Unit': 'None',
'Value': filecount
}],
Namespace='SAP'
)- AWS Lambda environment
- Python 3.x
- boto3 library
- Appropriate IAM permissions for CloudWatch
The Lambda function requires the following CloudWatch permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData"
],
"Resource": "*"
}
]
}- Package the Lambda function
- Deploy to AWS Lambda
- Configure appropriate IAM role
- Set up CloudWatch alarms based on the metrics
The function publishes metrics to CloudWatch under the SAP namespace with the metric name Files. You can create alarms and dashboards based on these metrics to monitor file count thresholds.
This project is provided as-is for educational and monitoring purposes.