-1

I am trying to monitor the time used of my application by individual users in order to calculate the subscription credits left, accordingly.

It is PaaS service running on AWS EC2 instances and I would like to check how long it's been running every 15 minutes or something so that I can cancel the service if the user goes beyond the limit.

I am currently using python FastAPI for the API and the backend is built using ExpressJS, but am ready to try any other service to get this running.

Any help would be greatly appreciated!

guidingfox
  • 77
  • 2
  • 12
  • If your application is running on a linux machine or a docker container, then you can use that system to check the uptime for your application. – mama Aug 20 '21 at 14:53
  • @GhostOps Is that an ideal solution for use in a production environment? – guidingfox Aug 20 '21 at 15:01
  • @mama It is not running on any machine I can control. Like I said it is an AWS EC2 instance. – guidingfox Aug 20 '21 at 15:02
  • I don't know, sry i haven't tried like that actually i wouldn't recommend even that to anyone, i just gave my idea... sry... – Ghost Ops Aug 20 '21 at 15:02
  • The statement that you can't control this because it is an AWS EC2 instance doesn't make sense. An AWS EC2 instance is just a Linux or Windows VM. What is preventing you from logging into that instance? – Mark B Aug 20 '21 at 15:06
  • https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html – mama Aug 20 '21 at 15:06
  • @MarkB Sorry for the confusion. I meant that each user accesses each instance and for privacy reasons I can't login into it. – guidingfox Aug 20 '21 at 15:11
  • Are users paying for the time they run a specific application? Or the time the EC2 instance is running or something? The problem you are trying to solve is not clear. – Mark B Aug 20 '21 at 15:30
  • @MarkB They are paying for the time the instance is running. I thought I would use the CloudWatch metrics provided, but that is not available in the case of windows instances I think. I would require windows instances occasionally, too... Hope the question is clear now :) – guidingfox Aug 20 '21 at 16:05
  • If they are paying for the time an instance is running, why not just track how long the instance is running instead of tracking how long a specific application is running on the instance? – Mark B Aug 20 '21 at 16:21
  • @MarkB That would work for me, too. But I didn't find any way to get the time the instance has been running. (At least for Windows machines I didn't). – guidingfox Aug 20 '21 at 16:31
  • If you use the AWS API to get the time the instance was launched, then it doesn't matter what operating system the instance is running https://stackoverflow.com/questions/36417401/getting-running-time-for-an-aws-instance – Mark B Aug 20 '21 at 17:03
  • @MarkB I already use that while the instance is being launched, but if for example, A user 's limit expires while the person is using the instance, I would want to stop it immediately, but for that I would have to know the time left in every 15 mins or something. That is what is confusing me here. . – guidingfox Aug 20 '21 at 17:08

1 Answers1

1

It appears that your requirement is to know how long a currently-running Amazon EC2 instance has been running.

You can obtain this information by calling DescribeInstances() for all instances, or for specific instances. This will return a LaunchTime that you can use to calculate how long the instance has been running.

If you wish to implement a 'Time Limit' system, then a popular method is:

  • Add a tag to the instance indicating a time when the instance should be stopped/terminated
  • Trigger an AWS Lambda function every minute (or 5 minutes, choice is up to you)
  • The Lambda function should call DescribeInstances, which also returns the Tags for an instance
  • The Lambda instance should compare the current time to the time in the Tag. If the expiry time has passed, the Lambda function can stop/terminate the instance.
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • You might be interested in: [Simple EC2 Stopinator in Lambda - DEV Community](https://dev.to/aws/simple-ec2-stopinator-in-lambda-5goj) – John Rotenstein Aug 21 '21 at 02:02