1

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
Chris Chambers
  • 1,367
  • 21
  • 39

2 Answers2

1

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?

garnaat
  • 44,310
  • 7
  • 123
  • 103
  • Where should the Key ID and Secret Access Key go? – Lordphartmore Jan 23 '13 at 21:21
  • 1
    I have updated the code sample to show that. Better yet, you could put your credentials in a boto config file rather than having to pass them in each time. – garnaat Jan 23 '13 at 21:26
  • Thanks looks like I am where i wanted to be now. – Lordphartmore Jan 23 '13 at 21:55
  • I have used the code snippet that you have provided, still it gives me the same socket error ` File "/usr/lib64/python2.7/socket.py", line 556, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno -2] Name or service not known` – Gaurav Parashar Nov 10 '15 at 04:32
  • If you are getting that error you are either doing something wrong in creating your connection or your DNS is messed up. That error is saying that it can't find the host associated with the service endpoint. – garnaat Nov 10 '15 at 13:50
0

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

Geo
  • 11
  • 3