ソース
Bashスクリプト(gist)
#!/bin/bash
set -eu
# Prerequisite
# - aws cli
# - session-manager-plugin
# - 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 $@ | egrep -q "prd|prod"; then
color=$red
elif echo $@ | egrep -q "stg|stage|staging"; 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
$cmd
}
main
使い方
# ダウンロードして実行します
wget https://gist.githubusercontent.com/yuki777/640cba3e0a68587c36165b8a87d25390/raw/0aa9e48bc1e29e9a5b7c888cc16067dcccd6cc0a/sssh
chmod 744 sssh
./sssh
# AWSプロファイルを選択します
Select aws profile.
1) default
2) foo-bar
#? 2
profile: foo-bar
# ECSクラスタを選択します
Select cluster.
1) dev-foo-bar-cluster 9) prod-foo-app-cluster
2) prd-foo-hoge-ad-cluster 10) stg-foo-app-cluster
3) prd-foo-hoge-cluster 11) stg-foo-hoge-cluster
4) prd-foo-piyo2021-ad-cluster 12) stg-foo-piyo2021-cluster
5) prd-foo-piyo2021-cluster 13) stg-foo-bar-cluster
6) prd-foo-bar-ad-cluster 14) stg-foo-tags-cluster
7) prd-foo-bar-cluster 15) hoge-foo-bar-cluster
8) prd-foo-tags-cluster
#? 1
cluster: dev-foo-bar-cluster
# ECSサービスを選択します
1) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1125-service
2) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1206-service
3) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1249-service
4) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1275-service
5) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1323-service
6) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1348-service
7) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1349-service
8) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1384-service
9) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1386-service
10) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1391-service
11) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1397-service
12) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1399-service
13) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1412-service
14) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1413-service
15) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1419-service
16) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-qa-1420-service
17) arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-service
#? 16
service: arn:aws:ecs:ap-northeast-1:************:service/dev-foo-bar-cluster/dev-foo-bar-qa-1420-service
# ECSタスクを選択します
1) arn:aws:ecs:ap-northeast-1:************:task/dev-foo-bar-cluster/********************************
#? 1
task: arn:aws:ecs:ap-northeast-1:************:task/dev-foo-bar-cluster/********************************
# ECSコンテナを選択します
1) container-foo
2) container-bar
3) container-hoge
4) container-piyo
#? 1
container: container-foo
# 実行されるコマンドです
aws ecs execute-command --profile foo-bar --cluster dev-foo-bar-cluster --container container-foo --task arn:aws:ecs:ap-northeast-1:************:task/dev-foo-bar-cluster/******************************** --interactive --command '/bin/sh'
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
# sessionが開始されて'/bin/sh'コマンドが実行されます
Starting session with SessionId: ecs-execute-command-*****************
/path/to/home # :)
参考
Amazon ECS で、Amazon EC2 または AWS Fargate で実行されているコンテナでのコマンドの実行が可能に
実行中のコンテナに乗り込んでコマンドを実行できる「ECS Exec」が公開されました
ecs - AWS CLI 1.20.36 Command Reference
↧