Amazon web services 使用boto3 Python在aws中创建实例

Amazon web services 使用boto3 Python在aws中创建实例,amazon-web-services,boto3,Amazon Web Services,Boto3,我正在尝试使用boto3在aws中创建一个实例 instance = ec2.create_instances( ImageId='ami-15e9c770', MinCount=1, MaxCount=1, InstanceType='t2.micro', KeyName="xyz", Placement={'AvailabilityZone':'us-east-2b'}) 我使用的代码创建了一个实例,但它不允许我连接到该实例。我在某个地方出错了吗?我还需要做哪些事情才能通过ssh连接到该实

我正在尝试使用boto3在aws中创建一个实例

instance = ec2.create_instances(
ImageId='ami-15e9c770',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName="xyz",
Placement={'AvailabilityZone':'us-east-2b'})

我使用的代码创建了一个实例,但它不允许我连接到该实例。我在某个地方出错了吗?我还需要做哪些事情才能通过ssh连接到该实例。

使用此脚本连接到您的实例

import boto3
import botocore
import paramiko

key = paramiko.RSAKey.from_private_key_file(path/to/mykey.pem)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect/ssh to an instance
try:
    # Here 'ubuntu' is user name and 'instance_ip' is public IP of EC2
    client.connect(hostname=instance_ip, username="ubuntu", pkey=key)

    # Execute a command(cmd) after connecting/ssh to an instance
    stdin, stdout, stderr = client.exec_command(cmd)
    print stdout.read()

    # close the client connection once the job is done
    client.close()
    break

except Exception, e:
    print e

但是它与这个命令相同:ssh-ikey.pemuser@ip. 这不起作用它会导致连接超时对实例ssh进行故障排除并重试Hanks Mausam事实上我找到了解决此问题的方法。您还必须在我的代码中添加安全组参数,然后才能连接到实例。您所说的“不允许我连接到实例”是什么意思?你是如何尝试联系的,发生了什么?您是否已将安全组与实例关联?如果没有,您需要这样做,允许适当的连接(例如端口22=SSH)。由于您没有指定VPC id、子网id、安全组,api只是将所有内容放在默认的VPC、子网下。安全组。您应该检查连接部件上的默认设置。