简体   繁体   中英

Python AWS BOTO error

So I am trying to get all instances to check their status via python.

I found a script on here that someone suggested that looks like this:

from boto.ec2.connection import EC2Connection

conn = EC2Connection('MY Key ID', 'Secret Access Key')

reservations = conn.get_all_instances()
instance = reservations.instances[0]
print instance.status

However every time I run this I get an error as follows:

File "/usr/lib/python2.7/dist-packages/boto/ec2/connection.py", line 466, in get_all_instances
, verb='POST')
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 882, in get_list
response = self.make_request(action, params, path, verb)
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 868, in make_request
return self._mexe(http_request)
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 794, in _mexe
raise e
socket.gaierror: Errno -2 Name or service not known

Rather than directly constructing the EC2Connection object, try something like this:

import boto.ec2
conn = boto.ec2.connect_to_region('us-east-1',
                                  aws_access_key_id='<access_key>',
                                  aws_secret_access_key='<secret_key>')

reservations = conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for instance in instances:
    print instance.id, instance.state

Does that work for you?

The boto--- documentation actually tells you to use the AWS CLI tools to do the credentials

specifically this article: https://boto3.readthedocs.io/en/latest/guide/quickstart.html

    If you have the AWS CLI installed, 
 then you can use it to configure your credentials file:   aws configure

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM