AWS Systems ManagerでサンプルのAutomation Documentを作成・実行してみる(2)~複数ステップ実行 & ステップ実行エラー時の動作確認~
前回のメモでは、AWS Systems ManagerでAutomation Documentのサンプルを作成しましたが、実行ステップ数が1個のみだったので、今回は複数ステップで実行してみます。
新規Automation Documentを作成します。名前はMySampleAutomationDocument2、ドキュメントタイプはオートメーションドキュメントにします。
コンテンツは今回は2ステップで実行したいので、以下を入れてみます。
{ "description": "Automation Document Example JSON Template", "schemaVersion": "0.3", "assumeRole": "{{ AutomationAssumeRole }}", "parameters": { "AutomationAssumeRole": { "type": "String", "description": "(Optional) The ARN of the role that allows Automation to perform the actions on your behalf.", "default": "" } }, "mainSteps": [ { "name":"createText1", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Abort", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "echo hello1 > testfile1" ] } } }, { "name":"ls", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Abort", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "ls" ] } } } ] }
/home/ec2-userディレクトリ内にtestfile1を作成して、こちらのディレクトリ内でlsコマンドを実行します。
作成したMySampleAutomationDocument2を実行してみます。
結果の画面は以下です。
無事2つのステップが成功しているのが分かります。あとは、lsコマンドを実行するステップの詳細を見てみます。
上記画面にて、lsコマンドを/home/ec2-userディレクトリで自動実行し、作成したtextfile1ファイルがリスト表示されているのが分かります。
続いて、わざと1ステップでエラーを発生させてみます。実際には存在しないファイルの削除を行うステップを追加します。まずは、既存のMySampleAutomationDocument2で新しいバージョンを作成します。画面右上の新しいバージョンの作成を選びます。
コンテンツを修正します。
コンテンツの中身は以下です。ステップ2番目に存在しないファイルを削除するステップを入れてみます。
{ "description": "Automation Document Example JSON Template", "schemaVersion": "0.3", "assumeRole": "{{ AutomationAssumeRole }}", "parameters": { "AutomationAssumeRole": { "type": "String", "description": "(Optional) The ARN of the role that allows Automation to perform the actions on your behalf.", "default": "" } }, "mainSteps": [ { "name":"createText1", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Abort", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "echo hello1 > testfile1" ] } } }, { "name":"rm", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Abort", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "rm 1111.txt" ] } } }, { "name":"ls", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Abort", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "ls" ] } } } ] }
新しいバージョンの作成を選んだあと、MySampleAutomationDocument2の新しいバージョンを実行します。
すると当然ですが、存在しないファイルを削除しようとするステップ2が失敗します。
ステップ実行の失敗時の挙動については、OnFailureで指定可能です。デフォルトではAbort (中止)の動作になりますが、Continueを指定することで、継続も可能です。
Systems Manager 自動化ドキュメントのリファレンス - AWS Systems Manager
今回は、rmのステップにて、失敗時に継続するように変更してみます。変更箇所は以下。
{ "name":"rm", "action":"aws:runCommand", "maxAttempts":3, "timeoutSeconds":1200, "onFailure":"Continue", "inputs":{ "DocumentName":"AWS-RunShellScript", "InstanceIds":[ "i-04e0ac63bc6e4105f" ], "Parameters":{ "workingDirectory":"/home/ec2-user", "commands":[ "rm 1111.txt" ] } } },
修正して再度実行した結果は以下。
ステップ2が失敗しても、続けてステップ3が実行されたことが分かります。