Created
October 5, 2020 00:47
-
-
Save kangks/49aaf9c273ddefce68fba75ade80231a to your computer and use it in GitHub Desktop.
AWS Lambda getting public IP address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib3 | |
def lambda_handler(event, context): | |
return { | |
'statusCode': 200, | |
'body': get_public_ip() | |
} | |
def get_public_ip(): | |
try: | |
http = urllib3.PoolManager() | |
r = http.request('GET', "http://jsonip.com/") | |
response = json.loads(r.data) | |
return response["ip"] | |
except Exception as e: | |
print("Exception:", e) | |
return None | |
if __name__ == "__main__": | |
print(get_public_ip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment