Skip to content

Instantly share code, notes, and snippets.

@mims92
Created July 27, 2020 10:46
Show Gist options
  • Save mims92/033c35b1c7d1f7d2386c4fcb2a26ec33 to your computer and use it in GitHub Desktop.
Save mims92/033c35b1c7d1f7d2386c4fcb2a26ec33 to your computer and use it in GitHub Desktop.
AWS - Bash - Get tasks instances
#!/bin/bash
#chmod +x /usr/bin/awsecs
#Usage: awsecs <service-name> <profile name>
#Output: list of ec2 instance id
SERVICE="$1"
shift
PROFILE="$1"
shift
CLUSTER="<<<<<<YOUR CLUSTER NAME>>>>>>"
tasks_arn=$(aws ecs list-tasks \
--cluster "${CLUSTER}" \
--service-name "${SERVICE}" \
--profile "${PROFILE}" \
| jq -r -c ".taskArns | join(\" \")"\
)
container_instances_arn=$(aws ecs describe-tasks \
--cluster "${CLUSTER}" \
--tasks ${tasks_arn} \
--profile "${PROFILE}" | jq -r -c ".tasks[].containerInstanceArn" \
)
aws ecs describe-container-instances \
--cluster "${CLUSTER}" \
--container-instance ${container_instances_arn} \
--profile "${PROFILE}" \
| jq -r -c ".containerInstances[].ec2InstanceId"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment