clear chef nodes: quick and dirty

Note: Intended for self reference.

We've to delete clients/nodes from chef when an ec2 instance terminates.

We've followed the pattern of using instance id as chef node id. And the following script helps to delete nodes that are terminated. The script runs every 15 minutes.

#!/bin/bash
# Scan for terminated nodes and remove from Chef

getTerminatedNodes() {
echo `date`
cd /home/srujan/chef-workstation-directory/

# get nodes that are terminated via aws-cli
nodes=$(aws ec2 describe-instances --region=us-east-1 --profile production --filters Name=instance-state-name,Values=terminated | grep "InstanceId" | awk -F ":" '{print $2}' | awk -F '"' '{print $2}')
 echo $nodes

# loop and have knife find them, then delete client and node
for node in $nodes;
 do
 knife client delete ${node} -y && knife node delete ${node} -y
 done

}

#main
getTerminatedNodes