[英]How do I pass multiple parameters to AWS SSM send_command with Boto3
[英]Is there a way to check that ssm send_command is running properly?
我目前正在尝试使用 python 和 boto 在我的 ec2 实例上远程运行脚本,但我无法判断我对send_command
的调用是否正常工作。 截至目前,我的代码看起来像
ec2 = boto3.client('ssm',region_name='us-east-2', aws_access_key_id='XXXXXXXXX',aws_secret_access_key='XXXXXXXXXXXXXXXXXXX')
a = ec2.send_command(InstanceIds=ids, DocumentName='AWS-RunShellScript', Comment='abcdabcd', Parameters={"commands":["mkdir hello"]})
我要做的就是创建一个新目录,然后将 ssh 放入我的实例中,看看它是否存在。 我现在已经运行了这个脚本几次,但一点运气都没有,我是否遗漏了什么,或者是否有更好的方法来检查send_command
调用是否有效?
是的,实际上有很多方法可以检查命令是否有效。
如果我们运行以下命令:
ssm_response = ec2.send_command(InstanceIds=[instance_id],
DocumentName='AWS-RunShellScript',
Parameters={"commands": ["cd ~ && mkdir hello && ls -lart"]})
send_command
的返回是一个字典,其中包含命令的 id。 可以按如下方式检索此 id:
command_id = ssm_response['Command']['CommandId']
我们需要这个 id,因为预计命令将运行更长的时间,并且send_command
不会等到命令终止。
为了获取命令的状态,我们可以使用get_command_invocation
如下:
command_invocation_result = ec2.get_command_invocation(CommandId=command_id, InstanceId=instance_id)
这个 function 的结果也是一个字典,我们可以从中检索到很多关于命令的信息。
command_invocation_result['Status'] ## Returns the status of the execution of the command
command_invocation_result['StatusDetails'] ## Returns more information about the execution status
我们还可以得到命令的output和命令的错误output:
command_invocation_result['StandardOutputContent']
command_invocation_result['StandardErrorContent']
请不要在命令中执行ls -lart
,可以从StandardOutputContent
检索 output 。
send_command
和get_invocation_command
的文档: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html
此外,我们可以 go 进入 AWS 控制台 -> 系统管理器 -> 运行命令,select 命令历史记录,我们还应该在此处获得有关已执行命令的一些信息:
最后但同样重要的是,为了成功运行命令,EC2 实例需要具有 SSM 的 IAM 角色: https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-rc-setting- up.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.