My application is to use an link(url) to invoke lambda function ,then I want to know public IP of lambda and get page source. How could I get lambda public IP by using python? Thanks a lot.
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    10
            
            
        - 
                    1Welcome to StackOverflow. Please take the [tour](http://stackoverflow.com/tour) have a look around, and read through the [HELP center](http://stackoverflow.com/help), then read [How to Ask Question](http://stackoverflow.com/help/how-to-ask), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) – Dwhitz Feb 05 '18 at 09:24
 - 
                    Try https://gist.github.com/kixorz/3b172e2fc3ce35421ee9. The code is simple to adapt to Python – FelixEnescu Feb 05 '18 at 11:16
 
4 Answers
13
            You can curl to checkip.amazonaws.com to get the public IP.
import requests
requests.get('http://checkip.amazonaws.com').text.rstrip()
Output:
52.x.147.64
        helloV
        
- 50,176
 - 7
 - 137
 - 145
 
6
            
            
        import urllib3
http = urllib3.PoolManager()
response = http.request('GET', 'http://checkip.amazonaws.com')
response.__dict__
IP address found in the '_body' attribute.
Alternative solution due to:
- unable to use 
requestsas it's not part of the core - botocore vendored version removed by AWS found here
 
        John
        
- 111
 - 1
 - 5
 
3
            
            
        I would suggest:
from botocore.vendored import requests
requests.get('http://checkip.amazonaws.com').text.rstrip()
inside your lambda function.
Otherwise you may get an error that says that lambda cannot find requests unless you created your lambda from a .zip file that includes requests installed via pip
        Leo Skhrnkv
        
- 1,513
 - 16
 - 27
 
- 
                    1Thanks a lot.Just like you said,I created my lambda function from a .zip file containing requests installed via pip. – Rick.Wang Jul 09 '18 at 02:28
 - 
                    This solution no longer works because the vendored version of requests has been removed from botocore. https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/ – Khorkrak Apr 10 '20 at 14:42
 - 
                    it works but with DeprecationWarning: This dependency was removed from Botocore and will be removed from Lambda after 2021-12-01. https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/. Install the requests package, 'import requests' directly, and use the requests.get() function instead. – SAndriy Aug 07 '21 at 20:55
 
1
            
            
        Instead of import requests, you can use urllib that does not require pip.
The sample code is below;
from urllib.request import Request, urlopen
def lambda_handler(event, context):
    url = 'http://checkip.amazonaws.com'
    with urlopen(Request(url)) as response:
        print(response.read().decode('utf-8'))
        LittleWat
        
- 53
 - 6