boto3 how to retrieve the elastic ip from an instance









up vote
0
down vote

favorite












I need to do the following:



I have some instances that contain a certain tag, I need to loop through those instances and for each instance which contains that certain tag, if that instance has an elastic ip attached, I need to tag that elastic ip with that same tag. My code is the following:



import boto3
import json

region_list = ['us-east-1']
session = boto3.Session(profile_name='default')

for region in region_list:
ec2 = session.resource('ec2',region)
client = boto3.client('ec2',region)
# Retrieve instances that contain this specific tag
instances = ec2.instances.filter(Filters=['Name':'tag:MyTargetTag', 'Values':['*']])

for instance in instances:
for tag in instance.tags:
if tag['Key'] == "MyTargetTag":
MyTargetTag = tag['Value']
## check if this instance has an elasticip
## if it has, assign the value of MyTargetTag to it
response = client.add_tags(
ResourceArns=[
#elasticip ip identifier of some sort,
],
Tags=[

'Key': 'MyTargetTag',
'Value': MyTargetTag
,
]
)


I've read through the docs and videos and what not but honestly I don't understand it quite completely and I'm just doing trial and error without any success.










share|improve this question

























    up vote
    0
    down vote

    favorite












    I need to do the following:



    I have some instances that contain a certain tag, I need to loop through those instances and for each instance which contains that certain tag, if that instance has an elastic ip attached, I need to tag that elastic ip with that same tag. My code is the following:



    import boto3
    import json

    region_list = ['us-east-1']
    session = boto3.Session(profile_name='default')

    for region in region_list:
    ec2 = session.resource('ec2',region)
    client = boto3.client('ec2',region)
    # Retrieve instances that contain this specific tag
    instances = ec2.instances.filter(Filters=['Name':'tag:MyTargetTag', 'Values':['*']])

    for instance in instances:
    for tag in instance.tags:
    if tag['Key'] == "MyTargetTag":
    MyTargetTag = tag['Value']
    ## check if this instance has an elasticip
    ## if it has, assign the value of MyTargetTag to it
    response = client.add_tags(
    ResourceArns=[
    #elasticip ip identifier of some sort,
    ],
    Tags=[

    'Key': 'MyTargetTag',
    'Value': MyTargetTag
    ,
    ]
    )


    I've read through the docs and videos and what not but honestly I don't understand it quite completely and I'm just doing trial and error without any success.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to do the following:



      I have some instances that contain a certain tag, I need to loop through those instances and for each instance which contains that certain tag, if that instance has an elastic ip attached, I need to tag that elastic ip with that same tag. My code is the following:



      import boto3
      import json

      region_list = ['us-east-1']
      session = boto3.Session(profile_name='default')

      for region in region_list:
      ec2 = session.resource('ec2',region)
      client = boto3.client('ec2',region)
      # Retrieve instances that contain this specific tag
      instances = ec2.instances.filter(Filters=['Name':'tag:MyTargetTag', 'Values':['*']])

      for instance in instances:
      for tag in instance.tags:
      if tag['Key'] == "MyTargetTag":
      MyTargetTag = tag['Value']
      ## check if this instance has an elasticip
      ## if it has, assign the value of MyTargetTag to it
      response = client.add_tags(
      ResourceArns=[
      #elasticip ip identifier of some sort,
      ],
      Tags=[

      'Key': 'MyTargetTag',
      'Value': MyTargetTag
      ,
      ]
      )


      I've read through the docs and videos and what not but honestly I don't understand it quite completely and I'm just doing trial and error without any success.










      share|improve this question













      I need to do the following:



      I have some instances that contain a certain tag, I need to loop through those instances and for each instance which contains that certain tag, if that instance has an elastic ip attached, I need to tag that elastic ip with that same tag. My code is the following:



      import boto3
      import json

      region_list = ['us-east-1']
      session = boto3.Session(profile_name='default')

      for region in region_list:
      ec2 = session.resource('ec2',region)
      client = boto3.client('ec2',region)
      # Retrieve instances that contain this specific tag
      instances = ec2.instances.filter(Filters=['Name':'tag:MyTargetTag', 'Values':['*']])

      for instance in instances:
      for tag in instance.tags:
      if tag['Key'] == "MyTargetTag":
      MyTargetTag = tag['Value']
      ## check if this instance has an elasticip
      ## if it has, assign the value of MyTargetTag to it
      response = client.add_tags(
      ResourceArns=[
      #elasticip ip identifier of some sort,
      ],
      Tags=[

      'Key': 'MyTargetTag',
      'Value': MyTargetTag
      ,
      ]
      )


      I've read through the docs and videos and what not but honestly I don't understand it quite completely and I'm just doing trial and error without any success.







      amazon-ec2 boto3 elastic-ip






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 20:15









      Garo San

      103




      103






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You may access VPC or classic Elastic IPs on the boto3.resource('ec2') resources vpc_addresses and classic_addresses respectively.



          If the addresses are associated, they will have an instance_id attribute. You'll be able to get the tags with ec2.Instance(address.instance_id).tags



          If you want to go through all addresses, the boto3.client('ec2') client has describe_addresses that will give you the same information.






          share|improve this answer




















          • Yes, this was helpful thanks!
            – Garo San
            Nov 14 at 17:50

















          up vote
          0
          down vote













          ElasticIP addresses are 'associated' with a network interface. One instance may have multiple network interfaces, each of which may or may not have an Elastic IP address assigned to it.



          This lists all the ElasticIPs attached to any of your instances, for example:



          import boto3 
          from pprint import pprint

          ec2 = boto3.resource('ec2')

          instances = ec2.instances.filter()
          for instance in instances:
          for interface in instance.network_interfaces:
          if interface.association_attribute is not None:
          pprint (interface.association_attribute)


          However I can't see any functionality in boto3 that allows you to return the ElasticIP as an object, or apply any tags to it. It's clearly possible, as you can do it via the console, I just can't see the functionality in boto3.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215469%2fboto3-how-to-retrieve-the-elastic-ip-from-an-instance%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You may access VPC or classic Elastic IPs on the boto3.resource('ec2') resources vpc_addresses and classic_addresses respectively.



            If the addresses are associated, they will have an instance_id attribute. You'll be able to get the tags with ec2.Instance(address.instance_id).tags



            If you want to go through all addresses, the boto3.client('ec2') client has describe_addresses that will give you the same information.






            share|improve this answer




















            • Yes, this was helpful thanks!
              – Garo San
              Nov 14 at 17:50














            up vote
            1
            down vote



            accepted










            You may access VPC or classic Elastic IPs on the boto3.resource('ec2') resources vpc_addresses and classic_addresses respectively.



            If the addresses are associated, they will have an instance_id attribute. You'll be able to get the tags with ec2.Instance(address.instance_id).tags



            If you want to go through all addresses, the boto3.client('ec2') client has describe_addresses that will give you the same information.






            share|improve this answer




















            • Yes, this was helpful thanks!
              – Garo San
              Nov 14 at 17:50












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You may access VPC or classic Elastic IPs on the boto3.resource('ec2') resources vpc_addresses and classic_addresses respectively.



            If the addresses are associated, they will have an instance_id attribute. You'll be able to get the tags with ec2.Instance(address.instance_id).tags



            If you want to go through all addresses, the boto3.client('ec2') client has describe_addresses that will give you the same information.






            share|improve this answer












            You may access VPC or classic Elastic IPs on the boto3.resource('ec2') resources vpc_addresses and classic_addresses respectively.



            If the addresses are associated, they will have an instance_id attribute. You'll be able to get the tags with ec2.Instance(address.instance_id).tags



            If you want to go through all addresses, the boto3.client('ec2') client has describe_addresses that will give you the same information.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 14:39









            Vince

            58539




            58539











            • Yes, this was helpful thanks!
              – Garo San
              Nov 14 at 17:50
















            • Yes, this was helpful thanks!
              – Garo San
              Nov 14 at 17:50















            Yes, this was helpful thanks!
            – Garo San
            Nov 14 at 17:50




            Yes, this was helpful thanks!
            – Garo San
            Nov 14 at 17:50












            up vote
            0
            down vote













            ElasticIP addresses are 'associated' with a network interface. One instance may have multiple network interfaces, each of which may or may not have an Elastic IP address assigned to it.



            This lists all the ElasticIPs attached to any of your instances, for example:



            import boto3 
            from pprint import pprint

            ec2 = boto3.resource('ec2')

            instances = ec2.instances.filter()
            for instance in instances:
            for interface in instance.network_interfaces:
            if interface.association_attribute is not None:
            pprint (interface.association_attribute)


            However I can't see any functionality in boto3 that allows you to return the ElasticIP as an object, or apply any tags to it. It's clearly possible, as you can do it via the console, I just can't see the functionality in boto3.






            share|improve this answer
























              up vote
              0
              down vote













              ElasticIP addresses are 'associated' with a network interface. One instance may have multiple network interfaces, each of which may or may not have an Elastic IP address assigned to it.



              This lists all the ElasticIPs attached to any of your instances, for example:



              import boto3 
              from pprint import pprint

              ec2 = boto3.resource('ec2')

              instances = ec2.instances.filter()
              for instance in instances:
              for interface in instance.network_interfaces:
              if interface.association_attribute is not None:
              pprint (interface.association_attribute)


              However I can't see any functionality in boto3 that allows you to return the ElasticIP as an object, or apply any tags to it. It's clearly possible, as you can do it via the console, I just can't see the functionality in boto3.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                ElasticIP addresses are 'associated' with a network interface. One instance may have multiple network interfaces, each of which may or may not have an Elastic IP address assigned to it.



                This lists all the ElasticIPs attached to any of your instances, for example:



                import boto3 
                from pprint import pprint

                ec2 = boto3.resource('ec2')

                instances = ec2.instances.filter()
                for instance in instances:
                for interface in instance.network_interfaces:
                if interface.association_attribute is not None:
                pprint (interface.association_attribute)


                However I can't see any functionality in boto3 that allows you to return the ElasticIP as an object, or apply any tags to it. It's clearly possible, as you can do it via the console, I just can't see the functionality in boto3.






                share|improve this answer












                ElasticIP addresses are 'associated' with a network interface. One instance may have multiple network interfaces, each of which may or may not have an Elastic IP address assigned to it.



                This lists all the ElasticIPs attached to any of your instances, for example:



                import boto3 
                from pprint import pprint

                ec2 = boto3.resource('ec2')

                instances = ec2.instances.filter()
                for instance in instances:
                for interface in instance.network_interfaces:
                if interface.association_attribute is not None:
                pprint (interface.association_attribute)


                However I can't see any functionality in boto3 that allows you to return the ElasticIP as an object, or apply any tags to it. It's clearly possible, as you can do it via the console, I just can't see the functionality in boto3.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 8:10









                weegolo

                414




                414



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215469%2fboto3-how-to-retrieve-the-elastic-ip-from-an-instance%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)