19

特定のサブネットで新しいマシンを起動するec2.run_instances必要がありますが、パブリック IP を自動で割り当てる必要があります (固定のエラスティック IP ではありません)。

インスタンスのリクエスト (インスタンスの詳細) を介して Amazon の Web EC2 マネージャーから新しいマシンを起動すると、パブリック IP を割り当ててパブリック IPを自動割り当てするというチェックボックスがあります。スクリーンショットで強調表示されていることを確認してください。

インスタンスのリクエストウィザード

でそのチェックボックス機能を実現するにはどうすればよいbotoですか?

4

3 に答える 3

39

興味深いことに、この問題を抱えている人はそれほど多くないようです。私にとって、これを正しく行うことができることは非常に重要でした。この機能がないと、 に起動されたインスタンスからインターネットにアクセスできませんnondefault subnet

最近修正された関連するバグがありました。 https://github.com/boto/boto/pull/1705を参照してください。

の代わりにsubnet_idと セキュリティgroupsをネットワーク インターフェイスに提供する必要があることに注意してください。NetworkInterfaceSpecificationrun_instance

import time
import boto
import boto.ec2.networkinterface

from settings.settings import AWS_ACCESS_GENERIC

ec2 = boto.connect_ec2(*AWS_ACCESS_GENERIC)

interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(subnet_id='subnet-11d02d71',
                                                                    groups=['sg-0365c56d'],
                                                                    associate_public_ip_address=True)
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)

reservation = ec2.run_instances(image_id='ami-a1074dc8',
                                instance_type='t1.micro',
                                #the following two arguments are provided in the network_interface
                                #instead at the global level !!
                                #'security_group_ids': ['sg-0365c56d'],
                                #'subnet_id': 'subnet-11d02d71',
                                network_interfaces=interfaces,
                                key_name='keyPairName')

instance = reservation.instances[0]
instance.update()
while instance.state == "pending":
    print instance, instance.state
    time.sleep(5)
    instance.update()

instance.add_tag("Name", "some name")

print "done", instance
于 2013-09-27T12:27:25.860 に答える
8

boto3 には、DeviceIndex=0 に設定できる NetworkInterfaces があり、代わりに Subnet と SecurityGroupIds をインスタンス レベルからこのブロックに移動する必要があります。ここに私のための作業バージョンがあります、

def launch_instance(ami_id, name, type, size, ec2):
   rc = ec2.create_instances(
    ImageId=ami_id,
    MinCount=1,
    MaxCount=1,
    KeyName=key_name,
    InstanceType=size,
    NetworkInterfaces=[
        {
            'DeviceIndex': 0,
            'SubnetId': subnet,
            'AssociatePublicIpAddress': True,
            'Groups': sg
        },
    ]
   )

   instance_id = rc[0].id
   instance_name = name + '-' + type
   ec2.create_tags(
    Resources = [instance_id],
    Tags = [{'Key': 'Name', 'Value': instance_name}]
   )

   return (instance_id, instance_name)
于 2016-06-16T19:53:48.740 に答える