Answers: <p>AWS has something built in called <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html" rel="nofollow noreferrer">cloudwatch alarms</a> that can handle the basics.</p>
  <p>You could also use a Lambda Function. Give it access to the ASG, and then have cloudwatch configured so once the min amount of traffic starts coming in it sends an <a href="https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html" rel="nofollow noreferrer">Event</a> to the Lambda.</p>
  <pre><code>{
    &quot;ASG&quot;: &quot;MyASGName&quot;
    &quot;Action&quot;: &quot;Start&quot;
  }
  </code></pre>
  <p>And once the traffic goes below the min.</p>
  <pre><code>{
    &quot;ASG&quot;: &quot;MyASGName&quot;
    &quot;Action&quot;: &quot;Stop&quot;
  }
  </code></pre>
  <p>You could also build on this over time to control Vertical and Horizontal scaling.</p>
  <p>EDIT: I actually prefer the Lambda. I always seem to run into these weird use cases where we can't shut down any of the instances, we have to do some math or investigation on which instances to shut down. In these situations a python lambda running boto3 is really helpful.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-04-19T12:27:03.920Z
Answers: 2018-04-19T14:36:47.227Z
Answers: 2020-06-18T08:28:10.803Z
Answers: Levi
Body: <p>I have started playing around with AWS (privately). I have one free tier Amazon Linux 2 VM running right now, which serves a "fat" service (in a docker container) to myself and one other person. It has an Elastic IP associated with it.</p>
  
  <p>We use the service 1-2 hours every few days.</p>
  
  <p>Is there, in the rather large AWS zoo of services, a smart way to have the VM shut down when not in use, and come up when we need it? "Need it" could be measured by counting network traffic, i.e. I could in principle write a little script which uses <code>tcpdump</code> or something like that to watch for 15 minutes of no traffic on the port of the service and then somehow trigger a shutdown; but how would we get it up again?</p>
  
  <p>I am happy to change it in any way necessary; but the Docker image has to be considered a black box for all intents and purposes. There is one docker volume (realised as a directory in the home of the <code>ec2-user</code> right now) if that makes a difference. Timing is not critical.</p>
  
  <p>So far I'm doing shutdowns/startups through the EC2 Management Console, but would like to end that.</p>
  
Comments: CC BY-SA 3.0
Comments: 2018-04-19T01:30:54.610Z
Comments: Are you sure you want to shutdown the entire VM? You are correct in questioning how you would decide when to start the virtual machine. In most circumstances, you will always want at least 1 instance running to receive traffic from an outside source. From there, you can scale up as needed.
Comments: Preston Martin
Comments: CC BY-SA 3.0
Comments: 2018-04-19T06:49:47.223Z
Comments: No, I'm not sure about anything - i'm trying to find what AWS offers in this case; and maybe how to release the ressources I am not using most of the time. I'm aware that in a professional context, having a minimal amount of "control CPU" running 24/7 would be the thing to do...
Comments: AnoE
Comments: CC BY-SA 3.0
Comments: 2018-04-24T16:39:29.157Z
Comments: Isn't ECS an option to run this container ? I've the feeling it would be easier to start/stop it on demand but I may be wrong
Comments: Tensibai
Tags: <amazon-web-services><amazon-ec2><startup>
Title: Saving time on an EC2 instance
UserDisplayName: AnoE
ThreadText: # Saving time on an EC2 instance
  
  (Asked by: username_0 on 2018-04-18)
  
  I have started playing around with AWS (privately). I have one free tier Amazon Linux 2 VM running right now, which serves a "fat" service (in a docker container) to myself and one other person. It has an Elastic IP associated with it.
  
  We use the service 1-2 hours every few days.
  
  Is there, in the rather large AWS zoo of services, a smart way to have the VM shut down when not in use, and come up when we need it? "Need it" could be measured by counting network traffic, i.e. I could in principle write a little script which uses `tcpdump` or something like that to watch for 15 minutes of no traffic on the port of the service and then somehow trigger a shutdown; but how would we get it up again?
  
  I am happy to change it in any way necessary; but the Docker image has to be considered a black box for all intents and purposes. There is one docker volume (realised as a directory in the home of the `ec2-user` right now) if that makes a difference. Timing is not critical.
  
  So far I'm doing shutdowns/startups through the EC2 Management Console, but would like to end that.
  
  **Tags:** amazon-web-services, amazon-ec2, startup
  
  > **Comments:**
  > **username_1**: Are you sure you want to shutdown the entire VM? You are correct in questioning how you would decide when to start the virtual machine. In most circumstances, you will always want at least 1 instance running to receive traffic from an outside source. From there, you can scale up as needed.  
  > **username_0**: No, I'm not sure about anything - i'm trying to find what AWS offers in this case; and maybe how to release the ressources I am not using most of the time. I'm aware that in a professional context, having a minimal amount of "control CPU" running 24/7 would be the thing to do...  
  > **username_2**: Isn't ECS an option to run this container ? I've the feeling it would be easier to start/stop it on demand but I may be wrong  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-04-19. Score: 3
  
  AWS has something built in called [cloudwatch alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html) that can handle the basics.
  
  You could also use a Lambda Function. Give it access to the ASG, and then have cloudwatch configured so once the min amount of traffic starts coming in it sends an [Event](https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html) to the Lambda.
  
  ```
  {
    "ASG": "MyASGName"
    "Action": "Start"
  }
  
  ```
  
  And once the traffic goes below the min.
  
  ```
  {
    "ASG": "MyASGName"
    "Action": "Stop"
  }
  
  ```
  
  You could also build on this over time to control Vertical and Horizontal scaling.
  
  EDIT: I actually prefer the Lambda. I always seem to run into these weird use cases where we can't shut down any of the instances, we have to do some math or investigation on which instances to shut down. In these situations a python lambda running boto3 is really helpful.
  
  
  ---
  
  