【问题标题】:How to access EMR master private ip address using pure python / boto如何使用纯 python/boto 访问 EMR master 私有 ip 地址
【发布时间】:2014-10-07 03:23:47
【问题描述】:

我在这个网站和谷歌上搜索过,但没有得到答案。

我有从 EC2 实例运行的代码,该实例使用 boto 创建和管理 EMR 集群。 我可以使用这个框架来获取flow_id(或cluster_id,不确定哪个是正确的名称),它以“j-”开头,并且有固定数量的字符来标识集群。

使用该框架,我可以建立一个 emr 或 ec2 连接,但对于我来说,我无法使用 boto 执行以下操作:

    aws emr --list-clusters --cluster-id=j-ASDFGHJKL | json '["instances"].[0].["privateipaddress"]

**上面有点捏造,我不记得json格式以及json命令是什么或它想要什么args,但还是cli。

我已经 pprint.pprint()'ed 并使用 inspect.getmembers() 检查了连接,将 conn 获取到特定的 cluster_id,但是我还没有看到这个字段/var/attribute 有或没有方法调用。

我一直在亚马逊和博托上下,他们是如何做到的 here?

    def test_list_instances(self): #line 317
        ...
        self.assertEqual(response.instances[0].privateipaddress , '10.0.0.60')
        ...

附:我试过this,但python抱怨“instances”属性不可迭代,数组可访问(我忘记了“var [0]”命名),以及我尝试过的其他东西,包括检查。 顺便说一句,我可以从这里访问 publicDNSaddress 和许多其他东西,只是不能访问 privateIP...

如果我在某个地方搞砸了,请告诉我在哪里可以找到答案,我正在使用子进程进行丑陋的修复!

【问题讨论】:

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


    【解决方案1】:

    如果您要求获取 emr 的主 IP,则以下命令将起作用:

    list_intance_resp =  boto3.client('emr',region_name='us-east-1').list_instances(ClusterId ='j-XXXXXXX')
    print list_intance_resp['Instances'][len(list_intance_resp['Instances'])-1]['PrivateIpAddress']
    

    【讨论】:

      【解决方案2】:

      使用检查您的 boto 版本 pip show boto 我的猜测是您使用的是 2.24 或更早版本,因为此版本未解析实例信息,请参阅 https://github.com/boto/boto/blob/2.24.0/tests/unit/emr/test_connection.py#L117 相比 https://github.com/boto/boto/blob/2.25.0/tests/unit/emr/test_connection.py#L313

      如果您将 boto 版本升级到 2.25 或更高版本,您将能够执行以下操作

      from boto.emr.connection import EmrConnection
      
      conn = EmrConnection(<your aws key>, <your aws secret key>)
      jobid = 'j-XXXXXXXXXXXXXX' # your job id
      response = conn.list_instances(jobid)
      
      for instance in response.instances:
          print instance.privateipaddress 
      

      【讨论】:

      • 对于 boto3,它将是 import boto3 client = boto3.client('emr', region_name='us-east-1') response = client.list_instances(ClusterId='j-1KWURUMOCK57F') print response
      【解决方案3】:

      您只需要借助 EMR 集群 ID 从主实例组中查询主实例。如果您有多个 master,则可以解析 boto3 输出,如果是第一个列出的 master,则获取 IP。

      您的 Boto3 执行环境应该有权访问 EMR 描述集群及其实例组。这里是

      emr_list_instance_rep = boto_client_emr.list_instances(
          ClusterId=cluster_id,
          InstanceGroupTypes=[
              'MASTER',
          ],
          InstanceStates=[
              'RUNNING',
          ]
      )
      
      return emr_list_instance_rep["Instances"][0]["PrivateIpAddress"]
      

      你可以在这里找到完整的 boto3 脚本及其解释https://scriptcrunch.com/script-retrieve-aws-emr-master-ip/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        相关资源
        最近更新 更多