Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

460
Vistas
Programmatically Stop AWS EC2 in case of inactivity

Can we stop an AWS windows server EC2 instance of a development environment when there is no activity in it, say after 2 hours of inactivity? I am having trouble identifying whether any user is connected to the server virtually.

I can easily start/stop the EC2 at a fixed time, programmatically, but in order to cut the cost of my server, I am trying to stop the EC2 when it is not being used.

My intent(or use case) is: If no user is using the EC2 till a specified amount of time, it will automatically stop. Developers can restart it as and when needed.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Easiest solution probably would be to set up an Alert with CloudWatch.

Have a read at the documentation, which basically describes your use case perfectly:

You can create an alarm that stops an Amazon EC2 instance when a certain threshold has been met

A condition could be the average CPU utilisation, e.g. CPU utilisation is below a certain point (which most probably correlates with no logged in users / no developer actually utilising the machine).

over 4 years ago · Santiago Trujillo Denunciar

0

This is not a simple task.

The Amazon EC2 service provides a virtual computer that has RAM, CPU and Disk. It can view the amount of activity on the CPU, Network traffic and disk access but it cannot see into the Operating System.

So, the problem becomes how to detect 'inactivity'. This really comes down to the operating system and making some hard decisions. For example, your home computer screen turns off after a defined time of no mouse/keyboard input but the operating system is still doing activity in the background. If the system is running an application such as a web server, and there are no web requests, it is hard to know whether this is 'inactive' because there are no requests, or 'active' because the web server is running.

Bottom line: There is no out-of-the-box feature to do this. You would need to find your own definition of 'inactivity' and then trigger a shutdown in the Operating System.

If you wish to do it via schedule, this might help: Auto-Stop EC2 instances when they finish a task - DEV Community

over 4 years ago · Santiago Trujillo Denunciar

0

UPDATE: Lambda's aren't needed anymore, see tpschmidt's answer.

Create a Lambda to turn off the EC2 that will be triggered by a Cloud Watch Alarm when for example the CPU goes under 20% average for an hour. This is fine when you're coding as you will be using more than 20%, and when you have a break for over an hour that's when you want it turned off.

Be sure to set auto save in your IDE's.

Example Python Lambda:

import boto3
region = 'eu-west-3'
instances = ['i-05be5c0c4039881ed']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
    #TODO getInstanceIDFromCloudWatch = event["instanceid"]
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

Ref: https://www.howtoforge.com/aws-lambda-function-to-start-and-stop-ec2-instance/

In AWS Console:

  1. Goto EC2, select the EC2 instance and copy the Instance ID

  2. Goto Cloud Watch and select Metrics

  3. Under AWS Namespaces click EC2

  4. Paste the Instance ID to find it

  5. Select EC2 > Per-Instance Metrics

  6. Choose the first metric CPU utilisation

  7. Select the second tab called Graphed Metric

  8. Click the Bell icon under Actions

  9. Set a threshold, also this is the hard part, leave the default of Statistic: Average over 1 hour

  10. Set the Condition Lower/Equal and put the value as 20% (you'll need to use the machine more than 1/5th of the hour over 20% CPU otherwise it'll turn off).

  11. Next create an alarm, setup a notification if you like or remove it

  12. Once the Alarm is created

  13. In Cloud Watch select Event > Rules

  14. Add a Rule

  15. Select EC2 as the Service Name and All Event

  16. Click Target and select your Lambda.

  17. When the Alarm goes off the Lambda will turn off the instance ID

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda