0

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%\Miniconda\lib\site-packages\paramiko\hostkeys.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.

jason m
  • 6,519
  • 20
  • 69
  • 122
  • Is it a Windows Instance? – Piyush Patil Jun 27 '16 at 01:38
  • @error2007s Yes it is a windows instance. – jason m Jun 27 '16 at 01:39
  • You cannot Ssh into a Windows instance using a pem file check this steps to connect to AWS Windows EC2 instance http://docs.aws.amazon.com/opsworks/latest/userguide/workinginstances-rdp.html – Piyush Patil Jun 27 '16 at 01:42
  • @error2007s Sorry it wasn't clear from the question but I want to run the script on the ec2 instance programatically. Meaning - after I create the instance I want to say `instance.run_command('python my_python_script.py')` - meaning - how do I access to command line programatically? – jason m Jun 27 '16 at 01:44
  • I guess there is a error the way you are running script in widows check this question http://stackoverflow.com/questions/1934675/how-to-execute-python-scripts-in-windows – Piyush Patil Jun 27 '16 at 01:49
  • @error2007s but you are totally missing the question. how do I access the command line via boto/boto3? – jason m Jun 27 '16 at 01:50

1 Answers1

0

As mentioned in the comments you cannot directly ssh into windows.

so to come back on your initial requirement : Running a script after I create an instance I would not do that from your python script but instead have script on the ec2 instance and have them automatically running on ec2 instance startup

You can have user data script that will run on your instance. on windows instance, you can run basic cmd script or powershell script

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139