AWS Boto-如何创建实例并返回instance_id [英] aws boto - how to create instance and return instance_id

查看:154
本文介绍了AWS Boto-如何创建实例并返回instance_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个python脚本,可以在其中传递参数/输入以指定实例类型,然后再附加一个额外的EBS(如果需要).

I want to create a python script where I can pass arguments/inputs to specify instance type and later attach an extra EBS (if needed).

ec2 = boto3.resource('ec2','us-east-1')
hddSize = input('Enter HDD Size if you want extra space ')
instType = input('Enter the instance type ')

def createInstance():
    ec2.create_instances(
        ImageId=AMI, 
        InstanceType = instType,  
        SubnetId='subnet-31d3ad3', 
        DisableApiTermination=True,
        SecurityGroupIds=['sg-sa4q36fc'],
        KeyName='key'
     )
return instanceID; ## I know this does nothing

def createEBS():
    ebsVol = ec2.Volume(
        id = instanceID,
        volume_type = 'gp2', 
        size = hddSize
        )

现在,ec2.create_instances()可以返回ID还是我必须进行一次保留迭代?

Now, can ec2.create_instances() return ID or do I have to do an iteration of reservations?

还是执行ec2.create(instance_id)/返回instance_id?这里的文档尚不明确.

or do I do an ec2.create(instance_id) / return instance_id? The documentation isn't specifically clear here.

推荐答案

在boto3中,create_instances返回一个列表,以便获取在请求中创建的实例ID,如下工作:

in boto3, create_instances returns a list so to get instance id that was created in the request, following works:

ec2_client = boto3.resource('ec2','us-east-1')
response = ec2_client.create_instances(ImageId='ami-12345', MinCount=1, MaxCount=1)
instance_id = response[0].instance_id

这篇关于AWS Boto-如何创建实例并返回instance_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆