Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I retrieve AWS Lambda public IP address by using Python?

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.

like image 400
Rick.Wang Avatar asked Feb 05 '18 09:02

Rick.Wang


People also ask

Does Lambda have a public IP?

No, Lambda functions do not get a public IP, regardless of the auto-assign IPv4 address setting. They cannot have public IPs. To reach the internet, they must route through a NAT (which routes to an IGW).

Does AWS Lambda have NumPy?

AWS Lambda does not include Pandas/NumPy Python libraries by default. How use Pandas and NumPy with Lambda functions?


2 Answers

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
like image 199
helloV Avatar answered Sep 16 '22 12:09

helloV


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

like image 22
Leo Skhrnkv Avatar answered Sep 18 '22 12:09

Leo Skhrnkv