EC2 in ECS keeps restarting in Aws - amazon-ec2

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.

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.

Initiate EC2 Instance Shutdown via EstimatedCharges Threshold

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.

Ensure the availability of a pool of stopped ec2 instances

I want to maintain a pool of stopped amazon ec2 instances. Whenever the amount is below the threshold, I would like to be able to create new instances and then immediately stop them once they are running. Is this possible within the amazon infrastructure alone?
You can certainly create Amazon EC2 instances and then Stop them, making the available to Start later. As you point out, this has the benefit that instances will Start faster than they take to Launch a new instance.
There is no automated method to assist with this. You could have to code a solution that does the following:
Monitor the number of Stopped instances
If the quantity is below the threshold, launch a new instance
The new instance could automatically stop itself via User Data (either via a Shutdown command to the Operating System, or via a StopInstances call to EC2)
Some things you would have to consider:
What triggers the monitoring? Would it be on a schedule?
The task that launches a new instance would need to wait for the new instance to Launch & Stop before launching any more instances
What Starts the instances when they are needed?
Do instances ever get Stopped when they are no longer required?
The much better choice would be to use Auto Scaling, with a scale-out alarm based on some metric that says your fleet is busy, and a scale-in alarm to remove instances when the fleet is not busy. The scale-out alarm could be set to launch instances once a threshold is passed (eg 80% CPU) that should allow the new instance(s) to launch before things are 100% busy. The time difference between launching a new instance and starting an existing instance is quite small (at least for Linux).
If you're using Windows, the biggest time delay when launching a new instance is due to Sysprep, which makes a "clean" machine with new Unique IDs. You could cheat by creating an AMI without Sysprep, which would boot faster.
Perhaps I am misunderstanding your objective... you can't "ensure availability" of instances without paying for them.
Instances in the stopped state are only logical entities that don't physically exist anywhere -- hardware is allocated on launch, deallocated on stop, reallocated on the next start. In the unlikely condition where an availability zone is exhausted of capacity for given instance class, stopped instances of that class won't start, because there is no hardware available for them to be deployed onto.
To ensure that instances are always available, you have to reserve them, and you have to specify the reservations in a specific availability zone:
Amazon EC2 Reserved Instances provide a significant discount (up to 75%) compared to On-Demand pricing and provide a capacity reservation when used in a specific Availability Zone. [emphasis added]
https://aws.amazon.com/ec2/pricing/reserved-instances/
Under most plans, reserved instances are billed the same rate whether they are running or not, so there would be little point in stopping them.

How can I perform autoscaling in amazon ec2 instance?

I would like to know how can I perform autoscaling in amazon ec2 instance. Do amazon support vertical autoscaling or not?
Is there any option of memory based or CPU based autoscaling?
Thanks in advance
I would lik eto know if it can increase the CPU or memory based on user defined threshhold on a single instance?
No, it does not. Not at-least at the time of writing this. They might add that feature in the offing.
In current situation, the only way to ad more CPU/MEM to an instance is to shut it down and then change instance type. This option is available in AWS Console and I am not sure of APIs.
While changing the instance type, you can choose a bigger type of instance which will eventually get you more CPU/MEM.
There is no way to add more CPU/MEM to a running instance at the moment. In fact, there is no way to add CPU/MEM to a current instance without changing its instance type.
Autscaling does not do this either.

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