Cloud9からECS Executeを実行する際に必要なIAMロールの権限を教えてください

Cloud9からIAMロールを使用してECS Executeを実行する際に、「User is not authorized to perform: ecs:ExecuteCommand」エラーが出てしまった時のIAMポリシーの設定方法をご案内します。
2023.08.25

困っていること

Cloud9からIAM Roleの権限でAWS CLIを使用してECS Executeを実行しようとしていますが、以下のエラーが出てしまいます。

An error occurred (AccessDeniedException) when calling the ExecuteCommand operation: User: arn:aws:sts::xxxxxxxx:assumed-role/AWSCloud9SSMAccessRole/instance-id is not authorized to perform: ecs:ExecuteCommand on resource: arn:aws:ecs:region:xxxxxxxxx:cluster-name because no identity-based policy allows the ecs:ExecuteCommand action

Cloud9のEC2にアタッチしているIAM ロールに、どのようなポリシーを設定すれば良いでしょうか?

どう対応すればいいの?

エラーメッセージに not authorized to perform: ecs:ExecuteCommand とあるので、同権限を含めたポリシーを作成します。

作成例:

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

その他、ポリシー作成の例を確認したい場合は、参考情報[1]の「IAM ポリシーを使用して ECS Exec へのアクセスを制限する」を参考にしてください。

参考情報

[1]デバッグ用にAmazon ECS Exec を使用 - Amazon Elastic Container Service

[2]ECS で ExecuteCommand を呼び出す際のエラー「 TargetNotConnectedException」のトラブルシューティングを行う | AWS re:Post

[3]AWS Cloud9 の環境から AWS のサービス の呼び出し - AWS Cloud9