Python 如何使用 boto3 创建 ec2 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32863768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:24:14  来源:igfitidea点击:

How to create an ec2 instance using boto3

pythonamazon-web-servicesamazon-ec2botoboto3

提问by MikA

Is it possible to create an ec2 instance using boto3 in python? Boto3 document is not helping here, and I couldn't find any helping documents online. please provide some sample codes/links.

是否可以在 python 中使用 boto3 创建一个 ec2 实例?Boto3 文档在这里没有帮助,我在网上找不到任何帮助文档。请提供一些示例代码/链接。

采纳答案by gbs

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

回答by ranjeetcao

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

回答by charlesreid1

The link you're really looking for in the documentation is the create_instances()method of the ServiceResource object. This is the type of object you are calling if you create an EC2 resource like this:

您在文档中真正要查找的链接是create_instances()ServiceResource 对象方法。如果您像这样创建 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. (Or, you can use a boto client and call the describe_instances()method.)

这会打印出一个 JSON 文件,可以从中提取相关参数并将其传递给create_instances()方法。(或者,您可以使用 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.)

(注意:如果您想知道 Client 和 Resource 之间的区别是什么,它们为同一端提供不同的用途 - 客户端是较低级别的接口,而 Resource 是较高级别的接口。)

回答by captainblack

You can run the code I used from the boto3 docs. You can add or remove parameters as per your requirements, but this is what you would normally require:

您可以运行我从boto3 docs使用的代码。您可以根据您的要求添加或删除参数,但这是您通常需要的:

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',
    ],
)

回答by Sonoo Kumar

If your running from your windows computer you need configure AWS Cli with proper EC2 permisssion to launch instance.

如果您从 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)