2

I am trying to execute an SSM automation and then apply a waiter to wait for the execution to complete but i am landing on a InvocationDoesNotExist exception.

My code is as follows:

# Get the SSM client
client = boto3.client('ssm')

#Start the automation runbook and capture response
response = client.start_automation_execution(
    DocumentName='document_name',
    DocumentVersion='$LATEST',
    Parameters={
        'RestoredInstanceIds': [
            'i-02fee85b181a1gb55',
        ]
    }
)

print(response)

waiter = client.get_waiter('command_executed')

waiter.wait(
    CommandId=response["AutomationExecutionId"],
    InstanceId='i-02fee85b181a1gb55'
)

print("DONE")

The error is as follows: botocore.exceptions.WaiterError: Waiter CommandExecuted failed: An error occurred (InvocationDoesNotExist):

The print(response) works fine and gives me the correct execution ID:

{'AutomationExecutionId': '9a433866-...', 'ResponseMetadata': {'RequestId': '9a433866-...', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 18 Aug 2021 23:21:32 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '64', 'connection': 'keep-alive', 'x-amzn-requestid': '9a433866...'}, 'RetryAttempts': 0}}

Can someone please help why this is not working ?

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • 1
    I believe that command waiter is for commands created with `send_command()`, which returns a `CommandId`. Because there doesn't appear to be a waiter for automation execution, you need to poll `get_automation_execution` manually – jordanm Aug 18 '21 at 23:38
  • Ohh yeah this makes sense. Any way to create a custom waiter for get automation execution? – Mervin Hemaraju Aug 18 '21 at 23:44
  • 2
    Yeah, just use a while loop and call `get_automation_execution` with a sleep between calls until the state is one of the ones that would be considered complete. There is a list of all the states in the docs https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html#SSM.Client.get_automation_execution – jordanm Aug 19 '21 at 00:16

1 Answers1

0

This is what a potential solution might look like in Boto3.

import boto3
from botocore.waiter import WaiterModel
from botocore.waiter import create_waiter_with_client

client = boto3.client('ssm')
automation_execution_filter = [
    {
        'Key': 'ExecutionId',
        'Values': [
            'your execution id',
        ]
    }
]
waiter_name = "SSMAutomationExecuted"
waiter_config = {
    "version": 2,
    "waiters": {
        "SSMAutomationExecuted": {
            "operation": "DescribeAutomationExecutions",
            "delay": 60, # Number of seconds to delay
            "maxAttempts": 5, # Max attempts before failure
            "acceptors": [
                {
                    "matcher": "pathAll",
                    "expected": "Success",
                    "argument": "AutomationExecutionMetadataList[].AutomationExecutionStatus",
                    "state": "success"
                },
                {
                    "matcher": "pathAll",
                    "expected": "CompletedWithSuccess",
                    "argument": "AutomationExecutionMetadataList[].AutomationExecutionStatus",
                    "state": "success"
                },
                {
                    "matcher": "pathAny",
                    "expected": "Failed",
                    "argument": "AutomationExecutionMetadataList[].AutomationExecutionStatus",
                    "state": "failure"
                }
            ]
        }
    }
}
waiter_model = WaiterModel(waiter_config)
ssm_automation_waiter = create_waiter_with_client(waiter_name, waiter_model, client)

ssm_automation_waiter.wait(Filters=automation_execution_filter)
Coin Graham
  • 1,343
  • 3
  • 11