amazon-web-services - 在 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
提前致谢。
解决方案
我遇到了同样的问题,我通过在调用 get_command_invocation() 之前添加 time.sleep() 调用来修复它。短暂的延迟就足够了。
推荐阅读
- mysql - “无法添加外键约束”
- logging - 我需要在我的网站上有一个 IP 日志吗?
- javascript - Reactjs onFocus 事件不适用于键盘导航
- php - 上传和缩放没有高度参数的图像
- c# - 如何根据 C# 中的用户输入在我的图片框中显示不同的图片?
- php - 使用输入日期选择器的 Codeigniter 无法发布到数据库
- c# - 将按钮内容绑定到数据库中的不同值
- android-ndk - 当 AMediaCodec_releaseOutputBufferAtTime() [详细]
- r - 从R中的表中打印行
- python - 根据 pandas/python 中多列的字符串对列中的值求和