Skip to content

Instantly share code, notes, and snippets.

@mlf4aiur
Created December 2, 2016 09:45
Show Gist options
  • Select an option

  • Save mlf4aiur/cbd409f9c180ec3bcdaffa440950afde to your computer and use it in GitHub Desktop.

Select an option

Save mlf4aiur/cbd409f9c180ec3bcdaffa440950afde to your computer and use it in GitHub Desktop.
function get_ecs_service_instances () {
local cluster_name=$1
local service_name=$2
local tasks
local container_arns
local ec2_instance_ids
echo "Cluster: ${cluster_name}"
echo "Service: ${service_name}"
echo "EC2 instances:"
# Get tasks
tasks=$(aws --output json \
ecs list-tasks \
--cluster "${cluster_name}" \
--service-name "${service_name}" | \
jq '.taskArns[]' | sed 's@.*task/@@' | tr -d '"' | tr "\n" " ")
# Get container arn and host port
container_arn_ports=$(aws --output json \
ecs describe-tasks \
--cluster "${cluster_name}" \
--tasks ${tasks} | \
jq '.tasks[] | [.containerInstanceArn, .containers[].networkBindings[].hostPort | tostring] | join(" ")' | tr -d '"')
# Get EC2 instance ip and port
echo "${container_arn_ports}" | \
while read container_arn host_port
do
# get instance id
ec2_instance_id=$(aws --output json \
ecs describe-container-instances \
--cluster "${cluster_name}" \
--container-instances "${container_arn}" | \
jq '.containerInstances[].ec2InstanceId' | tr -d '"')
# get instance private ip
ec2_instance_private_ip=$(aws --output json \
ec2 describe-instances \
--instance-ids ${ec2_instance_id} | \
jq '.Reservations[].Instances[].PrivateIpAddress' | tr -d '"')
echo "${ec2_instance_private_ip}:${host_port}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment