【EC2】「EC2: コマンドを実行」アクションでPowerShellスクリプトを実行したときに、コマンドは失敗したのにジョブは成功となってしまった

「EC2: コマンドを実行」アクションはバックエンドにAWS Systems Managerの Run Commandで「AWS-RunPowerShellScript」のドキュメントを使用しています。

この場合、スクリプトの最後で exit 命令を呼び出し、成功なら0、それ以外なら0以外を明示的に実行する必要があります。

PowerShell requires that you call exit explicitly in your scripts for Run Command to successfully capture the exit code.

https://docs.aws.amazon.com/systems-manager/latest/userguide/run-command-handle-exit-status.html

 

例えば、以下のようなスクリプトを実行し、コマンド内の実行結果によって、 exit を実行します。

aws ec2 describe-instances --instance-ids i-09f749540b678518d
$exitCode = $LASTEXITCODE

if ($exitCode -ne 0) {
    Write-Error "aws cli failed with exit code $exitCode"
   exit 1
}

Write-Output "EC2 start-instances succeeded"
exit 0

 

関連

コマンドでの終了コードの使用 - AWS Systems Manager
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/run-command-handle-exit-status.html

 

この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています
他にご質問がございましたら、リクエストを送信してください