ECS exec๋ ๋ถํธํ๊ณ , ๋ก๊ทธ๋ฅผ ๋ณด๋๊ฒ๋ ๋ถํธํ๊ณ ec2 ๋งค๋ฒ ์ฐพ์๋ค์ด๊ฐ๋๊ฒ๋ ๊ท์ฐฎ๊ณ ํ๋ค์ด์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค์๋ค..
๋ชจ๋์๊ฒ ๋์์ด ๋๊ธฐ๋ฅผ... ๐ฅฒ
Step 1 ) ECS ์๋น์ค ์ค์
๊ฐ์ฅ ๋จผ์ , enableExecuteCommand ๊ฐ ํ์ฑํ ๋์ด์์ด์ผ ํ๋ค.
aws ecs update-service --cluster ${ํด๋ฌ์คํฐ์ด๋ฆ} --services ${์๋น์ค์ด๋ฆ} --enable-execute-command true
enableExecuteCommand ํ์ฑํ ํ์๋ ๋ญ๊ฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค๋ฉด ์ด๊ฒ์ ์คํํด๋ณด์ (amazon-ecs-exec-checker)
Step2) ๋ก์ปฌ PC์ awscli, Session Manager ์ค์น
์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ธฐ์ํด์๋ awscli, session Manager Plugin์ด ์ค์น๋์ด์์ด์ผ ํ๋ค.
* AWS CLI: https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html
* AWS Session Manager Plugin: https://docs.aws.amazon.com/ko_kr/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html
Step3) ์คํฌ๋ฆฝํธ ์คํ
์คํฌ๋ฆฝํธ๋ ๋งจ ์๋์ค์ ์์ผ๋ ์คํฌ๋กค ๋ค์ดํ๊ณ ๋ก์ปฌPC์ ๋ณต๋ถํ๋ค. ๊ทธ๋ฅ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ฉด ๋๋ค.
sh ecs.sh
์คํฌ๋ฆฝํธ ์ค๋ช
sh ecs.sh ${command} ${profile_name} ${region} ${cluster} ${service}
[์คํฌ๋ฆฝํธ]
#!/bin/bash
set -euo pipefail
export AWS_DEFAULT_OUTPUT=json
#### Colors for output
COLOR_DEFAULT='\033[0m'
COLOR_RED='\033[0;31m'
COLOR_YELLOW='\033[1;33m'
COLOR_GREEN='\033[0;32m'
COLOR_CLEAR='\033[0m'
#### Functions
printSectionHeaderLine() {
printf "${COLOR_DEFAULT}---------------------------------------------------------------------\n"
}
#### Args
# sh ./ecs.sh \${command} \${profile_name} \${region} \${cluster} \${service}
COMMAND=${1:-None}
PROFILE_NAME=${2:-None}
REGION=${3:-None}
ECS_CLUSTER=${4:-None}
ECS_SERVICE=${5:-None}
# ์คํํ ๋ช
๋ น์ด ์ ํ
if [[ "${COMMAND}" = "None" ]]; then
AWS_CLI_VERSION=$(aws --version | cut -d " " -f1 | cut -d "/" -f2)
SESSION_PLUGIN_VERSION=$(/usr/local/bin/session-manager-plugin --version)
printf "
ECS๋ฅผ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๋ง๋ค์ด์ง ์คํฌ๋ฆฝํธ์
๋๋ค.
์์ ๋กญ๊ฒ ์์ ํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์คํฌ๋ฆฝํธ๊ฐ ์
๊ทธ๋ ์ด๋ ๋์๋ค๋ฉด ๊ณต์ ํด์ฃผ์ธ์. \n
๐จ ์ฌ์ ์กฐ๊ฑด \n
1) AWS CLI ๋ฒ์ 1.22.3 ์ด์ ๋๋ AWS CLI ๋ฒ์ 2.3.6 ์ด์ ์ฌ์ฉ
ํ์ฌ ๋ก์ปฌ์ ์ค์น๋์ด์๋ AWS CLI ๋ฒ์ : ${COLOR_YELLOW}$AWS_CLI_VERSION ${COLOR_CLEAR}
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
2) AWS CLI์ ๋ํ Session Manager ํ๋ฌ๊ทธ์ธ ์ค์น \n\
ํ์ฌ ๋ก์ปฌ์ ์ค์น๋์ด์๋ Session Manager ํ๋ฌ๊ทธ์ธ ๋ฒ์ : ${COLOR_YELLOW}$SESSION_PLUGIN_VERSION ${COLOR_CLEAR}
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html\n
๐จ ์ฌ์ฉ๋ฐฉ๋ฒ \n
1) ์ปจํ
์ด๋ bash ์คํ: ์ปจํ
์ด๋์ ๋ฐ๋ก bash๋ฅผ ์คํํฉ๋๋ค.
${COLOR_GREEN}sh ecs.sh exec \${profile_name} \${region} \${cluster} \${service}${COLOR_CLEAR} ๋ฅผ ์
๋ ฅํฉ๋๋ค.
2) ์ปจํ
์ด๋ ๋ก๊ทธ ํ์ธ: ์ปจํ
์ด๋ ๋ก๊ทธ๋ฅผ tail ๋ก ์คํํฉ๋๋ค.
${COLOR_GREEN}sh ecs.sh log \${profile_name} \${region} \${cluster} \${service}${COLOR_CLEAR} ๋ฅผ ์
๋ ฅํฉ๋๋ค.
3) ์ปจํ
์ด๋ ์ธ์คํด์ค ์ ์: ์ปจํ
์ด๋๊ฐ ์ด์๋๊ณ ์๋ ec2์ ๋ฐ๋ก ์ ์ํฉ๋๋ค.
${COLOR_GREEN}sh ecs.sh ec2 \${profile_name} \${region} \${cluster} \${service}${COLOR_CLEAR} ๋ฅผ ์
๋ ฅํฉ๋๋ค.
"
printSectionHeaderLine
printf "[์คํ ๋ช
๋ น์ด ๋ชฉ๋ก]
${COLOR_GREEN}exec ${COLOR_CLEAR} ์ปจํ
์ด๋์ bash ์คํ
${COLOR_GREEN}log ${COLOR_CLEAR} ์ปจํ
์ด๋ tail log ํ์ธ
${COLOR_GREEN}ec2 ${COLOR_CLEAR} ์ปจํ
์ด๋ ํธ์คํธ์ ์ ์\n"
printSectionHeaderLine
PS3='์คํํ ๋ช
๋ น์ด ๋ฒํธ๋ฅผ ์ ํํ์ธ์:'
select COMMAND in "exec" "log" "ec2"
do
# echo "The one you have selected is: $ECS_CLUSTER"
break
done
fi
if [[ "${COMMAND}" = "exec" ]]; then
# ์ฌ์ฉ ๋ฐฉ๋ฒ ์๋ด
if [[ "${PROFILE_NAME}" = "None" ]]; then
AWS_CLI_VERSION=$(aws --version | cut -d " " -f1 | cut -d "/" -f2)
SESSION_PLUGIN_VERSION=$(/usr/local/bin/session-manager-plugin --version)
printf "
---------------------------------------------------------------------
#### ##### ##### #### #### ### #### #####
## ## ## ## ## ## ## ## # ## ## ## ##
###### ## #### ###### #### ###### ##
## ## #### ## ### ## ##
## ## # # # ## # ## ## ## #
#### #### ##### #### ### ### #### #### "
fi
# ์ฌ์ฉํ ํ๋กํ ์ ํ
if [[ "${PROFILE_NAME}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Profile ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:default]: " PROFILE_NAME
PROFILE_NAME=${PROFILE_NAME:-default}
printf "${COLOR_GREEN} - AWS Profile: "$PROFILE_NAME
fi
# ๋ฆฌ์ ์ ํ
if [[ "${REGION}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Region ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:ap-northeast-2]: " REGION
REGION=${REGION:-ap-northeast-2}
printf "${COLOR_GREEN} - AWS Profile: "$REGION
fi
# ECS Cluster ์ ํ
if [[ "${ECS_CLUSTER}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
CLUSTER_LIST=$(aws ecs list-clusters --profile $PROFILE_NAME --region $REGION | jq '.clusterArns[]' | cut -d '/' -f2 | cut -d '"' -f1)
PS3='ํด๋ฌ์คํฐ ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_CLUSTER in $CLUSTER_LIST
do
# echo "The one you have selected is: $ECS_CLUSTER"
break
done
printf "${COLOR_GREEN} - ECS Cluster: "$ECS_CLUSTER
fi
# ECS Service ์ ํ
if [[ "${ECS_SERVICE}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
SERVICE_LIST=$(aws ecs list-services --cluster $ECS_CLUSTER --profile $PROFILE_NAME --region $REGION | jq '.serviceArns[]' | cut -d '/' -f3 | cut -d '"' -f1)
PS3='์๋น์ค ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_SERVICE in $SERVICE_LIST
do
# echo "The one you have selected is: $ECS_SERVICE"
break
done
printf "${COLOR_GREEN} - ECS Service: "$ECS_SERVICE
fi
# ECS Task ์ ํ
printf "\n"
printSectionHeaderLine
TASK_LIST=$(aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_SERVICE --profile $PROFILE_NAME --region $REGION | jq '.taskArns[]' | cut -d '/' -f3 | cut -d '"' -f1)
TASK_COUNT=$(aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_SERVICE --profile $PROFILE_NAME --region $REGION | jq '.taskArns[]' | wc -l)
if [[ "${TASK_COUNT}" -gt 1 ]]; then
PS3='Task ID ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์: '
select ECS_TASK in $TASK_LIST
do
# echo "The one you have selected is: $task_list"
CONTAINER_NAME=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containers[].name' | tr -d '"')
break
done
printf "${COLOR_GREEN} - ECS task: "$ECS_TASK"\n - Container name: "$CONTAINER_NAME
else
ECS_TASK=${TASK_LIST}
CONTAINER_NAME=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containers[].name' | tr -d '"')
printf "${COLOR_GREEN} - ECS task: "$ECS_TASK"\n - Container name: "$CONTAINER_NAME
fi
# ์ ํํ ๋ชฉ๋ก ์ถ๋ ฅ ๋ฐ ์ปค๋ฉ๋ ์คํ
printf "\n"
printSectionHeaderLine
printf "${COLOR_GREEN} AWS Profile: $PROFILE_NAME\n${COLOR_GREEN} AWS region: $REGION\n${COLOR_GREEN} ECS Cluster: $ECS_CLUSTER\n${COLOR_GREEN} ECS Service: $ECS_SERVICE\n${COLOR_GREEN} ECS task: $ECS_TASK\n๐${COLOR_YELLOW}'sh ./ecs.sh $COMMAND $PROFILE_NAME $REGION $ECS_CLUSTER $ECS_SERVICE'\nECS $CONTAINER_NAME ์ปจํ
์ด๋์ bash๋ฅผ ์คํํฉ๋๋ค\n${COLOR_CLEAR}"
aws ecs execute-command --cluster $ECS_CLUSTER --task $ECS_TASK --container $CONTAINER_NAME --profile $PROFILE_NAME --region $REGION --interactive --command "/bin/bash"
elif [[ "${COMMAND}" = "ec2" ]]; then
# ์ฌ์ฉ ๋ฐฉ๋ฒ ์๋ด
if [[ "${PROFILE_NAME}" = "None" ]]; then
printf "
---------------------------------------------------------------------
####### ###### #####
#### ##### ##### ## # ## ## ## ##
## ## ## ## ## ## ## # ##
###### ## #### ##### ## ###
## ## #### ## ## ###
## ## # # # ## # ## ## ## ##
#### #### ##### ####### ##### ####### "
fi
# ์ฌ์ฉํ ํ๋กํ ์ ํ
if [[ "${PROFILE_NAME}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Profile ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:default]: " PROFILE_NAME
PROFILE_NAME=${PROFILE_NAME:-default}
printf "${COLOR_GREEN} - AWS Profile: "$PROFILE_NAME
fi
# ๋ฆฌ์ ์ ํ
if [[ "${REGION}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Region ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:ap-northeast-2]: " REGION
REGION=${REGION:-ap-northeast-2}
printf "${COLOR_GREEN} - AWS Profile: "$REGION
fi
# ECS Cluster ์ ํ
if [[ "${ECS_CLUSTER}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
CLUSTER_LIST=$(aws ecs list-clusters --profile $PROFILE_NAME --region $REGION | jq '.clusterArns[]' | cut -d '/' -f2 | cut -d '"' -f1)
PS3='ํด๋ฌ์คํฐ ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_CLUSTER in $CLUSTER_LIST
do
# echo "The one you have selected is: $ECS_CLUSTER"
break
done
printf "${COLOR_GREEN} - ECS Cluster: "$ECS_CLUSTER
fi
# ECS Service ์ ํ
if [[ "${ECS_SERVICE}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
SERVICE_LIST=$(aws ecs list-services --cluster $ECS_CLUSTER --profile $PROFILE_NAME --region $REGION | jq '.serviceArns[]' | cut -d '/' -f3 | cut -d '"' -f1)
PS3='์๋น์ค ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_SERVICE in $SERVICE_LIST
do
# echo "The one you have selected is: $ECS_SERVICE"
break
done
printf "${COLOR_GREEN} - ECS Service: "$ECS_SERVICE
fi
# ECS Task ์ ํ
printf "\n"
printSectionHeaderLine
TASK_LIST=$(aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_SERVICE --profile $PROFILE_NAME --region $REGION | jq '.taskArns[]' | cut -d '/' -f3 | cut -d '"' -f1)
TASK_COUNT=$(aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_SERVICE --profile $PROFILE_NAME --region $REGION | jq '.taskArns[]' | wc -l)
if [[ "${TASK_COUNT}" -gt 1 ]]; then
PS3='Task ID ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์: '
select ECS_TASK in $TASK_LIST
do
# echo "The one you have selected is: $task_list"
CONTAINER_NAME=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containers[].name' | tr -d '"')
break
done
printf "${COLOR_GREEN} - ECS task: "$ECS_TASK"\n - Container name: "$CONTAINER_NAME
else
ECS_TASK=${TASK_LIST}
CONTAINER_NAME=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containers[].name' | tr -d '"')
printf "${COLOR_GREEN} - ECS task: "$ECS_TASK"\n - Container name: "$CONTAINER_NAME
fi
# ์ ํํ ๋ชฉ๋ก ์ถ๋ ฅ ๋ฐ ์ปค๋ฉ๋ ์คํ
printf "\n"
printSectionHeaderLine
printf "${COLOR_GREEN} AWS Profile: $PROFILE_NAME\n${COLOR_GREEN} AWS region: $REGION\n${COLOR_GREEN} ECS Cluster: $ECS_CLUSTER\n${COLOR_GREEN} ECS Service: $ECS_SERVICE\n${COLOR_GREEN} ECS task: $ECS_TASK\n๐${COLOR_YELLOW}'sh ./ecs.sh $COMMAND $PROFILE_NAME $REGION $ECS_CLUSTER $ECS_SERVICE'\nECS $CONTAINER_NAME ์ปจํ
์ด๋๊ฐ ์คํ์ค์ธ EC2์ ์ฐ๊ฒฐํฉ๋๋ค\n${COLOR_CLEAR}"
ECS_INSTANCE=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containerInstanceArn'| cut -d '/' -f3 | tr -d '"')
INSTANCE_ID=$(aws ecs describe-container-instances --cluster $ECS_CLUSTER --container-instances $ECS_INSTANCE --profile $PROFILE_NAME --region $REGION | jq '.containerInstances[].ec2InstanceId' | tr -d '"')
aws ssm start-session --target $INSTANCE_ID --profile $PROFILE_NAME --region $REGION
elif [[ "${COMMAND}" = "log" ]]; then
# ์ฌ์ฉ ๋ฐฉ๋ฒ ์๋ด
if [[ "${PROFILE_NAME}" = "None" ]]; then
printf "
---------------------------------------------------------------------
### ##
#### ##### ##### ## #### #####
## ## ## ## ## ## ## ## ## ##
###### ## #### ## ## ## #####
## ## #### ## ## ## ##
## ## # # # ## ## ## #####
#### #### ##### #### #### ## ## "
fi
# ์ฌ์ฉํ ํ๋กํ ์ ํ
if [[ "${PROFILE_NAME}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Profile ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:default]: " PROFILE_NAME
PROFILE_NAME=${PROFILE_NAME:-default}
printf "${COLOR_GREEN} - AWS Profile: "$PROFILE_NAME
fi
# ๋ฆฌ์ ์ ํ
if [[ "${REGION}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
read -p " AWS Region ์
๋ ฅํ์ธ์ [๊ธฐ๋ณธ๊ฐ:ap-northeast-2]: " REGION
REGION=${REGION:-ap-northeast-2}
printf "${COLOR_GREEN} - AWS Profile: "$REGION
fi
# ECS Cluster ์ ํ
if [[ "${ECS_CLUSTER}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
CLUSTER_LIST=$(aws ecs list-clusters --profile $PROFILE_NAME --region $REGION | jq '.clusterArns[]' | cut -d '/' -f2 | tr -d '"')
PS3='ํด๋ฌ์คํฐ ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_CLUSTER in $CLUSTER_LIST
do
# echo "The one you have selected is: $ECS_CLUSTER"
break
done
printf "${COLOR_GREEN} - ECS Cluster: "$ECS_CLUSTER
fi
# ECS Service ์ ํ
if [[ "${ECS_SERVICE}" = "None" ]]; then
printf "\n"
printSectionHeaderLine
SERVICE_LIST=$(aws ecs list-services --cluster $ECS_CLUSTER --profile $PROFILE_NAME --region $REGION | jq '.serviceArns[]' | cut -d '/' -f3 | tr -d '"')
PS3='์๋น์ค ๋ชฉ๋ก ํ์ธ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์:'
select ECS_SERVICE in $SERVICE_LIST
do
# echo "The one you have selected is: $ECS_SERVICE"
break
done
printf "${COLOR_GREEN} - ECS Service: "$ECS_SERVICE
fi
# ECS Task ์ ํ
printf "\n"
printSectionHeaderLine
ECS_TASK=$(aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_SERVICE --profile $PROFILE_NAME --region $REGION | jq '.taskArns[]' |head -1| cut -d '/' -f3 | tr -d '"')
CONTAINER_NAME=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].containers[].name' | tr -d '"')
printf "${COLOR_GREEN} - ECS task: "$ECS_TASK"\n - Container name: "$CONTAINER_NAME
# ์ ํํ ๋ชฉ๋ก ์ถ๋ ฅ ๋ฐ ์ปค๋ฉ๋ ์คํ
printf "\n"
printSectionHeaderLine
printf "${COLOR_GREEN} AWS Profile: $PROFILE_NAME\n${COLOR_GREEN} AWS region: $REGION\n${COLOR_GREEN} ECS Cluster: $ECS_CLUSTER\n${COLOR_GREEN} ECS Service: $ECS_SERVICE\n${COLOR_GREEN} ECS task: $ECS_TASK\n๐${COLOR_YELLOW}'sh ./ecs.sh $COMMAND $PROFILE_NAME $REGION $ECS_CLUSTER $ECS_SERVICE'\nECS $CONTAINER_NAME ์ปจํ
์ด๋์ ๋ก๊ทธ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.\n${COLOR_CLEAR}"
TASK_DEF=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $ECS_TASK --profile $PROFILE_NAME --region $REGION | jq '.tasks[].taskDefinitionArn'| tr -d '"')
TASK_LOG_GROUP=$(aws ecs describe-task-definition --task-definition $TASK_DEF --profile $PROFILE_NAME --region $REGION | jq '.taskDefinition.containerDefinitions[].logConfiguration.options."awslogs-group"'| tr -d '"')
printSectionHeaderLine
aws logs tail $TASK_LOG_GROUP --profile $PROFILE_NAME --region $REGION --follow --since 1m
fi