【问题标题】:Boto3 SSM waiter not working for Automation ExecutionBoto3 SSM 服务员不适用于自动化执行
【发布时间】:2021-10-20 17:16:18
【问题描述】:

我正在尝试执行 SSM 自动化,然后应用服务员等待执行完成,但我遇到了 InvocationDoesNotExist 异常。

我的代码如下:

# 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")

报错如下:botocore.exceptions.WaiterError: Waiter CommandExecuted failed: An error occurred (InvocationDoesNotExist):

打印(响应)工作正常,并为我提供正确的执行 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}}

有人可以帮忙解释一下为什么这不起作用吗?

【问题讨论】:

  • 我相信命令服务员是为用send_command()创建的命令,它返回一个CommandId。因为好像没有自动执行的服务员,需要手动轮询get_automation_execution
  • 哦,是的,这是有道理的。有什么方法可以创建自定义服务员以获取自动化执行?
  • 是的,只需使用一个 while 循环并调用 get_automation_execution 并在调用之间休眠,直到状态是被认为是完整的状态之一。文档中有一个所有状态的列表boto3.amazonaws.com/v1/documentation/api/latest/reference/…

标签: python python-3.x amazon-web-services boto3 aws-ssm


【解决方案1】:

这就是 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2020-10-05
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多