2016-01-04 59 views
-2

我已經在互聯網上看到了不同的做法,但我似乎可以得到任何工作。我知道這可能是一個簡單的代碼塊,可以做到這一點,但我無法弄清楚。如果您願意分享一些代碼,我將不勝感激,因此我可以完成此任務。謝謝你的時間。使用Python API Boto簡單創建AWS實例?

+2

請顯示您嘗試的方法,並解釋他們有什麼問題。 – GingerPlusPlus

+0

http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances – gregwhitaker

回答

0

您可以嘗試下面的代碼開始。請注意,只有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 
)