能否使用Python的Boto3创建EC2实例? Boto3文档在这方面没有帮助,我找不到任何有用的在线文档,请提供一些样本代码/链接。
能否使用Python的Boto3创建EC2实例? Boto3文档在这方面没有帮助,我找不到任何有用的在线文档,请提供一些样本代码/链接。
API已经改变,但它在文档中就在那里。
# Boto 3
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5)
链接到文档:
您可以从 boro3文档 运行我所用的代码。您可以根据自己的需求添加或删除参数,但这通常是您需要的内容:
import boto3
client = boto3.client('ec2', region_name='us-west-2')
response = client.run_instances(
BlockDeviceMappings=[
{
'DeviceName': '/dev/xvda',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': 8,
'VolumeType': 'gp2'
},
},
],
ImageId='ami-6cd6f714',
InstanceType='t3.micro',
MaxCount=1,
MinCount=1,
Monitoring={
'Enabled': False
},
SecurityGroupIds=[
'sg-1f39854x',
],
)
您真正需要查找的文档链接是ServiceResource对象的create_instances()
方法。如果您像这样创建EC2资源,则调用此类型的对象:
s = boto3.Session(region_name="us-west-1")
ec2 = s.resource('ec2')
...
instance = ec2.create_instances(**y_kwargs)
这里包含一个更详细的示例和可用参数的长列表。
您还可以使用AWS命令行界面获取已经运行的AWS实例的参数值:
$ aws ec2 describe-instances
create_instances()
方法。(或者,您可以使用boto客户端并调用describe_instances()
方法。)请参考 API 文档以获取创建实例的所有可用选项。
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Subnet.create_instances
如果您在Windows计算机上运行,则需要使用适当的EC2权限配置AWS Cli以启动实例。
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-5eb63a32',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
)
print(instance[0].id)
import boto3
ec2 = boto3.client('ec2', region_name='')
conn = ec2.run_instances(ImageId='<ami-image-id>',
InstanceType='<instance-type>',
MinCount=1,
MaxCount=5
)
SecurityGroups=['secgroup', ..]
关键字参数。这个 问题 似乎表明create_instances
在幕后运行经典的 boto2run_instances
,因此run_instances
的原始参数应该是有效的。请参阅文档。 - gbscreate_instances
中添加这个关键字参数:InstanceType="c3.xlarge"
。 - gbs