Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
service-ip.sh
#!/bin/bash
AWS_COMMAND="aws --profile ion"
SERVICE="$1"
if [ "x$SERVICE" = "x" ]; then
SERVICE=bunsen
echo "INFO: Using default service [$SERVICE]"
fi
echo "Looking up service [$SERVICE]"
CLUSTERARN=$(${AWS_COMMAND} ecs list-clusters | jq -r '.clusterArns[]' | grep ion-channel-testing-IonChannelECSStack)
echo "CLUSTERARN=[$CLUSTERARN]"
SERVICEARN=$(${AWS_COMMAND} ecs list-services --cluster $CLUSTERARN | jq -r '.serviceArns[]' | grep "${SERVICE}service")
echo "SERVICEARN=[$SERVICEARN]"
# this can be multiple tasks
TASKARNS=$(${AWS_COMMAND} ecs list-tasks --cluster $CLUSTERARN --service-name $SERVICEARN | jq -r '.taskArns[]' | xargs)
echo "TASKARNS=[$TASKARNS]"
# needs more debug, but should be able to send a space-separated list of task IDs
#--cluster $CLUSTER
#${AWS_COMMAND} ecs describe-tasks --cluster $CLUSTERARN --tasks $TASKARNS
#| jq -r '.tasks[].containerInstanceArn')
for TASKARN in $TASKARNS; do
CONTAINERINSTANCEARN=$(${AWS_COMMAND} ecs describe-tasks --cluster $CLUSTERARN --tasks $TASKARN | jq -r '.tasks[].containerInstanceArn')
echo "CONTAINERINSTANCEARN=[$CONTAINERINSTANCEARN]"
EC2_INSTANCE_ID=$(${AWS_COMMAND} ecs describe-container-instances --cluster $CLUSTERARN --container-instances $CONTAINERINSTANCEARN | jq -r '.containerInstances[].ec2InstanceId')
echo "EC2_INSTANCE_ID=[$EC2_INSTANCE_ID]"
EC2_PRIVATE_IPV4=$(${AWS_COMMAND} ec2 describe-instances --instance-ids $EC2_INSTANCE_ID | jq -r '.Reservations[].Instances[].PrivateIpAddress')
echo "EC2_PRIVATE_IPV4=[$EC2_PRIVATE_IPV4]"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment