如何解決 Amazon ECS 中的錯誤 "An error occurred (TargetNotConnectedException) when calling the ExecuteCommand operation"?

1 分的閱讀內容
0

當我嘗試在 Amazon Elastic Container Service (Amazon ECS) 中執行 AWS Command Line Interface (AWS CLI) 命令 execute-command 時,收到下列錯誤: "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) 角色或使用者沒有必要的權限。

**注意:**如果您在執行 AWS CLI 命令時收到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

解決方法

檢查 Amazon ECS 任務角色權限

Amazon ECS 任務角色沒有必要的權限時,您會收到此錯誤。若要解決此錯誤,請建立具有必要權限的 IAM 政策,然後將該政策附加到 Amazon ECS 任務角色。

  1. 建立下列 IAM 政策
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "ssmmessages:CreateControlChannel",
            "ssmmessages:CreateDataChannel",
            "ssmmessages:OpenControlChannel",
            "ssmmessages:OpenDataChannel"
          ],
          "Resource": "*"
        }
      ]
    }
    **注意:**請確定 AWS Organizations 層級不會拒絕這些權限。
  2. 將政策附加到 Amazon ECS 任務角色

在任務層級進行這些變更可能會有延遲。因此,請在將政策附加到任務角色之後等待一段時間,然後執行 execute-command 命令。

檢查 IAM 使用者或角色權限

執行 execute-command 命令的 IAM 使用者或角色必須具有下列權限:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ecs:ExecuteCommand",
      "Resource": "arn:aws:ecs:example-region:example-arn:cluster/example-cluster/*"
    }
  ]
}

如果您仍然收到錯誤,請從 GitHub 網站執行 amazon-ecs-exec-checker 指令檔。此指令檔可讓您檢查和驗證 AWS CLI 環境以及 Amazon ECS 叢集或任務。指令檔還會通知您未滿足的先決條件

相關資訊

使用 ECS Exec

AWS 官方
AWS 官方已更新 1 年前