Search code examples
amazon-web-servicesaws-ssm

SSM runCommand document with conditional branching


I am creating a aws ssm document and in the first step, I am trying to run shell script.

- name: checkMembership
  action: 'aws:runCommand'
  inputs:
    DocumentName: AWS-RunShellScript
    InstanceIds:
      - '{{InstanceIds}}'
    Parameters:
      commands:
        - mkdir testFolder

my question is would I be able to do a multi-line if else here with the powershell script or shell script?

- name: checkMembership
  action: 'aws:runCommand'
  inputs:
    DocumentName: AWS-RunShellScript
    InstanceIds:
      - '{{InstanceIds}}'
    Parameters:
      commands:
        - if $foo
        - then
        - echo 'hello'
        - else
        - echo 'bye'

Solution

  • Yes, you can use conditional branching in the command.

    Here's an example:

    "runCommand": [
        "#!/bin/bash",
        "example_var=true",
        "   if [ \"$example_var\" = true ] ;",
        "   then",
        "     echo \"example_var is true\"",
        "   else",
        "     echo \"example_var is not true\"",
        "   fi"
    ]
    

    I recommend you take a look at the AWS managed documents. There are some really good examples there that you can reference.