Udi*_*erg 9 python instances amazon-web-services ssm
我正在尝试使用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"]})
Run Code Online (Sandbox Code Playgroud)
输出:
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:
Run Code Online (Sandbox Code Playgroud)
如果我正在尝试用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:
Run Code Online (Sandbox Code Playgroud)
有人知道怎么解决吗?
当您尝试访问的实例上没有安装SSM代理时,可能会发生这种情况.有关可以运行SSM命令的实例列表,请运行:
aws ssm describe-instance-information --output text
Run Code Online (Sandbox Code Playgroud)
从那里,您可以获取实例ID,然后send_command使用该实例运行该命令.
如AWS的故障排除指南中此处记录的,此错误的可能原因有多种。
接受的答案将aws ssm describe-instance-information检查在可用状态下均可用且已安装SSM代理的实例,以便在一行中涵盖多个故障排除步骤(不错;))。
如果您使用的boto3是相同的,则可以通过以下方法实现:
ssm.client.describe_instance_information()
Run Code Online (Sandbox Code Playgroud)
我不确定是否检查权限,但可以这样假设。如果列表中缺少您的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'])
Run Code Online (Sandbox Code Playgroud)
将突出显示此问题(如果存在-肯定是给我的)。
这可能是由于执行UserData脚本所花费的时间(请参阅此SO帖子,以了解有关等待用户数据完成的可能的讨论),但是我无法告诉您(没有付出比我愿意付出的更多努力) !)就是那样,或者仅仅是AWS更新其服务数据库所固有的时间。
为了解决这个问题,我编写了一个简短的服务员(带有超时异常以处理其他故障模式),该服务员反复调用describe_instance_information(),直到实例ID出现在列表中。
| 归档时间: |
|
| 查看次数: |
10343 次 |
| 最近记录: |