使用Python API Boto简单地创建AWS实例?

时间:2016-01-04 17:15:46

标签: python amazon-web-services boto

我已经在互联网上查找了不同的方法,但我似乎可以开始工作。我知道它可能是一个简单的代码块,可以做到这一点,但我无法弄明白。如果您愿意分享一些代码以便我完成这项任务,我将不胜感激。谢谢你的时间。

1 个答案:

答案 0 :(得分:-1)

您可以尝试以下代码开始。请注意,只有AMI ID, MinCount & Max Count是启动EC2实例所需的必备参数,其他值(如Subnet ID, Key Pair, Instance Size etc.)也可以指定用于实时方案。

import boto3

ec2 = boto3.session.Session(aws_access_key_id='abcedfgh', aws_secret_access_key='abcedfgh', region_name='us-east-1')

response = client.run_instances(
    ImageId='ami-12345678',
    MinCount=1,
    MaxCount=1
)

print response

boto3 - EC2运行实例文档[http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances]中提供了相同输入的完整存根。

response = client.run_instances(
    DryRun=True|False,
    ImageId='string',
    MinCount=123,
    MaxCount=123,
    KeyName='string',
    SecurityGroups=[
        'string',
    ],
    SecurityGroupIds=[
        'string',
    ],
    UserData='string',
    InstanceType='t1.micro'|'m1.small'|'m1.medium'|'m1.large'|'m1.xlarge'|'m3.medium'|'m3.large'|'m3.xlarge'|'m3.2xlarge'|'m4.large'|'m4.xlarge'|'m4.2xlarge'|'m4.4xlarge'|'m4.10xlarge'|'t2.micro'|'t2.small'|'t2.medium'|'t2.large'|'m2.xlarge'|'m2.2xlarge'|'m2.4xlarge'|'cr1.8xlarge'|'i2.xlarge'|'i2.2xlarge'|'i2.4xlarge'|'i2.8xlarge'|'hi1.4xlarge'|'hs1.8xlarge'|'c1.medium'|'c1.xlarge'|'c3.large'|'c3.xlarge'|'c3.2xlarge'|'c3.4xlarge'|'c3.8xlarge'|'c4.large'|'c4.xlarge'|'c4.2xlarge'|'c4.4xlarge'|'c4.8xlarge'|'cc1.4xlarge'|'cc2.8xlarge'|'g2.2xlarge'|'cg1.4xlarge'|'r3.large'|'r3.xlarge'|'r3.2xlarge'|'r3.4xlarge'|'r3.8xlarge'|'d2.xlarge'|'d2.2xlarge'|'d2.4xlarge'|'d2.8xlarge',
    Placement={
        'AvailabilityZone': 'string',
        'GroupName': 'string',
        'Tenancy': 'default'|'dedicated'|'host',
        'HostId': 'string',
        'Affinity': 'string'
    },
    KernelId='string',
    RamdiskId='string',
    BlockDeviceMappings=[
        {
            'VirtualName': 'string',
            'DeviceName': 'string',
            'Ebs': {
                'SnapshotId': 'string',
                'VolumeSize': 123,
                'DeleteOnTermination': True|False,
                'VolumeType': 'standard'|'io1'|'gp2',
                'Iops': 123,
                'Encrypted': True|False
            },
            'NoDevice': 'string'
        },
    ],
    Monitoring={
        'Enabled': True|False
    },
    SubnetId='string',
    DisableApiTermination=True|False,
    InstanceInitiatedShutdownBehavior='stop'|'terminate',
    PrivateIpAddress='string',
    ClientToken='string',
    AdditionalInfo='string',
    NetworkInterfaces=[
        {
            'NetworkInterfaceId': 'string',
            'DeviceIndex': 123,
            'SubnetId': 'string',
            'Description': 'string',
            'PrivateIpAddress': 'string',
            'Groups': [
                'string',
            ],
            'DeleteOnTermination': True|False,
            'PrivateIpAddresses': [
                {
                    'PrivateIpAddress': 'string',
                    'Primary': True|False
                },
            ],
            'SecondaryPrivateIpAddressCount': 123,
            'AssociatePublicIpAddress': True|False
        },
    ],
    IamInstanceProfile={
        'Arn': 'string',
        'Name': 'string'
    },
    EbsOptimized=True|False
)