AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何疑難排解在 Fargate 任務中使用 ECS Exec 時發生的錯誤?
我想要疑難排解在 AWS Fargate 任務中使用 Amazon Elastic Container Service (Amazon ECS) Exec 時收到的錯誤。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
在 Fargate 任務中使用 ECS Exec 時,您可能會收到以下其中一則錯誤訊息:
- 「An error occurred (InvalidParameterException) when calling the ExecuteCommand operation: The execute command failed because execute command was not enabled when the task was run or the execute command agent isn't running.Wait and try again or run a new task with execute command enabled and try again.」
- 「An error occurred (TargetNotConnectedException) when calling the ExecuteCommand operation: The execute command failed due to an internal error.Try again later.」
最佳實務是使用 AWS CloudShell 疑難排解 Fargate 任務中的 ECS Exec。CloudShell 已預先安裝 AWS Systems Manager Agent (SSM Agent) 和 AWS CLI。
InvalidParameterException 錯誤
如果您的 Fargate 任務已停用 ExecuteCommand 選項,您會收到 InvalidParameterException 錯誤。
若要解決此問題,請完成以下步驟:
-
若要檢查 enableExecuteCommand 參數設定為 true 或 false,請執行 describe-tasks 命令:
aws ecs describe-tasks --cluster example-cluster-name --tasks example-task-id| grep enableExecuteCommand**注意:**將 example-cluster-name 替換為您的叢集,並將 example-task-id 替換為您的任務 ID。
-
如果 enableExecuteCommand 參數為 false,請執行以下 update-service 命令,將此參數更新為 true:
aws ecs update-service --cluster example-cluster-name --service example-service --region example-region --enable-execute-command --force-new-deployment**注意:**將 example-cluster-name 替換為您的叢集、將 example-service 替換為您的服務,並將 example-region 替換為您的 AWS 區域。force-new-deployment 選項會依服務的部署組態建立新部署,啟動新任務並停止先前的任務。如果您的服務使用 AWS CodeDeploy 藍/綠部署,請啟動 CODE_DEPLOY 部署,而不要使用 force-new-deployment。您無法將 force-new-deployment 用於藍/綠部署,因為強制部署會啟動滾動更新。
-
若要檢查 ExecuteCommandAgent 的狀態,請執行以下 describe-tasks 命令:
aws ecs describe-tasks --cluster example-cluster-name --tasks example-task-id | grep -A 6 managedAgents**注意:**將 example-cluster-name 替換為您的叢集,並將 example-task-id 替換為您的任務 ID。
-
檢查命令輸出中 ExecuteCommand 代理程式的狀態。如果 lastStatus 顯示 ExecuteCommandAgent 不是 RUNNING,請檢查 ExecuteCommandAgent 代理程式日誌以找出根本原因。繼續執行產生 ECS Exec 日誌以識別問題的疑難排解步驟,以產生 ExecuteCommandAgent 日誌。
如果您在容器中設定 Proxy,導致 ExecuteCommandAgent 無法擷取憑證,請將以下 NO_PROXY 選項新增至容器執行個體的組態檔案:
env no_proxy=169.254.169.254,169.254.170.2
TargetNotConnectedException 錯誤
若要解決 TargetNotConnectionException 錯誤,請採取以下措施。
新增必要權限並驗證網路組態
請完成以下步驟:
- 將必要權限新增至您的 Amazon ECS 任務 AWS Identity and Access Management (IAM) 角色。如果任務的 IAM 角色已有必要權限,請確認服務控制政策 (SCP) 未阻擋任務連線至 SSM Agent。
- 如果您搭配 Amazon ECS 使用 Amazon Virtual Private Cloud (Amazon VPC) 介面端點,請建立以下端點:
ec2messages.region.amazonaws.com
ssm.region.amazonaws.com
ssmmessages.region.amazonaws.com
**注意:**將 region 替換為您的 AWS 區域。 - 若要確認 AWS CLI 環境與 Amazon ECS 叢集或任務已可使用 ECS Exec,請執行 check-ecs-exec.sh 指令碼。check-ecs-exec.sh 指令碼的輸出會顯示您在使用 ECS Exec 前必須解決的問題。如需先決條件與使用方式的相關資訊,請參閱 GitHub 網站上的 Amazon ECS Exec Checker。
以下輸出範例顯示,任務已停用 ECS Exec,且任務角色缺少必要的 Systems Manager 權限:
**注意:**若要執行 ECS Exec,您必須在任務定義中將 ReadonlyRootFilesystem 參數設定為 false。如果 ReadonlyRootFileSystem 為 true,SSM Agent 將無法建立必要的目錄。Prerequisites for check-ecs-exec.sh v0.7 ------------------------------------------------------------- jq | OK (/usr/bin/jq) AWS CLI | OK (/usr/local/bin/aws) ------------------------------------------------------------- Prerequisites for the AWS CLI to use ECS Exec ------------------------------------------------------------- AWS CLI Version | OK (aws-cli/2.11.0 Python/3.11.2 Linux/4.14.255-291-231.527.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 prompt/off) Session Manager Plugin | OK (1.2.398.0) ------------------------------------------------------------- Checks on ECS task and other resources ------------------------------------------------------------- Region : us-east-1 Cluster: Fargate-Testing Task : ca27e41ea3f54fd1804ca00feffa178d ------------------------------------------------------------- Cluster Configuration | Audit Logging Not Configured Can I ExecuteCommand? | arn:aws:iam::12345678:role/Admin ecs:ExecuteCommand: allowed ssm:StartSession denied?: allowed Task Status | RUNNING Launch Type | Fargate Platform Version | 1.4.0 Exec Enabled for Task | NO Container-Level Checks | ---------- Managed Agent Status - SKIPPED ---------- ---------- Init Process Enabled (Exec-check:2) ---------- 1. Disabled - "nginx" ---------- Read-Only Root Filesystem (Exec-check:2) ---------- 1. Disabled - "nginx" Task Role Permissions | arn:aws:iam::12345678:role/L3-session ssmmessages:CreateControlChannel: implicitDeny ssmmessages:CreateDataChannel: implicitDeny ssmmessages:OpenControlChannel: implicitDeny ssmmessages:OpenDataChannel: implicitDeny VPC Endpoints | SKIPPED (vpc-abcd - No additional VPC endpoints required) Environment Variables | (Exec-check:2) 1. container "nginx" - AWS_ACCESS_KEY: not defined - AWS_ACCESS_KEY_ID: not defined - AWS_SECRET_ACCESS_KEY: not defined - 確認您已在容器層級設定 IAM 使用者憑證,例如存取金鑰或私密存取金鑰。
注意:SSM Agent 使用適用於 Java 的 AWS SDK 執行驗證檢查。如果您在容器執行個體中將存取金鑰或私密存取金鑰設定為環境變數,這些設定會覆寫任務層級權限。若要使用 ECS Exec,容器層級的 IAM 憑證必須為 SSM Agent 提供權限。 - 在任務定義中,確認 pidMode 未設定為 task。
**注意:**每個程序 ID (PID) 命名空間只能有一個 ECS Exec 工作階段。如果任務共用 PID 命名空間,您只能在其中一個容器中啟動 ECS Exec 工作階段。
使用 ECS Exec 透過正確的 Shell 進入容器
不同的基礎映像可能包含不同的 Shell。如果您使用錯誤的 Shell,系統會傳回錯誤。請根據應用程式映像使用正確的 Shell。
若要使用 ECS Exec 進入容器,請執行 execute-command 命令:
aws ecs execute-command --region example-region --cluster example-cluster --container example-container --task example-task --command "example_shell" --interactive
**注意:**將 example-region 替換為您的 AWS 區域、將 example-cluster 替換為您的叢集名稱、將 example-container 替換為您的容器執行個體名稱,並將 example-task 替換為您的任務名稱。
產生 ECS Exec 日誌以識別問題
若要產生 SSM Agent 日誌以取得 ECS Exec 無法運作的原因,請在容器定義的環境區段中執行以下命令。
主控台命令:
bin/bash,-c,sleep 2m && cat /var/log/amazon/ssm/amazon-ssm-agent.log
JSON 命令:
"/bin/bash","-c","sleep 2m && cat /var/log/amazon/ssm/amazon-ssm-agent.log"
**注意:**不同的應用程式會使用不同的 Shell 和編輯器。請依應用程式需求修改上述命令參數。
如果您使用 awslogs 日誌驅動程式,上述命令會產生 SSM Agent 日誌,並將日誌傳送至 Amazon CloudWatch 日誌群組。如果您使用其他日誌驅動程式或記錄端點,系統會將 SSM Agent 日誌傳送至這些位置。
JSON 範例:
"entryPoint": [], "portMappings": [], "command": [ "bin/bash", "-c", "sleep 2m && cat /var/log/amazon/ssm/amazon-ssm-agent.log" ],
相關內容
已提問 2 年前
已提問 3 年前

