Question

I'm learning Fabric. I want to achieve the following: ssh into my EC2 machine and ls the home directory.

I have started with the following:

from boto import ec2

from fabric.colors import green as _green, yellow as _yellow


class EC2Conn:
    def __init__(self):
        print(_green("Started..."))
        self.ec2conn = None
        self.user = 'fabUser'
        self.access_key = 'xxxx'
        self.secret_key = 'xxxx'

    def connect(self):
        print(_green("Connecting..."))
        ec2.connect_to_region("eu-west-1a")
        self.ec2conn = ec2.connection.EC2Connection(self.access_key, self.secret_key)

        print(self.get_instances())


    def get_instances(self):
        return self.ec2conn.get_all_instances()


def run_me():
    a = EC2Conn()
    a.connect()

But this gives me a blank list [] I do have 1 instance running so this is incorrect.

Was it helpful?

Solution

Try to change little bit in your code as following

self.ec2conn = ec2.connect_to_region('eu-west-1', 
                  aws_access_key_id=self.access_key,
                  aws_secret_access_key=self.secret_key)

print(self.get_instances())

Or

region = ec2.get_region('eu-west-1')
self.ec2conn = ec2.connection.EC2Connection(self.access_key,
                  self.secret_key, region=region)

print(self.get_instances())

Remember that ec2.connect_to_region and ec2.connection.EC2Connection both returns ec2.connection.EC2Connection object. Refer here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top