Search code examples
powershellaws-ssmaws-powershell

How to print the output of each EC2 instance of a run command executed through SSM automation document


I am trying to print the output of multiple EC2 instances executed via AWS-RunPowerShellScript document through AWS SSM Automationin Automation execution step outputs section. However, it seems to be not working. Can anyone help me with correct syntax for the same. Please find the below code:

description: printssmruncommandoutput
schemaVersion: '0.3'
mainsteps:
  -name:printssmoutput
   action:'aws:runcommand'
   onfailure:Abort
   inputs:
     DocumentName:AWS-RunPowerShellScript
     Targets:
       -Key: ec2key
        Values:
          - ec2instance
     Parameters:
       Commands:
         - 'hostname'
         - 'Write-Host "this is the test server"'
   outputs:
     - Name: DesiredOutput
       Selector: $.OutputPayload
       Type: String

outputs:
 - DesiredOutput.OutputPayload.CommandId
 - DesiredOutput.OutputPayload.Status

It seems like not working. In the outputs section of SSM Automation execution, I am seeing this error even after successful execution of SSM automation document step: "No output available yet because the step is not successfully executed"


Solution

  • I had the same question and finally figured it out. I have not been able to access OutputPayload as a JSON object but you can access the four items it contains: Status, ResponseCode, Output, and CommandId.

    description: PrintSSMRunCommandOutput
    schemaVersion: "0.3"
    mainSteps:
      - name: PrintSSMOutput
        action: aws:runCommand
        onFailure: Abort
        inputs:
          DocumentName: AWS-RunPowerShellScript
          Targets:
            - Key: tag:ec2key
              Values:
                - ec2instance
          Parameters:
            commands:
              - hostname
              - Write-Host "this is the test server"
        outputs:
          - Name: OutputStatus
            Selector: $.Status
            Type: String
          - Name: OutputResponseCode
            Selector: $.ResponseCode
            Type: Integer
          - Name: OutputOutput
            Selector: $.Output
            Type: String
          - Name: OutputCommandId
            Selector: $.CommandId
            Type: String
    outputs:
      - PrintSSMOutput.OutputStatus
      - PrintSSMOutput.OutputResponseCode
      - PrintSSMOutput.OutputOutput
      - PrintSSMOutput.OutputCommandId