Faily new to AWS however I am looking to terminate a set of ec2 instances using the AWS CLI by filtering by a Tag name.
If I use describe-instances
, I can filter
by tag:key=value . For terminate-instances
I don't see a way of filtering. I assume this is possible since I can filter and terminate using the AWS console but I am looking to do this via CLI.
I created the following script(.sh) and it worked for me:
Latest AWS CLI allows you to avoid the need for any scripts or jq:
as long as the number of expected instances is not huge, the above can be used.
The
terminate-instances
command only takes a list of instance IDs. You would need to write a script to run thedescribe-instances
command first and capture the instance IDs, then pass those IDs to theterminate-instances
command.