How to delete autoscaling groups with aws cli? - amazon-web-services

I am trying to write a bash script that will delete my EC2 instances and the auto scaling group that launched them:
EC2s=$(aws ec2 describe-instances --region=eu-west-3 \
--filters "Name=tag:Name,Values=*-my-dev-eu-west-3" \
--query "Reservations[].Instances[].InstanceId" \
--output text)
for id in $EC2s
do
aws ec2 terminate-instances --region=eu-west-3 --instance-ids $id
done
aws autoscaling delete-auto-scaling-group --region eu-west-3 \
--auto-scaling-group-name my-asg-dev-eu-west-3
But it fails with this error:
An error occurred (ResourceInUse) when calling the DeleteAutoScalingGroup operation:
You cannot delete an AutoScalingGroup while there are instances or pending Spot
instance request(s) still in the group.
There is no issue if I use the AWS console to do the same thing. Why does the aws cli prevent me from deleting the ASG if I have terminated all the instances?

if you really want to do this with CLI, you may first want to use aws autoscaling suspend-processes command to prevent ASG from creating new instances. Then use aws ec2 terminate-instances like you are doing. Then use aws ec2 wait instance-terminated command and pass instance ids. Once all that is done, you should be able use aws autoscaling delete-auto-scaling-group

aws ec2 terminate-instances will return before the instances have finished terminating (which could take several minutes).
I highly recommend using something like CloudFormation or Terraform for this sort of thing instead of the AWS CLI tool.

You can force delete the ASG with active spot instance requests with AWS cli:
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name Your-ASG-Name --force-delete

Related

How do I upgrade my AWS EC2 instance using the AWS CLI?

I would like to increase the number of CPU cores in my EC2 instance, using the AWS CLI.
My instance is currently a C5.4 Large.
I don't know the command for this, and I don't know if I have to know the instance type I want to switch to beforehand, or if I can browse different instance types from the AWS CLI.
aws ec2 help lists the commands describe-instances, stop-instances, modify-instance-attribute, and start-instances.
Calling aws ec2describe-instances may provide too much information. You can select the fields you would like with the --query option. --query takes a "JMESPath", which stands for "JSON Matching Expression paths" - a set of special syntaxes for getting values out of complicated JSON.1
You need enough information to identify the instance, for example, its name and type. Start the JMESPath with Reservations[*].Instances[*], followed by [InstanceId, InstanceType, KeyName].2
Example:
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId, InstanceType, KeyName]' \
--region us-east-1
Copy the ID.
Stop the instance before upgrading it:
aws ec2 stop-instances \
--instance-ids "$INSTANCE_ID"
It can take 5-10 minutes for it to stop.
Change the instance type with modify-instance-attribute:
aws ec2 modify-instance-attribute \
--instance-id "$INSTANCE_ID" \
--instance-type "{\"Value\":\"$REQUESTED_TYPE\"}
Replace $REQUESTED_TYPE with the name of a type, for example, t2.small.3
There may be limitations on what kinds of instances are allowed for your account, so make sure it's an instance type you have access to.
Now, restart the instance and you're done:
aws ec2 start-instances \
--instance-ids "$INSTANCE_ID"
You'll have to wait a few minutes before the instance has finished booting up.
1 Here is a tutorial on using JMESPath.
2 Every time you call an ec2 command, it is important to specify the region with the option --region. If you don't query a specific region, you won't see your instances.
3 (The quotes are escaped with a backslash so your shell doesn't misinterpret them.)

Failed to terminate ec-2 instance: How to modify its 'disableApiTermination' attribute from EC2 Dashboard?

I opened a free tier instance for some practice.
I tried to terminate it, as I've done many times successfully.
But upon selecting Terminate instance from the dropdown;
I got the following error:
Failed to terminate the instance <instance id>
The instance '<instance id>' may not be terminated. Modify its 'disableApiTermination' instance attribute and try again.
Where can I find the disableApiTermination attribute?
According to the documentation
To disable termination protection for a running or stopped instance
Select the instance, and choose Actions, Instance Settings, Change Termination Protection.
Choose Yes, Disable.
Solution: you need to disable api termination protection by changing instance attribute, I'll demonstrate how to do it with aws api.
(documentation attached)
instance_id=$(aws ec2 describe-instances \
--filter "Name=tag:Name,Values=instance-name-example" \
--query "Reservations[].Instances[].InstanceId[]" \
--output text)
aws ec2 modify-instance-attribute --instance-id $instance_id --no-disable-api-termination
You can also enable/disable the instance termination by using aws cli.
To enable protection:
aws ec2 modify-instance-attribute --instance-id <instance-id> --disable-api-termination
To disable protection:
aws ec2 modify-instance-attribute --instance-id <instance-id> --no-disable-api-termination

How to terminate multiple EC2 instances in AWS via CLI?

I'm looking for terminating multiple EC2 instances via AWS CLI. Yes, can able to terminate an EC2 instance by executing the below command.
Syntax:
aws ec2 terminate-instances --instance-ids <intance id> --profile <profile name>
Example:
aws ec2 terminate-instances --instance-ids <i-...> --profile xxx
But I have a big list of instances that I need to terminate so I'm searching for a solution to terminating a batch of EC2 instances by providing the list of instance ids. I tried with multiple instance ids as below but those not working.
aws ec2 terminate-instances --instance-ids ("instance-id1", "intance-id2") --profile xxx
aws ec2 terminate-instances --instance-ids ("instance-id1intance-id2") --profile xxx
aws ec2 terminate-instances --instance-ids (instance-id1,intance-id2) --profile xxx
Kindly let me know if there is any possibility to terminate a batch of instances.
I can able to achieve this by following the below command as recommended by luk2302
aws ec2 terminate-instances --instance-ids instance-id1 instance-id2 --profile xxx
Also as recommended by Alex Bailey, we can try with the shell script (.sh) or batch (.bat) which will make our job easier.
Instead of running all the instance ID's through at once I would create a loop in a shell script to do this.
Assuming you have each instance ID on a separate line in a text file you could do something like:
#!/usr/bin/env bash
while read ins_id; do
aws ec2 terminate-instances --instance-ids $ins_id --profile <profile name> || echo "error terminating ${ins_id}"
done < instance_ids.txt
That's not tested and I'm not great with shell scripting so if you try using it just try with one or two instances first and see what happens.

Is it possible to create and Auto Scaling Group Launch config with the CLI and define the instance tags in one command?

Is it possible to create and Auto Scaling Group Launch config with the CLI and define the instance tags in one command?
Maybe I am missing something but right now it looks like have to do it in two steps.
i.e.
aws autoscaling create-launch-configuration ...
and then
aws autoscaling create-or-update-tags --tags ...
Since you need to have asg LC created first to tag it, it is two step process as you mentioned.
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/create-launch-configuration.html
This example creates a launch configuration based on an existing instance. In addition, it also specifies launch configuration attributes such as a security group, tenancy, Amazon EBS optimization, and a bootstrapping script:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/autoscaling-tagging.html
aws autoscaling create-launch-configuration --launch-configuration-name my-launch-config --key-name my-key-pair --instance-id i-7e13c876 --security-groups sg-eb2af88e --instance-type m1.small --user-data file://myuserdata.txt --instance-monitoring Enabled=true --no-ebs-optimized --no-associate-public-ip-address --placement-tenancy dedicated --iam-instance-profile my-autoscaling-role
aws autoscaling create-or-update-tags --tags "ResourceId=my-asg,ResourceType=auto-scaling-group,Key=environment,Value=test,PropagateAtLaunch=true"

Finding EC2 status using EC2 API

Is there any way to find out status of AWS EC2 instances, which are running on various different regions, from one EC2 instance which is present in any one of region by using EC2 API tool ?
How this is possible ?
I got the answer :-
ec2-describe-instances instance-ID --region region
Example :-
ec2-describe-instances i-f82d5ca0 --region eu-west-1
Where instance ID is EC2 instance ID which is located in region eu-west-1
Thats all .
Or in the new unified AWS CLI, this is slightly different:
aws ec2 describe-instances --instance-id i-f82d5ca0
You can also change the --output into json, text, or a table