mardi 28 juin 2016

AWS Python - Windows - Running a script after I create an instance

I have made much progress.

My imports are:

import boto.rds as rds
import boto3 as b3
import boto.ec2 as ec2
from boto.manage.cmdshell import sshclient_from_instance

I can connect to my aws using:

conn = boto.ec2.connect_to_region('us-west2',aws_access_key_id='MY_ID', aws_secret_access_key='MY_PASS')

I can create an instance using:

conn_args = {
    'aws_access_key_id': 'MY_KEY',
    'aws_secret_access_key': 'MY_PASS',
    'region_name': 'us-west-2'
}

ec2_res = b3.resource('ec2', **conn_args)

new_instance = ec2_res.create_instances(
    ImageId='ami-123456',
    MinCount=1,
    MaxCount=1,
    InstanceType='t2.micro',
    KeyName='my-keyname',
SecurityGroups=[
        'my-securitygroup'
    ]
    )

Now, when I try to run a command on my new instance I am banging my head against a wall.

I am trying something like:

ssh_client = sshclient_from_instance(instance,
                                     'C:\users\%USER%\aws\windows-west-keypair.pem',
                                     user_name='Administrator')

status, stdout, stderr = ssh_client.run('cd')

But I get back:

C:Users%USER%Minicondalibsite-packagesparamikohostkeys.pyc in load(self, filename)
     93         :raises IOError: if there was an error reading the file
     94         """
---> 95         with open(filename, 'r') as f:
     96             for lineno, line in enumerate(f, 1):
     97                 line = line.strip()

IOError: [Errno 2] No such file or directory: 'C:\Users\%USER%/.ssh/known_hosts'

Connecting via ssh to my aws instance is a new thing for me, so I do not expect there to be a directory/file.

What do I require to be there? Is there anything I need to install in advance? What exactly is going wrong?

I feel so close, yet so far!

Any help would be great.

Aucun commentaire:

Enregistrer un commentaire