In AWS infrastructure, We create a lot of EC2 instances on demand and we tend to forget about the stopped instances considering that it is stopped and not being billed for.
But the fact is that Even the stopped instances would cost you a couple of pennies in various forms like
- Elastic IP Idle Usage
- Volume Usage
- EBS GP2 Usage etc.
Though it would not be much when it adds up to the number of instances It can increase your monthly infrastructure cost by 10%.
So it is indispensable that you keep track of the shutdown or stopped instances and terminate them
Please be warned that there is no way to recover the terminated instances in AWS. Termination means deletion so please be cautious and I recommend using
--dry-run
and--no-dry-run
commands for being safe side
In this article, we are going to see how to terminate the EC2 instance from AWS CLI. Single or multiple instances at the same time.
If you are new to AWS CLI and have not installed and tried it yet. Please follow this article to get it set up before proceeding.
How to terminate the running EC2 instance using AWS CLI
To terminate the instance using AWS CLI you must get hold of the instance ID of the instance. you can get the instance ID using the aws ec2 describe-instances
command. (this article could help you )
Once you know the instance Id you can execute the terminate-instance
command like this
# aws ec2 terminate-instances – instance-ids i-1234567890abcdef0
{
"TerminatingInstances": [
{
"InstanceId": "i-1234567890abcdef0",
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}
How to terminate Multiple EC2 instances using AWS CLI
In this section, we are going to see how to terminate multiple EC2 instances at the same time using the AWS CLI command
Terminate instances accepts multiple instance-ids at once. You can define N number of instance-ids
one after another with space
as a delimiter
# aws ec2 terminate-instances – instance-ids i-0aac8d3e627de822a i-00d47a0fedbb9c6f0
{
"TerminatingInstances": [
{
"InstanceId": "i-0aac8d3e627de822a",
"CurrentState": {
"Code": 48,
"Name": "terminated"
},
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
},
{
"InstanceId": "i-00d47a0fedbb9c6f0",
"CurrentState": {
"Code": 48,
"Name": "terminated"
},
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
}
]
}
AWS CLI Command to List the Stopped instances with the date of Shutdown.
The following aws cli command would list the stopped instances for you
aws ec2 describe-instances
--filters "Name=instance-state-code,Values=80"
--query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, InstanceId, PublicIpAddress, State.Name, StateTransitionReason]'
--output table
It would print the instance name, instance id, Public IP, Current state (stopped) and the date when it was shut down and why
List the Stopped instances which are shut down by filtering the year
Let's suppose you want to get rid of the instances which are stopped from 2011 to 2019. you can write your aws ec2 cli query like this and it would find the instances for you
aws ec2 describe-instances
--filters "Name=instance-state-code,Values=80" "Name=reason,Values=*2011*,*2012*,*2013*,*2014*,*2015*,*2016*,*2017*,*2018*,*2019*"
--query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, InstanceId, PrivateIpAddress, State.Name, StateTransitionReason]'
--output table
Here we are explicitly defining years in the filter you can add or remove the years based on your need and create different filters of your own like by month, year etc.
Terminate the EC2 instances from the output of Describe-instance ( Dryrun First )
Having found the list of instances already you can terminate all of them which is our objective. You can write nested AWS CLI Query like this and get it done
aws ec2 terminate-instances
--dry-run
--instance-ids `aws ec2 describe-instances – filters "Name=instance-state-code,Values=80" "Name=reason,Values=*2011*,*2012*,*2013*,*2014*,*2015*,*2016*,*2017*,*2018*,*2019*" – query 'Reservations[].Instances[].[InstanceId]' – output text|tr '\n' ' '`
You can simply copy your list command use it with in the `
quotes
Another way of doing this in bash while loop is this
aws ec2 describe-instances – filters "Name=instance-state-code,Values=80" "Name=reason,Values=*2011*,*2012*,*2013*,*2014*,*2015*,*2016*,*2017*,*2018*,*2019*" – query 'Reservations[].Instances[].[InstanceId]' – output text|while read line
do
echo "terminating the instance with id $line"
aws ec2 terminate-instances – instance-ids $line – dry-run
done
Both these preceding methods have the --dry-run
reasons for safety reasons and to help anyone who is in a rush to copy and execute this snippet. Since this is dry run the termination would not actually happen. It would dry run successfully if you have the necessary permission to terminate the instances. If not it would present an error.
If you are confident now and want to terminate the instances. remove the
--dry-run
flag from the preceding snippet and you can use it.
Once you have issued the command for termination without --dry-run
or --no-dry-run
option you would get JSON output like given below.
{
"TerminatingInstances": [
{
"InstanceId": "i-0aac8d3e627de822a",
"CurrentState": {
"Code": 48,
"Name": "terminated"
},
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
},
{
"InstanceId": "i-00d47a0fedbb9c6f0",
"CurrentState": {
"Code": 48,
"Name": "terminated"
},
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
},
Hope this article helps you save a couple of pennies in your monthly infrastructure cost and teaches you how to terminate EC2 instances with AWS CLI.
Cheers
Sarav AK
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content