Created
July 2, 2016 17:25
-
-
Save rynbrd/96b6e5a53ca87f4bd65b27689f0e25d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
def get_ecs_task_instance(task_arn): | |
"""Return the EC2 instance associated with the task.""" | |
ecs = boto3.client('ecs') | |
task = None | |
for cluster in ecs.list_clusters()['clusterArns']: | |
res = ecs.describe_tasks(cluster=cluster, tasks=[task_arn]) | |
if res['tasks']: | |
task = res['tasks'][0] | |
break | |
if not task: | |
return None | |
container_arn = task['containerInstanceArn'] | |
res = ecs.describe_container_instances(cluster=cluster, containerInstances=[container_arn]) | |
containers = res['containerInstances'] | |
if not containers: | |
return None | |
instance_id = containers[0]['ec2InstanceId'] | |
ec2 = boto3.client('ec2') | |
res = ec2.describe_instances(InstanceIds=[instance_id]) | |
if not res['Reservations']: | |
return None | |
if not res['Reservations'][0]['Instances']: | |
return None | |
return res['Reservations'][0]['Instances'][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment