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

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

5 Comments

Where should the Key ID and Secret Access Key go?
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.
Thanks looks like I am where i wanted to be now.
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`
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.