amazon-web-services - 使用 boto3 Python 在 aws 中创建实例
问题描述
我正在尝试使用 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
推荐阅读
- perl - 如何关闭 Gnu 进程?
- php - 使用 PHP 库
- docker - Sentry On-Premise 无法创建超级用户
- sql - 如何计算分组的总和值
- tensorflow - 为什么 MobileNetV2 仅在移动设备上比 MobileNetV1 快?
- html - 如何在中间使用全尺寸搜索表单制作 bootstrap 3 响应式导航栏?
- vue.js - Twilio TaskRouter JS SDK 不工作
- java - 使用模板的 OrientDB 的 Spring-Data
- android - 如何在双卡上使用 PhoneStateListener 收听?
- iis - 限制特定域的 IIS 重写映射