Boto3 SSM Automation调用waiter出现InvocationDoesNotExist异常如何解决
问题原因
command_executed这个waiter仅适用于SSM的Run Command功能(对应调用send_command接口的场景),只有该接口返回的CommandId才能传给这个waiter做校验。- 你当前调用的是
start_automation_execution启动SSM自动化文档,返回的AutomationExecutionId是自动化执行任务的ID,和Run Command的CommandId属于完全不同的资源标识,传错ID后系统查询不到对应的执行记录,就会抛出InvocationDoesNotExist错误。
解决方案
根据你的实际需求选择对应方案即可:
场景1:需要等待整个SSM自动化任务执行完成
不要使用command_executed waiter,替换为适配自动化执行的等待逻辑:
方式1:使用官方自动化执行完成waiter(boto3 1.20+版本支持)
直接替换waiter相关代码即可:
# 替换原有command_executed waiter逻辑 waiter = client.get_waiter('automation_execution_completed') waiter.wait( AutomationExecutionId=response["AutomationExecutionId"] )
方式2:自行实现轮询逻辑(兼容旧版本boto3)
如果你的boto3版本没有提供对应官方waiter,自己写轮询逻辑即可:
import time automation_execution_id = response["AutomationExecutionId"] while True: resp = client.get_automation_execution(AutomationExecutionId=automation_execution_id) status = resp['AutomationExecution']['AutomationExecutionStatus'] if status in ['Success', 'TimedOut', 'Cancelled', 'Failed']: if status == 'Success': print("自动化执行成功") else: print(f"自动化执行结束,状态异常:{status}") break time.sleep(10) # 轮询间隔可自行调整
场景2:你的自动化文档内部包含Run Command步骤,需要单独等待该步骤完成
先调用get_automation_execution接口获取自动化执行的步骤详情,提取出对应Run Command步骤返回的真实CommandId,再将这个ID传给command_executed waiter即可。
内容的提问来源于stack exchange,提问作者Mervin Hemaraju
相关产品推荐
相关产品推荐

