#!/usr/bin/env bash
id=j-3E40PX6SLJQ97
cluster=$(aws emr describe-cluster --cluster-id=$id)

instance_group=$(echo $cluster | jq -r '.Cluster.InstanceGroups[] | select(.InstanceGroupType == "CORE").Id')
instances=$(aws emr list-instances --cluster-id=$id --instance-group-id=$instance_group)
bootstrapping_instances=$(echo $instances | jq -r '.Instances[] | select(.Status.State == "BOOTSTRAPPING").Ec2InstanceId')
for i in $bootstrapping_instances; do
    aws ec2 modify-instance-attribute --instance-id $i --attribute disableApiTermination --value false
done
aws ec2 terminate-instances --instance-ids $bootstrapping_instances
