Skip to main content

AWS Lambda Monitor

AWS Lambda Monitor is a small monitoring tool which runs in AWS Lambda. The tool is triggered either regularly by a CloudWatch scheduled task or on demand by an email triggered event. With this tool you can monitor services on servers out on the internet for free and receive alert emails when there are problems.

Why create another monitoring tool?

The rationale behind creating this tool (instead of using one of the many existing monitoring tools in existence) is to take advantage of the AWS Lambda pricing model to get free monitoring forever. AWS provides 37 days of 128MB memory Lambda execution per month. Unlike the EC2 free tier, the Lambda free tier doesn’t expire after the first year.

The reason the tool uses email for input (triggering a monitoring run) and output (alerting on problems) is that the AWS SES pricing model allows for 1000 emails per month. The alternative interface for AWS Lambda would be the AWS API Gateway which costs $3.50/month.

How to build and upload awslambdamonitor to AWS

Build and package virtualenv

To build the zip file containing the virtualenv, spin up an Amazon Linux EC2 instance (as this is the environment that AWS Lambda functions run in). Create the zip file as follows

sudo yum groupinstall 'Development Tools'
sudo yum install libyaml-devel libffi-devel openssl-devel
virtualenv build-aws-lambda-monitor-environment
build-aws-lambda-monitor-environment/bin/pip install pyOpenSSL paramiko ecdsa pycrypto python-whois PyYAML ndg-httpsclient pyasn1 requests
pushd build-aws-lambda-monitor-environment/lib/python2.7/dist-packages/
zip -r ~/awslambdamonitor.zip *
popd

pushd build-aws-lambda-monitor-environment/lib64/python2.7/dist-packages/
zip -r ~/awslambdamonitor.zip *
popd

rm -rf build-aws-lambda-monitor-environment

scp fetch the file from the amazon linux machine

Download the resulting zipped virtualenv from the EC2 instance and destroy the instance.

Add the monitor to the zipped virtualenv

zip --junk-paths awslambdamonitor.zip awslambdamonitor/monitor.py

Add your config to the zipped virtualenv

zip --junk-paths awslambdamonitor.zip awslambdamonitor/monitor.yaml

Publish package to AWS Lambda and setup CloudWatch scheduled job

AWS_ACCOUNT_ID=123456789012
AWS_PROFILE=myprofilename
AWS_REGION=us-west-2
aws lambda create-function --function-name monitor --runtime python2.7 --timeout 30 --role arn:aws:iam::$AWS_ACCOUNT_ID:role/lambda_basic_execution --handler monitor.monitor --zip-file fileb://awslambdamonitor.zip  --profile $AWS_PROFILE --region $AWS_REGION
aws lambda invoke --function-name monitor --log-type Tail --payload '{"account": "123456789012","region": "us-east-1","detail": {},"detail-type": "Scheduled Event","source": "aws.events","time": "1970-01-01T00:00:00Z","id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c","resources": ["arn:aws:events:us-east-1:123456789012:rule/AWSLambdaMonitor5Minutes"]}'  --profile $AWS_PROFILE --region $AWS_REGION output.txt

aws lambda update-function-code --function-name monitor --zip-file fileb://awslambdamonitor.zip --profile $AWS_PROFILE --region $AWS_REGION

aws events put-rule --name AWSLambdaMonitor5Minutes --schedule-expression 'rate(5 minutes)' --state ENABLED --profile $AWS_PROFILE --region $AWS_REGION
aws events put-rule --name AWSLambdaMonitorDaily --schedule-expression 'rate(1 day)' --state ENABLED --profile $AWS_PROFILE --region $AWS_REGION

aws lambda add-permission --function-name monitor --statement-id AWSLambdaMonitor5MinutesID --action 'lambda:monitor' --principal events.amazonaws.com --source-arn arn:aws:events:$AWS_REGION:$AWS_ACCOUNT_ID:rule/AWSLambdaMonitor5Minutes --profile $AWS_PROFILE --region $AWS_REGION
aws lambda add-permission --function-name monitor --statement-id AWSLambdaMonitorDailyID --action 'lambda:monitor' --principal events.amazonaws.com --source-arn arn:aws:events:$AWS_REGION:$AWS_ACCOUNT_ID:rule/AWSLambdaMonitorDaily --profile $AWS_PROFILE --region $AWS_REGION

aws events put-targets --rule AWSLambdaMonitor5Minutes --targets '{"Id" : "AWSLambdaMonitor5Minutes-monitor", "Arn": "arn:aws:lambda:$AWS_REGION:$AWS_ACCOUNT_ID:function:monitor"}' --profile $AWS_PROFILE --region $AWS_REGION
aws events put-targets --rule AWSLambdaMonitorDaily --targets '{"Id" : "AWSLambdaMonitorDaily-monitor", "Arn": "arn:aws:lambda:$AWS_REGION:$AWS_ACCOUNT_ID:function:monitor"}' --profile $AWS_PROFILE --region $AWS_REGION

Iterate on code by updating and uploading

If you want to extend or modify the monitor you can update the running code like this

# Update the file in the zip archive
zip --junk-paths awslambdamonitor.zip awslambdamonitor/monitor.py

# Upload the new zip file
aws lambda update-function-code --function-name monitor --zip-file fileb://awslambdamonitor.zip --profile $AWS_PROFILE --region $AWS_REGION

If you want to change your configuration

# Update the file in the zip archive
zip --junk-paths awslambdamonitor.zip awslambdamonitor/monitor.yaml

# Upload the new zip file
aws lambda update-function-code --function-name monitor --zip-file fileb://awslambdamonitor.zip --profile $AWS_PROFILE --region $AWS_REGION

Test Event

Here is an example event that you can use in the AWS Lambda web console to test the monitor

{
  "account": "123456789012",
  "region": "us-east-1",
  "detail": {},
  "detail-type": "Scheduled Event",
  "source": "aws.events",
  "time": "1970-01-01T00:00:00Z",
  "id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
  "resources": [
    "arn:aws:events:us-east-1:123456789012:rule/AWSLambdaMonitor5Minutes"
  ]
}

Metadata

Release files for awslambdamonitor 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for awslambdamonitor 1.0.0
File Size Uploaded Provenance
awslambdamonitor-1.0.0.tar.gz 9.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for awslambdamonitor 1.0.0
File Interpreter ABI Platform
awslambdamonitor-1.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 21.0 kB

Release files / awslambdamonitor-1.0.0.tar.gz

Download URL awslambdamonitor-1.0.0.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c7848271d3ce00d05a49960d19cf56cf27d3bb936ae8455c196cf3a271aca92d
BLAKE2b-256 checksum
How to use checksums
0c351db20f9a9fab3c745d0b220bc911a5e3e90bea34fc094eed1a79a47228a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / awslambdamonitor-1.0.0-py2-none-any.whl

Download URL awslambdamonitor-1.0.0-py2-none-any.whl
Size 11.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
7cf07d01716d0ed214bad3517b8441619e6aa02020591005d0ee58ead69edc7d
BLAKE2b-256 checksum
How to use checksums
aa3dbde0faa7b394559e6421845689f9bf3242400b9cd844046a51d8fdaab480
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for awslambdamonitor 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page