How to generate monthly report for EC2 server uptime

109 Views Asked by At

In cloudwatch, I've set up an alarm when an instance check fails. I want generate a report all EC2 instances server uptime whenever a reboot, stopped and start happen. Do I need to create a log group in order to send a report to S3 to be downloaded? Do advise me if there are other way to download a report of ec2 server uptime?

1

There are 1 best solutions below

0
MisterSmith On

I dont believe this functionality exists out of the box in AWS, but you can "roll your own" fairly easily.

As you already have an alarm and SNS topic you just need to subscribe/trigger a lambda function from the SNS topic to actually record that notification to an object on S3. Writing 1 file per event in response to each SNS message would be a good idea - especially if you have a bunch of machines that happen to change state at the same time. If your reading, updating and replacing a report object in S3 you could potentially end up loosing some events. To get around this...

Once you have seperate notifications being created by each status change in s3, you can write another lambda that reads the objects in S3 and compiles your report. You can either write this report back to S3, or use SES from your Lambda to send it via an email. You can use EventBridge to trigger a lambda function on a schedule

Also, A few additional thoughts:

  • Include the date of the message in your s3 path (eg /startstopevents/2024/02/24/randomly-generated-filename.json). This makes reporting super-easy - your 2nd lambda just needs to list all the objects with a prefix of /startstopevents/2024/02 - all the details you need are in the individual objects)
  • if your writing file to S3 automatically like this, dont forget a retention policy on the bucket in S3 to auto-delete the objects after some suitable time.