๐ŸŒฑ Infra/Container_AWS_ECS

[ECS exec log ec2] ์Šคํฌ๋ฆฝํŠธ

mini_world 2022. 9. 15. 20:16
๋ชฉ์ฐจ ์ ‘๊ธฐ

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



728x90