Skip to content

Instantly share code, notes, and snippets.

@acsrujan
Created August 9, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acsrujan/2c77429eab3b0702af9dc178aa96e0f4 to your computer and use it in GitHub Desktop.
Save acsrujan/2c77429eab3b0702af9dc178aa96e0f4 to your computer and use it in GitHub Desktop.
Cleans up terminated nodes from Chef server.. run it as cron on a server with both AWS access and Chef access, with knife installed.
#!/bin/bash
# Scan for terminated nodes on AWS and remove from Chef
cleanupNodes() {
# get nodes that are terminated via aws-cli
terminated_nodes=$(aws ec2 describe-instances --region=us-east-1 --filters Name=instance-state-name,Values=terminated | grep "InstanceId" | awk -F ":" '{print $2}' | awk -F '"' '{print $2}')
for node in $terminated_nodes;
do
knife client delete ${node} -y && knife node delete ${node} -y
done
}
#main
cleanupNodes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment