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.
如何疑難排解 Amazon ECS 中與 Secrets Manager 祕密相關的問題?
當我嘗試在 Amazon Elastic Container Service (Amazon ECS) 中放置使用 AWS Secrets Manager 祕密的任務時,收到錯誤訊息。
簡短說明
如果您無法在 Amazon ECS 中放置使用 Secrets Manager 祕密的任務,可能會收到以下其中一則錯誤訊息:
「ResourceInitializationError error on AWS Fargate AccessDenied error on Amazon Elastic Compute Cloud (Amazon EC2)」
「The task is unable to retrieve secrets from Secrets Manager and fails.」
「ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secret from asm: service call has been retried 1 time(s): failed to fetch secret」
「AccessDeniedException: User: arn:aws:sts::XXXXXXXX:assumed-role/test-ecs-execution-role-no-secrets/XXXXXXXXXXX is not authorized to perform:」
以下其中一個原因會導致這些錯誤訊息:
- Amazon ECS 任務執行角色沒有存取 Secrets Manager 祕密所需的權限。
- Secrets Manager 找不到指定的祕密。
- 祕密名稱以連字號和六個字元結尾,而您使用部分 Amazon Resource Name (ARN) 搜尋祕密。
- Amazon Virtual Private Cloud (Amazon VPC) 網路組態有問題。
若要疑難排解無法啟動的 Amazon ECS 任務錯誤,請使用 AWSSupport-TroubleshootECSTaskFailedToStart 執行手冊。接著,請參閱與您問題相關的疑難排解步驟。
解決方法
先決條件:確認您的 AWS Identity and Access Management (IAM) 使用者或角色擁有使用此執行手冊所需的權限。如需更多資訊,請參閱 AWSSupport-TroubleshootECSTaskFailedToStart 的必要 IAM 權限一節。
請在 Amazon ECS 叢集資源所在的相同 AWS 區域中使用 AWSSupport-TroubleshootECSTaskFailedToStart 執行手冊。
使用最近失敗的任務 ID。如果失敗的任務屬於 Amazon ECS 服務,請使用該服務中最近失敗的任務。自動化執行期間,必須能在 ECS:DescribeTasks 中看到失敗的任務。根據預設,Amazon ECS 任務進入已停止狀態後,仍會顯示 1 小時。
執行 AWS Systems Manager 執行手冊
完成以下步驟:
- 開啟 Systems Manager console (Systems Manager 主控台)。
- 在導覽窗格中,選擇 Documents (文件)。
- 在搜尋列中,輸入 TroubleshootECSTaskFailedToStart。
- 選取 AWSSupport-TroubleshootECSTaskFailedToStart 文件。
- 選擇 Execute automation (執行自動化)。
- 選擇 Execute (執行)。
- 自動化完成後,請檢閱 Outputs (輸出) 區段中的詳細結果,然後使用以下其中一個手動疑難排解步驟。
檢查 Amazon ECS 任務執行角色權限
完成以下步驟:
- 開啟 IAM console (IAM 主控台)。
- 在導覽窗格中,選擇 Roles (角色)。
- 在角色清單中搜尋 Amazon ECS 任務所使用的任務執行角色。
- 確認您已授予任務執行角色存取 Secrets Manager 資源的必要權限。
檢查 Secrets Manager 祕密是否存在
完成以下步驟:
- 開啟 Secrets Manager console (Secrets Manager 主控台)。
- 在導覽窗格中,選擇 Secrets (祕密),然後選擇 Amazon ECS 任務使用的祕密。
- 確認 Amazon ECS 任務定義包含這些特定祕密名稱。如果您在祕密中指定其他參數,請確認任務定義包含祕密中指定的 json-key/version-stage/version-id。
檢查 Secrets Manager 祕密的名稱和 ARN
檢查祕密名稱是否以連字號和六個字元結尾。例如,myappsecret-xxxxxx。Secrets Manager 會自動在 ARN 結尾的祕密名稱後面加上連字號和六個隨機字元。最佳實務是不要讓祕密名稱以連字號和六個字元結尾。
若要解決此問題,請使用 Secrets Manager 中祕密的完整 ARN,包括額外的連字號和六個字元。
檢查 Amazon VPC 網路組態
符合以下條件時,請為 Secrets Manager 建立 Amazon VPC 端點:
- 您使用參考 Secrets Manager 祕密的任務定義,以擷取容器的敏感資料。
- 您使用 Amazon VPC 端點。
此外,請確認 Amazon VPC 端點的安全群組允許 Amazon ECS 基礎結構使用這些端點。
若要檢查 Secrets Manager 的 Amazon VPC 端點是否存在,請完成以下步驟:
- 開啟 Amazon VPC console (Amazon VPC 主控台)。
- 在導覽窗格中,選擇 Endpoints (端點)。
- 確認您已為 Secrets Manager 指定 Amazon VPC 端點 com.amazonaws.example-region.secretsmanager。
- 選取端點,然後選擇 Subnets (子網路) 索引標籤。確認此端點的子網路包含 Amazon ECS 任務使用的端點。
**注意:**如果沒有子網路,請選擇 Manage Subnets (管理子網路)。根據子網路的 Availability Zone (可用區域) 選取子網路,然後選擇 Modify Subnets (修改子網路)。
若要確認連接至端點 com.amazonaws.example-region.secretsmanager 的安全群組允許來自 Amazon ECS 任務透過連接埠 443 的傳入連線,請完成以下步驟:
- 開啟 Amazon VPC console (Amazon VPC 主控台)。
- 在導覽窗格中,選擇 Endpoints (端點)。
- 選取端點。
- 選擇 Security Groups (安全群組) 索引標籤。
- 針對您要檢查的安全群組,選擇 Group ID (群組 ID)。
- 選擇 Inbound rules (傳入規則) 索引標籤。
- 確認傳入規則清單包含允許 Amazon ECS 任務透過連接埠 443 連線的規則。
相關資訊
This article was reviewed and updated on 2026-02-25.
相關內容
已提問 2 年前
已提問 2 年前
