【问题标题】:How to auto assign public ip to EC2 instance with boto如何使用 boto 将公共 IP 自动分配给 EC2 实例
【发布时间】:2013-10-02 12:01:47
【问题描述】:

我必须在给定的子网中使用ec2.run_instances 启动一台新机器,而且还要自动分配一个公共 ip(不是固定的弹性 ip)。

当人们通过请求实例(实例详细信息)从 Amazon 的 Web EC2 管理器启动新机器时,会出现一个名为分配公共 IP 的复选框来自动分配公共 IP。 看到它在屏幕截图中突出显示:

如何使用boto 实现该复选框功能?

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto


    【解决方案1】:

    有趣的是,似乎没有多少人有这个问题。 对我来说,能够正确地做到这一点非常重要。如果没有此功能,则无法从启动到 nondefault subnet 的实例访问 Internet。

    boto 文档没有提供任何帮助,最近修复了一个相关的错误,请参阅:https://github.com/boto/boto/pull/1705

    需要注意的是subnet_id 和安全groups 必须提供给网络接口NetworkInterfaceSpecification 而不是run_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
    

    【讨论】:

    • 有人在执行此操作时遇到此错误吗? The associatePublicIPAddress parameter cannot be specified for a network interface with an ID.
    • 是的,如果你想要一个公共 IP,显然你不能先创建接口。我将为 boto3 添加一个反映这一点的答案。 [哦等等,我不需要。见 barryku]
    【解决方案2】:

    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)
    

    【讨论】:

      【解决方案3】:

      我自己从未使用过此功能,但run_instances 调用有一个名为network_interfaces 的参数。根据documentation,您可以在那里提供 IP 地址详细信息。

      【讨论】:

      • 确实必须使用network_interfaces,但遗憾的是文档没有帮助,我最终阅读了代码和amazon ec2 API。
      猜你喜欢
      • 1970-01-01
      • 2017-10-27
      • 2016-12-02
      • 2014-05-15
      • 2018-03-06
      • 2018-04-16
      • 2020-04-21
      • 2020-07-09
      • 2023-02-19
      相关资源
      最近更新 更多