-
-
Save naviocean/7b34b5ec3c6f65a10bb308570e109e4a to your computer and use it in GitHub Desktop.
aws ecs login script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -eu | |
| # Requirements | |
| # - aws cli | |
| # - session-manager-plugin | |
| # - github cli 2.0 | |
| # - jq | |
| selectProfile(){ | |
| select selected in `aws configure list-profiles` | |
| do | |
| break | |
| done | |
| echo $selected | |
| } | |
| selectCluster(){ | |
| select selected in `aws ecs list-clusters --profile $profile|jq -r ".clusterArns[]"|sort|cut -d "/" -f 2` | |
| do | |
| break | |
| done | |
| echo $selected | |
| } | |
| selectService(){ | |
| select selected in `aws ecs list-services --profile $profile --cluster $cluster|jq -r ".serviceArns[]"|sort` | |
| do | |
| break | |
| done | |
| echo $selected | |
| } | |
| selectTask(){ | |
| select selected in `aws ecs list-tasks --cluster $cluster --profile $profile --service-name $service --desired-status RUNNING |jq -r '.taskArns[]'|sort` | |
| do | |
| break | |
| done | |
| echo $selected | |
| } | |
| selectContainer(){ | |
| select selected in `aws ecs describe-tasks --cluster $cluster --tasks $task --profile $profile|jq -r ".tasks[].containers[].name"|sort` | |
| do | |
| break | |
| done | |
| echo $selected | |
| } | |
| colorEcho(){ | |
| red='\033[0;31m' | |
| green='\033[0;32m' | |
| yellow='\033[0;33m' | |
| reset='\033[0m' | |
| if echo $@ | grep -q "prd"; then | |
| color=$red | |
| elif echo $@ | grep -q "stg"; then | |
| color=$yellow | |
| else | |
| color=$green | |
| fi | |
| echo -e "${color}$@${reset}" | |
| } | |
| main(){ | |
| echo "Select aws profile." | |
| profile=`selectProfile` | |
| colorEcho profile: $profile | |
| echo | |
| echo "Select cluster." | |
| cluster=`selectCluster` | |
| colorEcho cluster: $cluster | |
| echo | |
| echo "Select service." | |
| service=`selectService` | |
| colorEcho service: $service | |
| echo | |
| echo "Select task." | |
| task=`selectTask` | |
| colorEcho task: $task | |
| echo | |
| echo "Select container." | |
| container=`selectContainer` | |
| colorEcho container: $container | |
| echo | |
| cmd="aws ecs execute-command --profile $profile --cluster $cluster --container $container --task $task --interactive --command '/bin/sh'" | |
| colorEcho $cmd | |
| # Run command | |
| $cmd | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment