17.07.2019»»среда

Aws Auto Shutdown Ec2 Instance

17.07.2019
    98 - Comments
  1. Auto Shutdown Windows 7

I wish to setup a scheduled shutdown 'policy' of EC2 instances in our lab environment every night. This is in case somebody in the team forgot. How would I be able to automatically schedule an amazon EC2 Micro instance to stop? I haven't created an amazon account yet; I just want to know if I would be able to do it. For example, have the instance turn off from 1AM to 7AM and then back on again. I'm planning to host a minecraft server on it, I'm not sure if micro might be too small.

I frequently fire up a temporary Ubuntu server on Amazon EC2 to test out some package feature, installation process, or other capability where I’m willing to pay a few pennies for a clean install and spare CPU.

I occasionally forget that I started an instance and leave it running for longer than I intended, turning my decision to spend ten cents into a cost of dollars. In one case, I ended up paying several hundred dollars for a super-sized instance I forgot I had running. Yes, ouch.

Because of this pain, I have a habit now of pre-scheduling the termination of my temporary instances immediately after creating them. I used to do this on the instance itself with a command like:

Pro

However, this only terminates the instance if its root disk is instance-store (S3 based AMI). I generally run EBS boot instances now, and a shutdown or halt only “stops” an EBS boot instance by default which leaves you paying for the EBS boot volume at, say, $1.50/month.

So, my common practice these days is to pre-schedule an instance termination call, generally from my local laptop, using a command like:

The at utility runs the commands on stdin with the exact same environment ($PATH, EC2 keys, current working directory, umask, etc.) as are in the current shell. There are a number of different ways to specify different times for the schedule and little documentation, but it will notify you when it plans to run the commands so you can check it.

After the command is run, at returns the output through an email. This gives you an indication of whether or not the terminate succeeded or if you will need to follow up manually.

Here’s an example email I got from the above at command:

I already have a personal custom command which starts an instance, waits for it to move to running, waits for the ssh server to accept connections, then connects with ssh. I think I’ll add a --temporary option to do this termination scheduling for me as well.

You can get a list of the currently scheduled at jobs with

You can see the commands in a specific job id with:

If you decide along the way that the instance should not be temporary and you want to cancel the scheduled termination, you can delete a given at job with a command like:

Automatically start and stop ec2 instance using tags

I’ve been thinking of writing something simple that would regularly monitor my AWS/EC2 resources (instances, EBS volumes, EBS snapshots, AMIs, etc.) and alert me if it detects patterns that may indicate I am spending money where I may not have intended to.

How do you monitor and clean up temporary resources on Amazon AWS/EC2?

01 Run describe-instances command (OSX/Linux/UNIX) using appropriate filtering to list the IDs of all the existing EC2 instances available in the selected region:

Auto Shutdown Windows 7

02 The command output should return a table with the requested instance IDs:

03 Run get-metric-statistics command (OSX/Linux/UNIX) to get the statistics recorded by AWS CloudWatch for the CPUUtilization metric representing the CPU usage of the selected EC2 instance. Change the --start-time (start recording date) and --end-time (stop recording date) parameters value to choose your own time frame for recording the resource CPU usage. Also, set the --period parameter value based on your requirements to define the granularity - in seconds - of the returned datapoints. A period can be as short as one minute (60 seconds) or as long as one day (86400 seconds). The following command example returns the average CPU usage of an EC2 instance identified by the ID i-07a371cacb4f19acd, usage data captured during a 7 days period (set by the --start-time and --end-time command parameters), using 1 hour period as the granularity of the returned datapoints (set by the --period parameter):

04 The command output should return the CPU usage details requested:


If the average CPU usage data returned is less than 2%, the selected EC2 instance qualifies as candidate for the idle instance.

05 Run again get-metric-statistics command (OSX/Linux/UNIX) to get the statistics recorded by AWS CloudWatch for the NetworkIn metric representing the incoming network traffic for the selected EC2 instance. The following command example returns the average incoming network traffic received by an EC2 instance identified by the ID i-07a371cacb4f19acd, usage data captured during a 7 days period (set by the --start-time and --end-time parameters), using 1 hour period as the granularity of the returned datapoints (set by the --period parameter):

06 The command output should return the instance network traffic data requested:


If the average incoming traffic values returned are less than 5 MB, the selected EC2 instance qualifies as candidate for the idle instance.

07 Run get-metric-statistics command (OSX/Linux/UNIX) to get the statistics recorded by AWS CloudWatch for the NetworkOut metric representing the outgoing network traffic for the selected EC2 instance. The following command example returns the average outgoing network traffic received by an EC2 instance identified by the ID i-07a371cacb4f19acd, usage data captured during a 7 days period (set by the --start-time and --end-time parameters), using 1 hour period as the granularity of the returned datapoints (set by the --period parameter):

08 The command output should return the instance network traffic data requested:


If the average outgoing traffic values returned are less than 5 MB, the selected EC2 instance qualifies as candidate for the idle instance.

09 Run describe-tags command (OSX/Linux/UNIX) to describe the tags for the selected EC2 instance.

10 The command output should return the tags (key-value pairs) applied to the instance. The Role and Owner tags returned and their values (highlighted) can be used to determine the resource role within the application stack and to contact its owner for more information in order to decide whether the EC2 instance can be shutdown/terminated or not:


If the data returned for the steps no. 3 - 10 satisfy the conditions set by the conformity rule (instance role, instance owner, CPU and Network In + Network Out usage), the selected EC2 instance is considered 'idle' and can be stopped or terminated in order to reduce AWS EC2 usage costs.

11 Repeat steps no. 3 - 10 to verify the CPU usage, the Network In/Out traffic and the role for the rest of the EC2 instances provisioned in the current region.

12Change the AWS region by updating the --region command parameter value and repeat steps no. 1 - 11 to perform the audit process for other regions.