首页 > 解决方案 > 在 AWS SSM 中检索命令调用

问题描述

我正在尝试将命令发送到正在运行的 ubuntu ec2 实例。我已经配置了适当的角色,并且在 ec2 实例上运行了一个 ssm 代理。使用 boto3 SDK,我能够使用该client.send_command()函数成功发送 shell 命令,并随后能够获取命令 ID。现在的挑战是轮询结果。我正在尝试使用该 client.get_command_invocation()功能,但不断收到InvocationDoesNotExist错误消息。我确信我使用了正确的命令 ID 和实例 ID,因为我已经使用 AWS CLI 对它们进行了测试, aws ssm list-command-invocations --command-id #### --instance-id i-#####并且这很有效。这是我的代码片段:

`ssm_client = boto3.client('ssm')
target_id = "i-####"
response = ssm_client.send_command(
            InstanceIds=[
                target_id 
                    ],
            DocumentName="AWS-RunShellScript",
            Comment="Code to run" ,
            Parameters={
                    "commands": ["ls -l"]
                        }
            )
cmd_id= response['Command']['CommandId']
response = ssm_client.get_command_invocation(CommandId=cmd_id, 
InstanceId=target_id)
print(response)`

这是返回的错误: botocore.errorfactory.InvocationDoesNotExist: An error occurred (InvocationDoesNotExist) when calling the GetCommandInvocation operation

提前致谢。

标签: amazon-web-servicesssm

解决方案


我遇到了同样的问题,我通过在调用 get_command_invocation() 之前添加 time.sleep() 调用来修复它。短暂的延迟就足够了。


推荐阅读