Rem*_*aru 10 amazon-web-services 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)`
Run Code Online (Sandbox Code Playgroud)
这是返回的错误:
botocore.errorfactory.InvocationDoesNotExist: An error occurred (InvocationDoesNotExist) when calling the GetCommandInvocation operation
提前致谢。
只需添加这两行即可。
import time
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
然后就可以正常工作了,一般只需要0.65秒,但最好给2秒。为了使它更好,您可以添加一些很酷的东西,例如 for 循环中的一些 print 语句,并在其中睡眠类似的东西。
以下内容对我有用。请在https://boto3.amazonaws.com查看 API 文档,了解如何更新默认延迟和最大尝试次数。
from botocore.exceptions import WaiterError
ssm_client = boto3.client(
"ssm",
region_name=REGION,
aws_access_key_id=KEY,
aws_secret_access_key=SECRET,
)
waiter = ssm_client.get_waiter("command_executed")
try:
waiter.wait(
CommandId=commands_id,
InstanceId=instance_id,
)
except WaiterError as ex:
logging.error(ex)
return
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4518 次 |
| 最近记录: |