LoginSignup
3
2

More than 1 year has passed since last update.

ECSのログインでInvalidParameterExceptionのエラーになった際の解決方法

Posted at

概要

ECSにログインしようとしたらエラーで弾かれるため、その解決方法のメモ。

前提条件

  • AWSのECSを組んでいること
  • ECSログインを試みるとエラーになること

エラーの事象

ECSログインコマンドでのエラー

$ aws ecs execute-command  \
    --region ap-northeast-1 \
    --cluster  {cluster名}  \
    --task {task名}  \
    --command "sh" \
    --interactive 

The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.

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.

エラー原因の確認方法

enableExecuteCommandの設定値を確認する。
設定値が大量に表示されるので、 | grep enableExecuteCommandをコマンド末尾に追加して、enableExecuteCommandだけ確認できるようにしておくのがオススメです。

$ aws ecs describe-tasks  \
    --region ap-northeast-1 \
    --cluster  {cluster名}  \
    --task {task名}  \
| grep enableExecuteCommand

"enableExecuteCommand": false,

enableExecuteCommandfalseになっていたらこれが原因の可能性があります。

対応方法

1. enableExecuteCommandをtrueへ更新

$ aws ecs update-service \
--cluster {cluster名} \
--service {service名} \  
--enable-execute-command

権限がないと怒られる場合

IAMユーザーに権限がないとエラーになります。

aws ecs update-service \
--cluster {cluster名} \
--service {service名} \    
--enable-execute-command 

An error occurred (AccessDeniedException) when calling the UpdateService operation

この場合、下記IAMポリシーを作成して、対象のIAMユーザーにアタッチしてあげると更新できるようになります。

  • ECS用IAMポリシー用JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "UpdateService",
            "Effect": "Allow",
            "Action": [
                "ecs:UpdateService"
            ],
            "Resource": "arn:aws:ecs:ap-northeast-1:{AWSアカウントID}:service/httpd-cluster/{ECSサービス名}"
        }
    ]
}

参考:https://dev.classmethod.jp/articles/github-actions-ecs-ecr-minimum-iam-policy/#toc-7

2. タスクの再起動を行う

enableExecuteCommandの設定値を更新後、ECSタスクの再起動を行います。

サービスの更新から「新しいデプロイの強制」にチェックを入れて再起動します。

スクリーンショット 2022-04-27 16.24.13.png

3. enableExecuteCommand設定値の再確認

enableExecuteCommandを再確認するとtrueに変わっているはずです!

$ aws ecs describe-tasks  \
    --region ap-northeast-1 \
    --cluster  {cluster名}  \
    --task {task名}  \
| grep enableExecuteCommand

"enableExecuteCommand": true,

4. ログイン確認

ログインしてみると成功します。

$ aws ecs execute-command  \
    --region ap-northeast-1 \       
    --cluster  {cluster名}  \
    --task {task名}  \
    --command "sh" \
    --interactive 
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.


Starting session with SessionId: ecs-execute-command-068a203129a208fa0
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2