import boto3

def main(event, context):
  client = boto3.client(u'ecs')

  inspect_clusters = [u'staging1']

  for cluster in inspect_clusters:
    resp = client.list_container_instances(
      cluster=cluster
    )

    instances = resp[u'containerInstanceArns']

    try:
      nxt_tok = resp[u'nextToken']

      while True:
        resp = client.list_container_instances(
          cluster=cluster,
          nextToken=nxt_tok
        )

        instances += resp[u'containerInstanceArns']
        nxt_tok = resp[u'nextToken']
    except KeyError:
      pass

    resp = client.describe_container_instances(
      cluster=cluster,
      containerInstances=instances
    )

    ec2 = boto3.resource('ec2')
    client = boto3.client('autoscaling')

    for inst in resp[u'containerInstances']:
      if not inst['agentConnected']:
        I = ec2.Instance(id=inst[u'ec2InstanceId'])

        autoscalegroup = filter(lambda k: k['Key'] == u'aws:autoscaling:groupName', I.tags)[0]['Value']

        print I.id, u': ', autoscalegroup

import boto3

def main(event, context):
  ecs_client = boto3.client(u'ecs')

  inspect_clusters = [u'staging1']

  for cluster in inspect_clusters:
    resp = ecs_client.list_container_instances(
      cluster=cluster
    )

    instances = resp[u'containerInstanceArns']

    try:
      nxt_tok = resp[u'nextToken']

      while True:
        resp = ecs_client.list_container_instances(
          cluster=cluster,
          nextToken=nxt_tok
        )

        instances += resp[u'containerInstanceArns']
        nxt_tok = resp[u'nextToken']
    except KeyError:
      pass

    resp = ecs_client.describe_container_instances(
      cluster=cluster,
      containerInstances=instances
    )

    ec2 = boto3.resource('ec2')
    autoscale_client = boto3.client('autoscaling')

    for inst in resp[u'containerInstances']:
      if not inst['agentConnected']:
        I = ec2.Instance(id=inst[u'ec2InstanceId'])

        autoscalegroup = filter(lambda k: k['Key'] == u'aws:autoscaling:groupName', I.tags)[0]['Value']

        # Danger! Detaching Instance from autoscaling group
        autoscale_client.detach_instances(
          InstanceIds=[I.id],
          AutoScalingGroupName=autoscalegroup,
          ShouldDecrementDesiredCapacity=False
        )

        # Danger! Terminating Instance
        I.terminate()

        print u'Detaching and Terminating: ', I.id, u' in autoscale group ', autoscalegroup

Add instance detach and terminate
Add instance detach and terminate
