funasaki memo

このブログ上の投稿は個人のものであり、所属する企業を代表する投稿ではありません。所属:AWSのSolutions Architect

AWS Systems ManagerでサンプルのAutomation Documentを作成・実行してみる(2)~複数ステップ実行 & ステップ実行エラー時の動作確認~

前回のメモでは、AWS Systems ManagerでAutomation Documentのサンプルを作成しましたが、実行ステップ数が1個のみだったので、今回は複数ステップで実行してみます。

新規Automation Documentを作成します。名前はMySampleAutomationDocument2、ドキュメントタイプはオートメーションドキュメントにします。

f:id:kenjifunasaki:20180413144636p:plain

コンテンツは今回は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を実行してみます。
f:id:kenjifunasaki:20180413144924p:plain

結果の画面は以下です。
f:id:kenjifunasaki:20180413145038p:plain

無事2つのステップが成功しているのが分かります。あとは、lsコマンドを実行するステップの詳細を見てみます。

f:id:kenjifunasaki:20180413145206p:plain

上記画面にて、lsコマンドを/home/ec2-userディレクトリで自動実行し、作成したtextfile1ファイルがリスト表示されているのが分かります。

続いて、わざと1ステップでエラーを発生させてみます。実際には存在しないファイルの削除を行うステップを追加します。まずは、既存のMySampleAutomationDocument2で新しいバージョンを作成します。画面右上の新しいバージョンの作成を選びます。

f:id:kenjifunasaki:20180413175932p:plain

コンテンツを修正します。

f:id:kenjifunasaki:20180413180018p:plain

コンテンツの中身は以下です。ステップ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の新しいバージョンを実行します。

f:id:kenjifunasaki:20180413180230p:plain

すると当然ですが、存在しないファイルを削除しようとするステップ2が失敗します。

f:id:kenjifunasaki:20180413180507p:plain

ステップ実行の失敗時の挙動については、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"
          ]
        }
      }
    },

修正して再度実行した結果は以下。

f:id:kenjifunasaki:20180413182122p:plain

ステップ2が失敗しても、続けてステップ3が実行されたことが分かります。