Skip to content

Instantly share code, notes, and snippets.

@adiii717
Created July 3, 2020 02:28
Show Gist options
  • Save adiii717/fca85afe24f74a7259cf87ec058c00db to your computer and use it in GitHub Desktop.
Save adiii717/fca85afe24f74a7259cf87ec058c00db to your computer and use it in GitHub Desktop.
Find instance or instance ID of AWS ECS running TASK or Services
#!/bin/bash
CLUSTER_NAME=default
CONTAINER_INSTANCE="$(aws ecs list-container-instances --cluster $CLUSTER_NAME --query 'containerInstanceArns[]' --output text)"
for container in $CONTAINER_INSTANCE; do
TASK=$(aws ecs list-tasks --cluster $CLUSTER_NAME --container-instance $container --query 'taskArns[]' --output text)
EC2_INSTANCE_ID=$(aws ecs describe-container-instances --cluster prod-bgworker --container-instances $container --region us-west-2 --query 'containerInstances[*].ec2InstanceId' --output text)
echo "**************************************"
echo "ECS TASK having ARN is $TASK"
echo "Running in EC2 instance having ID $EC2_INSTANCE_ID"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment