![](/img/trans.png)
[英]AWS - Associate EC2 with SSM to enable ssm.client.send_command
[英]SSM send command to EC2 instance Failed
我正在尝试使用boto3在EC2实例上运行ssh命令。 我读了这本指南: http : //docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshooting-remote-commands.html我做了他们在那里写的所有内容,但我一直收到错误信息:
>>>import boto3
>>> ec2 = boto3.client('ssm')
>>> a = ec2.send_command(InstanceIds=['i-0d5e16f6'], DocumentName='AWS-RunShellScript', Comment='abcdabcd', Parameters={"commands":["ifconfig"]})
输出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 543, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidInstanceId: An error occurred (InvalidInstanceId) when calling the SendCommand operation:
如果我正在尝试用awscli发送命令,我会遇到同样的问题:
aws ssm send-command --instance-ids "i-0d5e16f6" --document-name "AWS-RunShellScript" --comment "IP config" --parameters commands=ifconfig --output text
An error occurred (InvalidInstanceId) when calling the SendCommand operation:
有人知道怎么解决吗?
当您尝试访问的实例上没有安装SSM代理时,可能会发生这种情况。 有关可以运行SSM命令的实例列表,请运行:
aws ssm describe-instance-information --output text
从那里,您可以获取实例ID,然后使用该实例运行send_command
命令。
如AWS'故障排除指南中所述,此错误可能有多种原因。
接受的答案是aws ssm describe-instance-information
检查实例,这些实例都是可用的,处于有效状态并且安装了SSM代理,因此它涵盖了一行中的几个故障排除步骤(很好;))。
如果您使用的是boto3
,可以通过以下方式实现:
ssm.client.describe_instance_information()
我不确定它是否检查权限但是假定如此。 如果列表中缺少instance_id,则可以按照此处的分步操作确保正确的权限。
然而,还有另一个原因(最后但并非最不重要,因为它并不明显):
新创建的实例需要一些时间才能显示在describe_instance_information
列表中 。
甚至在等待实例完成创建后也是如此。 例如,做:
# Key names are the same as the keyword arguments required by boto
params = {
'ImageId': image_id_to_use,
'InstanceType': instance_type_to_launch,
'MinCount': 1,
'MaxCount': 1,
'UserData': user_data_script,
'SecurityGroups': ['your groups'],
'KeyName': 'yourkeyname',
}
# Run the instance and wait for it to start
reservation = ec2.client.run_instances(**params)
instance = ec2.resource.Instance(reservation['Instances'][0]['InstanceId'])
instance.wait_until_running()
# Also wait status checks to complete
waiter = ec2.client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance.id])
# Apply the IAM roles required (this instance will need access to, e.g., S3)
response = ec2.client.associate_iam_instance_profile(
IamInstanceProfile={
'Arn': 'your_arn',
'Name': 'ApplicableRoleEGAdministratorAccess'
},
InstanceId=instance.id
)
print('Instance id just created:', instance.id)
print('Instances in the SSM instances list right now:')
print(ssm.client.describe_instance_information()['InstanceInformationList'])
将突出这个问题(如果存在 - 它肯定是为了我)。
这可能是由于执行UserData脚本所花费的时间(请参阅此SO帖子以了解有关等待用户数据完成的可能相关的讨论 ),但我无法分辨(没有比我愿意采取的更多努力) !)是否是那个,或者只是AWS更新其服务数据库所固有的时间。
为了解决这个问题,我写了一个短服务员(处理其他故障模式的超时异常),重复调用describe_instance_information(),直到实例id出现在列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.