AWS Python - Windows - 运行 创建实例后的脚本

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

我进步很大

我的导入是:

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

我可以使用以下方式连接到我的 aws:

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

我可以使用以下方式创建实例:

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'
    ]
    )

现在,当我尝试 运行 在我的新实例上执行命令时,我正在用头撞墙。

我正在尝试类似的东西:

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

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

但我回来了:

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'

通过 ssh 连接到我的 aws 实例对我来说是新事物,所以我不希望有 directory/file。

我需要什么才能到达那里?有什么我需要提前安装的吗?到底出了什么问题?

我觉得很近,但又很远!

任何帮助都会很棒。

如评论中所述,您不能直接通过 ssh 进入 windows。

所以回到您的初始要求:运行 创建实例后的脚本 我不会从您的 python 脚本中执行此操作,但是而是在 ec2 实例上使用脚本并在 ec2 实例启动时自动 运行ning

您可以 user data script 在您的实例上 运行。在 windows 个实例上,您可以 运行 基本 cmd 脚本或 powershell 脚本