funasaki memo

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

AWS Systems ManagerでサンプルのAutomation Documentを作成・実行してみる(3)~承認のステップを追加~

前回のメモでは、AWS Systems ManagerのAutomation Documentにて複数ステップを実行しました。今回はそのDocumentをベースに、承認のステップを追加します。分かりやすさのため、前回のAutomation Documentでの「ファイル削除」のステップは割愛します。

今回使用するサンプルのAutomation Documentは以下です。

{
  "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":"approve",
         "action":"aws:approve",
         "timeoutSeconds":1000,
         "onFailure":"Abort",
         "inputs":{
            "NotificationArn":"arn:aws:sns:ap-northeast-1:123456789012:test",
            "Message":"Please approve this automation.",
            "MinRequiredApprovals":1,
            "Approvers":[
               "arn:aws:iam::123456789012:user/user1"
            ]
         }
    },
    {
    "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"
          ]
        }
      }
    }
  ]
}

Amazon SNS (Simple Notification Service)のトピックはすでに作成済みで、そのトピックに対して、今回は自分のメールアドレスでsubscribeしています。

上記Automation Documentを実行した直後の結果は以下。

f:id:kenjifunasaki:20180413190010p:plain

approveのステップで待機中となっているのが分かります。この状態で、自分のメールボックスを確認すると、以下のような内容のメールが届いていました。

Please approve this automation.

-- Approval Details --

Approval Step Name: approve
Region: ap-northeast-1
Automation Execution Id: 59cf7cbe-3f00-11e8-8d8a-6d3cffee791e
Approval Expires At: 2018-04-13 10:09 AM UTC

-- Approve or reject through AWS CLI --

Approve:   aws ssm send-automation-signal --automation-execution-id 59cf7cbe-3f00-11e8-8d8a-6d3cffee791e --signal-type Approve --payload Comment=Replace_This_With_Approve_Comment 

Reject:   aws ssm send-automation-signal --automation-execution-id 59cf7cbe-3f00-11e8-8d8a-6d3cffee791e --signal-type Reject --payload Comment=Replace_This_With_Reject_Comment

Approval Expiresには、今回タイムアウト時間を1000秒に指定しているため、その時間経過との時刻が指定されているようです。この時刻になる前に、指定された以下コマンドを、先ほどのドキュメントで指定したIAMユーザで実行してみます。

aws ssm send-automation-signal --automation-execution-id 59cf7cbe-3f00-11e8-8d8a-6d3cffee791e --signal-type Approve --payload Comment=I_approved_this_automation

実行後、Automationの実行結果を確認すると
f:id:kenjifunasaki:20180413190440p:plain

無事approveのステップが成功になり、その後のステップも成功していることが確認できました。