ECS Exec를 사용한 컨테이너 모니터링

Jake·2025년 2월 20일

https://docs.aws.amazon.com/ko_kr/AmazonECS/latest/developerguide/ecs-exec.html

ECS Exec를 활성화 하여 컨테이너에 직접 명령을 전달하여 모니터링할 수 있습니다.

ECS Exec를 사용하기 전 AWS cliSession Manager Plugin 설치가 필요합니다.

ECS Service Exec 활성화

Service를 생성하거나 업데이트할 때 --enable-execute-command 옵션을 추가하여 Exce를 활성화 할 수 있습니다.

활성화 이후에 생성된 Task 부터 적용되기 때문에 --force-new-deployment 옵션을 적용했습니다.

aws ecs update-service \
  --cluster {클러스터명} \
  --service {서비스명} \
  --enable-execute-command \
  --force-new-deployment

Exec 활성화 여부 확인

아래 명령어로 Task의 Exec 활성화 여부를 확인할 수 있습니다.

aws ecs describe-tasks \
	--cluster {클러스터명} \
	--tasks {Task ID}

출력에 아래와 같이 managedAgents가 추가되었는지 확인합니다.

{
    "tasks": [
        {
            ...
            "containers": [
                {
                    ...
                    "managedAgents": [
                        {
                            "lastStartedAt": "2021-03-01T14:49:44.574000-06:00",
                            "name": "ExecuteCommandAgent",
                            "lastStatus": "RUNNING"
                        }
                    ]
                }
            ],
            ...
            "enableExecuteCommand": true,
            ...
        }
    ]
}

ECS Exec를 사용해 명령 실행

아래 명령어로 컨테이너에 접속해 명령어로 실행할 수 있습니다.

aws ecs execute-command \
	--cluster {클러스터명} \
  --task {Task ID} \
  --container {컨테이너명} \
  --interactive \
  --command "/bin/sh"

트러블 슈팅

에러 1

An error occurred (TargetNotConnectedException) when calling the ExecuteCommand operation: The execute command failed due to an internal error. Try again later. 에러 발생

다음과 같은 이유로 인해 이 오류가 발생할 수 있습니다.

  • Amazon ECS 작업 역할에는 execute-command 명령을 실행하는 데 필요한 권한이 없습니다.
  • AWS Identity and Access Management(IAM) 역할 또는 명령을 실행하는 사용자에게 필요한 권한이 없습니다.

해결 방법

Task definitions의 Task role에 아래 정책 추가

{
   "Version": "2012-10-17",
   "Statement": [
       {
       "Effect": "Allow",
       "Action": [
            "ssmmessages:CreateControlChannel",
            "ssmmessages:CreateDataChannel",
            "ssmmessages:OpenControlChannel",
            "ssmmessages:OpenDataChannel"
       ],
      "Resource": "*"
      }
   ]
}

https://repost.aws/ko/knowledge-center/ecs-error-execute-command

0개의 댓글