Monitor EC2 using crontab

If you want to monitor your Amazon Web Services (AWS) Elastic Cloud Compute (EC2) instances using crontab and AWS SDK. Here in this example I have chosen php (7.1).

Install AWS SDK

Install composer

composer require aws/aws-sdk-php


Get the index.php from github.
Replace the key and secret with your AWS Access and Secret keys. There are multiple ways to provide your credentials, like using profiles from ~/.aws/credentials, or retrieving them from your environment variables using getenv(), etc.

In the EC2Client provide the region name from the sub-domain name of your ec2 instance’s public DNS.
Provide the ec2 instance id in the $instanceIds array.

Create a Test File

You can create a test.html with the text ok in it. Host it on the server you want to monitor and make sure it is available via your browser and copy paste the address into the $url variable in index.php.

Create a cron job

sudo -i
crontab -e

The following line runs every minute.

* * * * * php /path/to/script/index.php

Ctrl+O (save) -> Enter -> Ctrl+X (close)
service cron restart