Skip to content

Stop EC2 instance when its in ideal state.

0

Hi, I have installed Neo4j Community Edition on an EC2 instance, which will only be used for scheduled jobs. I want to stop the EC2 instance when it is idle, but using metrics like CPUUtilization and MemoryUsed from the instance makes it difficult to determine the ideal idle time. Since Neo4j CE does not support monitoring by default, I need a way—using an agent, custom script, or any supported metric—to accurately identify when Neo4j is idle and stop the instance accordingly.

2 Answers
1

You can create a lambda function that can ping the ec2 to check the status of Neo4j. If you cannot directly check the status of Neo4j, you can configure Neo4j to output the log to a file and you can query that file from lambda to check if it completed. Once it says completed you can put the command to stop ec2 instance in lambda itself. You can create an event bridge rule to run lambda every 1 hour.

Hope this helps

AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

0

To stop your EC2 instance running Neo4j Community Edition when it's idle, you can implement a custom solution using a combination of AWS services and custom scripts. Here's an approach you can consider:

  1. Create a custom script that monitors Neo4j activity:

    • Develop a script that checks Neo4j's activity status, such as querying for active transactions or monitoring log files for recent activity.
    • This script can be run periodically on the EC2 instance.
  2. Use Amazon CloudWatch with custom metrics:

    • Have your script push custom metrics to CloudWatch that indicate Neo4j's idle status.
    • For example, you could create a metric called "Neo4jIdleTime" that increases when no activity is detected.
  3. Set up CloudWatch Alarms:

    • Create an alarm based on your custom metric (e.g., when "Neo4jIdleTime" exceeds a certain threshold).
  4. Use AWS Lambda with Amazon EventBridge:

    • When the CloudWatch Alarm triggers, use an EventBridge rule to invoke a Lambda function.
    • The Lambda function can then use the AWS SDK to stop the EC2 instance.
  5. Implement a graceful shutdown:

    • Before stopping the instance, ensure your script performs a graceful shutdown of Neo4j to prevent data corruption.
  6. Consider using Instance Scheduler:

    • If your scheduled jobs follow a predictable pattern, you might also benefit from using Instance Scheduler on AWS to automate starting and stopping your EC2 instance based on a defined schedule.

By implementing this solution, you can more accurately determine when Neo4j is truly idle based on its activity, rather than relying solely on general instance metrics like CPU utilization. This approach allows for a more tailored and precise method of managing your EC2 instance's uptime, potentially reducing costs while ensuring your scheduled jobs can run as needed.

Remember to thoroughly test this setup in a non-production environment first to ensure it doesn't interfere with your scheduled jobs or cause any data integrity issues.
Sources
Stop and start Amazon EC2 instances - Amazon Elastic Compute Cloud

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.