https://docs.aws.amazon.com/ko_kr/AmazonECS/latest/developerguide/ecs-exec.html
ECS Exec를 활성화 하여 컨테이너에 직접 명령을 전달하여 모니터링할 수 있습니다.
ECS Exec를 사용하기 전 AWS cli와 Session Manager Plugin 설치가 필요합니다.
Service를 생성하거나 업데이트할 때 --enable-execute-command 옵션을 추가하여 Exce를 활성화 할 수 있습니다.
활성화 이후에 생성된 Task 부터 적용되기 때문에 --force-new-deployment 옵션을 적용했습니다.
aws ecs update-service \
--cluster {클러스터명} \
--service {서비스명} \
--enable-execute-command \
--force-new-deployment
아래 명령어로 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,
...
}
]
}
아래 명령어로 컨테이너에 접속해 명령어로 실행할 수 있습니다.
aws ecs execute-command \
--cluster {클러스터명} \
--task {Task ID} \
--container {컨테이너명} \
--interactive \
--command "/bin/sh"
An error occurred (TargetNotConnectedException) when calling the ExecuteCommand operation: The execute command failed due to an internal error. Try again later. 에러 발생
다음과 같은 이유로 인해 이 오류가 발생할 수 있습니다.
해결 방법
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