Sometimes you need to delete an EC2 instance but dont want to go into the AWS
web console. Most of the time you will have the internal DNS hostname of the instance
which would looks something like:
The AWS CLI doesnt let you terminate a node by a hostname. It requires the instance ID. So the first task is to find the instance ID and then terminate it
You can find the instance ID by describing an instance and filtering on the
aws ec2 describe-instances --filters "Name=private-dns-name,Values=ip-10-151-21-159.ec2.internal"
This will give you a big JSON output. Look for this key:
If you have jq installed, you can do this to rip it right out of the json:
aws ec2 describe-instances --filters "Name=private-dns-name,Values=ip-10-151-21-159.ec2.internal" | jq -r .Reservations.Instances.InstanceId
To terminate the instance, you simply run:
aws ec2 terminate-instances --instance-ids i-07a8839d40f993baa
Replacing it with your instance ID.
aws | cli | ec2