Skip to content

Instantly share code, notes, and snippets.

@mlf4aiur
Last active December 2, 2016 09:43
Show Gist options
  • Select an option

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

Select an option

Save mlf4aiur/9be421de77fed3108f2a5d4824bea032 to your computer and use it in GitHub Desktop.
Get AWS ECS service instance IPs
function get_ecs_service_instance_ips () {
local cluster_name=$1
local service_name=$2
local tasks
local container_arns
local ec2_instance_ids
# 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 arns
container_arns=$(aws --output json \
ecs describe-tasks \
--cluster "${cluster_name}" \
--tasks ${tasks} | \
jq '.tasks[].containerInstanceArn' | tr -d '"' | tr "\n" " ")
# Get EC2 instance ids
ec2_instance_ids=$(aws --output json \
ecs describe-container-instances \
--cluster "${cluster_name}" \
--container-instances ${container_arns} | \
jq '.containerInstances[].ec2InstanceId' | tr -d '"' | tr "\n" " ")
# Get EC2 instance ips
ec2_instance_private_ips=$(aws --output json \
ec2 describe-instances \
--instance-ids ${ec2_instance_ids} | \
jq '.Reservations[].Instances[].PrivateIpAddress' | tr -d '"')
echo "Cluster: ${cluster_name}"
echo "Service: ${service_name}"
echo "ec2_instance_ids: ${ec2_instance_ids}"
echo "ec2_instance_private_ips:"
echo "${ec2_instance_private_ips}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment