Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to handle information from SSM runpowershell or runscript #33

Open
FredSamuels opened this issue Jan 31, 2019 · 5 comments
Open

how to handle information from SSM runpowershell or runscript #33

FredSamuels opened this issue Jan 31, 2019 · 5 comments

Comments

@FredSamuels
Copy link

i am trying to figure out how to process complex structures with SSM work flow. I am trying to process data based on system returned data. Idea is to update Tags based on system information.

Example of a Step:

{
    "name": "WindowsSystemInfo",
    "action": "aws:runCommand",
    "onFailure": "Abort",
    "inputs": {
        "DocumentName": "AWS-RunPowerShellScript",
        "InstanceIds": [
            "{{ InstanceId }}"
        ],
        "Parameters": {
            "commands": [
                "$Output = [PSCustomObject]@{ ",
                "    systemname = (Get-WmiObject Win32_ComputerSystem).Name ",
                "    uptime     = (\"{0:dd'.'hh':'mm':'ss}\" -f (New-TimeSpan ((Get-CimInstance Win32_OperatingSystem).LastBootUpTime) (Get-Date))) ",
                "    domain     = if ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) { (Get-WmiObject Win32_ComputerSystem).Domain } Else { (Get-WmiObject -Class Win32_ComputerSystem).Workgroup } ",
                "} ",
                "Write-Output $Output | ConvertTo-Json "
            ]
        }
    }
}

Using the Output is represented in

WindowsSystemInfo.Output

But how do i parse the data to use on additional steps. I can not find examples or any good support sites.

please let me know if there is a community site or location for this workflow.

@sharvaniadiga
Copy link

If it is simple data type comparison like string or integer, Systems Manager Automation support conditional branching - https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-branchdocs.html
Using conditional branching, you can specify next step that you want to jump to after the current step. The public document AWS-DisablePublicAccessForSecurityGroup has an example of using conditionals.

@FredSamuels
Copy link
Author

So while I understand branching there is no detailed documentation on data handling.. please read my example.. as the data is generated from the system and is stored in output.

WindowsSystemInfo.Output

This data is a son data set or a MapString.

If I pull back a single result in text I can use the info as above var ...

While getting the data that the was cli commands can return there is no examples except for branch.

There is no documentation on var handling ... all that is there is the same example that used a single string. if you want us to make complete and complex workflows with sum you have to get us the info on how.

I am not looking to compare the data but use the data...

here is an example that does not work but should based on the rules of a function and var structure .

if there was good data to use I could process more data in 1 command and not have to write a single runpowershell for each value to be returned. This would be worse than Jenkins running python and boto3.

{
    "description": "Ellucain Demo Instance SSM Automation",
    "schemaVersion": "0.3",
    "assumeRole": "{{ AutomationAssumeRole }}",
    "parameters": {
        "InstanceId": {
            "type": "String",
            "description": "(Required) EC2Instance(s) for Description",
            "allowedPattern": "^[m]{0,1}i-[a-z0-9]{8,17}$"
        },
        "AutomationAssumeRole": {
            "type": "String",
            "description": "(Optional) The ARN of the role that allows Automation to perform the actions on your behalf.",
            "default": ""
        }
    },
    "mainSteps": [
        {
            "name": "assertInstanceIsManagedInstance",
            "action": "aws:assertAwsResourceProperty",
            "inputs": {
                "Service": "ssm",
                "Api": "DescribeInstanceInformation",
                "InstanceInformationFilterList": [
                    {
                        "key": "InstanceIds",
                        "valueSet": [
                            "{{ InstanceId }}"
                        ]
                    }
                ],
                "PropertySelector": "$.InstanceInformationList[0].PingStatus",
                "DesiredValues": [
                    "Online"
                ]
            }
        },
        {
            "name": "WindowsHostInfo",
            "action": "aws:runCommand",
            "onFailure": "Abort",
            "inputs": {
                "DocumentName": "AWS-RunPowerShellScript",
                "InstanceIds": [
                    "{{ InstanceId }}"
                ],
                "Parameters": {
                    "commands": [
                        "$Output = [PSCustomObject]@{ ",
                        "    systemname = (Get-WmiObject Win32_ComputerSystem).Name ",
                        "    uptime     = (\"{0:dd'.'hh':'mm':'ss}\" -f (New-TimeSpan ((Get-CimInstance Win32_OperatingSystem).LastBootUpTime) (Get-Date))) ",
                        "    domain     = if ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) { (Get-WmiObject Win32_ComputerSystem).Domain } Else { (Get-WmiObject -Class Win32_ComputerSystem).Workgroup } ",
                        "} ",
                        "Write-Output $Output | ConvertTo-Json "
                    ]
                }
            },
            "outputs": [
                {
                    "Name": "uptime",
                    "Selector": "$.uptime",
                    "Type": "String"
                }
            ],
            "isEnd": "true"
        }
    ],
    "outputs": [
        "WindowsHostInfo.Output",
        "WindowsHostInfo.uptime"
    ]
}

@Regancm
Copy link

Regancm commented Oct 15, 2020

@FredSamuels did you ever figure this out? I am trying to figure out the exact same thing..

@Regancm
Copy link

Regancm commented Oct 16, 2020

@FredSamuels I figured it out and I hate it.. but it works. AWS should really let the Selector parse Output though. This is so painful.

description: SSM Automation document for resizing the specified EBS volume to the target size
schemaVersion: '0.3'
assumeRole: '{{AutomationAssumeRole}}'
parameters:
  AutomationAssumeRole:
    type: String
    default: ''
    description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
  InstanceId:
    type: String
    default: ''
    description: (Required) EC2 InstanceId to use for execution target
mainSteps:
  - name: RunPowerShellScript
    action: 'aws:runCommand'
    timeoutSeconds: 300
    onFailure: Abort
    inputs:
      DocumentName: AWS-RunPowerShellScript
      InstanceIds:
        - '{{ InstanceId }}'
      Parameters:
        commands:
          - |
            try {
                # build some object and convert it to json
                $json = @{
                    string1 = 'string1'
                    string2 = 'string2'
                    string3 = 'string3'
                    int1 = 1
                    int2 = 2
                    int3 = 3
                } | ConvertTo-Json -Compress
                return $json
            }
            catch {
                Write-Output $_.Exception.Message
                Write-Output "----------------------------------------"
                Write-Output $_.Exception
                exit 1
            }
    nextStep: ConvertToOutputs
  - name: ConvertToOutputs
    action: 'aws:executeScript'
    inputs:
      Runtime: PowerShell Core 6.0
      Script: return (($env:InputPayload | ConvertFrom-Json).stringOfJson | ConvertFrom-Json);
      InputPayload:
        stringOfJson: '{{ RunPowerShellScript.Output }}'
    outputs:
      - Name: string1
        Selector: '$.Payload.string1'
        Type: String
      - Name: string2
        Selector: '$.Payload.string2'
        Type: String
      - Name: string3
        Selector: '$.Payload.string3'
        Type: String
      - Name: int1
        Selector: '$.Payload.int1'
        Type: Integer
      - Name: int2
        Selector: '$.Payload.int2'
        Type: Integer
      - Name: int3
        Selector: '$.Payload.int3'
        Type: Integer

@bar-bar-bar-bar-bar
Copy link

Is this still the only way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants