[英]How to create an ec2 instance using boto3
Is it possible to create an ec2 instance using boto3 in python? 是否可以在python中使用boto3创建ec2实例? Boto3 document is not helping here, and I couldn't find any helping documents online.
Boto3文档在这里没有帮助,我在网上找不到任何帮助文档。 please provide some sample codes/links.
请提供一些示例代码/链接。
The API has changed but it's right there in the documentation API已更改,但它正好在文档中
# Boto 3
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5)
Link to the documentation: http://boto3.readthedocs.org/en/latest/guide/migrationec2.html#launching-new-instances 链接到文档: http : //boto3.readthedocs.org/en/latest/guide/migrationec2.html#launching-new-instances
The link you're really looking for in the documentation is the create_instances()
method of the ServiceResource object . 您在文档中真正寻找的链接是ServiceResource对象的
create_instances()
方法 。 This is the type of object you are calling if you create an EC2 resource like this: 如果您创建这样的EC2资源,这是您正在调用的对象类型:
s = boto3.Session(region_name="us-west-1")
ec2 = s.resource('ec2')
...
instance = ec2.create_instances(**y_kwargs)
This contains a more detailed example and a longer list of available parameters. 这包含更详细的示例和更长的可用参数列表。
You can also get parameter values for AWS instances that are already running using the AWS command line interface: 您还可以使用AWS命令行界面获取已在运行的AWS实例的参数值:
$ aws ec2 describe-instances
This prints out a JSON file from which relevant parameters can be extracted and passed to the create_instances()
method. 这将打印出一个JSON文件,从中可以提取相关参数并将其传递给
create_instances()
方法。 (Or, you can use a boto client and call the describe_instances()
method .) (或者,您可以使用boto客户端并调用
describe_instances()
方法 。)
(Note: If you're wondering what the difference is between the Client and the Resource, they serve different purposes for the same end - the client is a lower-level interface while the Resource is a higher-level interface.) (注意:如果您想知道客户端和资源之间的区别是什么,它们为同一端服务的目的不同 - 客户端是较低级别的接口,而资源是较高级别的接口。)
Refer to API docs has all available options to create instance 请参阅API文档具有创建实例的所有可用选项
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Subnet.create_instances http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Subnet.create_instances
You can run the code I used from the boto3 docs . 您可以运行我在boto3文档中使用的代码。 You can add or remove parameters as per your requirements, but this is what you would normally require:
您可以根据自己的要求添加或删除参数,但这是您通常需要的:
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',
],
)
If your running from your windows computer you need configure AWS Cli with proper EC2 permisssion to launch instance. 如果从Windows计算机运行,则需要使用适当的EC2 permisssion配置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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.