Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #!/bin/bash
  2. # ecsssh
  3. #
  4. # Given an ECS service, ssh to any EC2 instance running it.
  5.  
  6. set -e
  7. set -o pipefail
  8.  
  9. cluster=$(
  10. aws ecs list-clusters | jq -r '.clusterArns[]' \
  11. | fzf -0 --prompt 'cluster> ')
  12.  
  13. service=$(
  14. aws ecs list-services --cluster ${cluster} | jq -r '.serviceArns[]' \
  15. | cut -d: -f6 | fzf -0 --prompt 'service> ' | tr '/' ':')
  16.  
  17. # Any EC2 instances is good, so just pick the first one.
  18. c_instance=$(
  19. aws ecs list-container-instances --cluster ${cluster} \
  20. --filter "task:group =~ ${service}" \
  21. | jq -r '.containerInstanceArns[0]')
  22.  
  23. instance_id=$(
  24. aws ecs describe-container-instances --cluster ${cluster} \
  25. --container-instances "${c_instance}" \
  26. | jq -r '.containerInstances[].ec2InstanceId')
  27.  
  28. private_ip=$(
  29. aws ec2 describe-instances --output json \
  30. --filters "Name=instance-id,Values=${instance_id}" \
  31. --query 'Reservations[].Instances[]' \
  32. | jq -r '.[].PrivateIpAddress')
  33.  
  34.  
  35. if [ "$1" = "--show" ] ; then
  36. echo ${private_ip}
  37. else
  38. ssh ${private_ip}
  39. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement