Initiate EC2 Instance Shutdown via EstimatedCharges Threshold - amazon-ec2

I am using amazon-ec2 and currently have a couple of CloudWatch Alarms set using the EstimatedCharges Threshold at different price ranges.
While my request here would obviously be a last/worst case situation, I am wondering how it would be possible to do this, if it even is.
What I am wanting to do is to setup an alarm that will (somehow, how??) initiate a Shutdown of a specific EC2 Instance when a specific alarm goes from a state of OK to a state of ALARM ?
I do not want to TERMINATE the instance, just a SHUTDOWN.
The idea here being making sure that a monthly bill does not all of a sudden go way beyond what can be afforded, even if it does mean shutting down the entire server.
Maybe there is another/better method of doing what I am after via another AWS service, if so, would love to know about that.

Related

How to use ELB and AutoScaling termination for long living connections

I want to set up Autoscaling groups where we can launch and terminate instances based on the CPU load. But usually our connections stays for long like more than 8hrs sometimes even more than that. When I use NLB, the Deregistration delay is only supported till 3600sec and after that NLB will forcefully remove the connection which cause our long living connections to fail and autoscaling will terminate the instances as well.
How do I make sure that all my connections to the target group is processed after 8-10hrs and then NLB deregister or autoscaling terminate the instance?
I checked the ASG Lifecycle hooks and it allows connections only till 2hrs.
Is it possible to deregister the instances in target group after all the connections are drained and terminate the instance using ASG?
There isn't any good/easy way to do what you want to do. What are the instances doing that can last up to 10 hours?
Depending on your work type, this is the best workaround I can think of, but it would probably involve a bit of rearchitecting.
1) design your application so that all data is stored off the instance is some sort of data tier (S3, RDS, EFS, etc). When an instance is done doing whatever its doing, save that info to the data tier. This way a user request can go to any instance and get the same information
2) The ASG decides to scale in
3) You have a lifecycle hook configured and a cloudwatch notification setup to be triggered when an instance enters the terminating:wait state which notifies the instance
4) The instance periodically sends a heartbeat to the lifecycle hook which can extend the hooks timeout for up to 2 days
5) Whenever the instance finishes what its doing, it saves the information out to the data tier mentioned in 1) and the client can connect to a new instance to get the information that was being processed on the old one
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/record-lifecycle-action-heartbeat.html
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/complete-lifecycle-action.html
Try to use, Scaling CoolDown period. By the default Scaling Cooldown Period is (300 Secs). you can increase the number. which will help to increase the scale in time.

EC2 in ECS keeps restarting in Aws

I have created an ECS cluster that has created an EC2 instance for me. Since it is in the dev phase, I would like to 'stop' the instance when not in use. But it restarts itself whenever I stop it manually.
I have a faint idea that it might be because of the ASG that tries to maintain it's desired state of 1 instances but how do I control this? If I edit the desired state in ASG to 0, it shuts down my instance altogether.
I just want to stop it when not in use from being unnecessarily billed.
Any help will be appreciated.
Thanks.
Setting Auto Scaling group's Desired Capacity to zero means terminating all of the instances and not stopping them. Setting it to one, one the other hand, means always keeping one instance running. So, it is not possible to have stopped instances in ASG. But you can schedule scaling for your ASG to save some money when you do not use your test machine. Read more about scheduled scaling here.

AWS: Alarm when I leave an EC2 instance running?

I playing around with EC2 on some non-free VM's. I'd like to set up an alarm that will email me every, say, 24 hours that a VM is running, to remind me in case I forget to shut one down. Can this be done?
I'm reasonably confident that it probably can be, but I have no idea how.
You can setup Alarm based on resource usage and send an email to you (or) shutdown. Here is documentation on how to Create Alarms to Stop, Terminate, Reboot, or Recover an Instance

Amazon Auto Scale is ping ponging since the health checks fail

I have my Auto Scaling Group configured like so:
My Load balancer, when it registers an instance, fails since the health check fails while the instance is still loading.
For what it's worth, my health check is as follows:
I've added the "web-master" instance myself, not part of the auto scale, since what normally happens is that the registering instance fails to add itself to the load balancer, terminates and a new one pops up. This happens countless times until I manually intervene. What am I doing wrong? Is there any way to delay the ELB Health check or at least have it wait till the instance is fully registered?
An instance shouldn't register until its passed its availability tests. Could it be that its your applications spin up time that's the issue, not the instance.
I guess my question is, is your application set up on the port your pinging? I have a lightweight 'healthcheck' app in iis for heathchecks to get around the 'ping pong' effect.

Change running ec2 instance type

Does amazon have the ability to ever offer a feature to allow users to change their ec2 instance types while the server is running? So like a t1.micro to a m1.large and not shut anything down. I know nothing about VMs or what would be involved, so I'm not sure if this is even possible, the level of difficulty (I'd assume difficult enough if they haven't rolled it out), and if there are any plans to do so.
No, instance type can not be changed while the instance is running. To change the instance type you must stop and, change the instance type and then start it.

Resources