Skip to content

Instantly share code, notes, and snippets.

@kangks
Created October 5, 2020 00:47
Show Gist options
  • Save kangks/49aaf9c273ddefce68fba75ade80231a to your computer and use it in GitHub Desktop.
Save kangks/49aaf9c273ddefce68fba75ade80231a to your computer and use it in GitHub Desktop.
AWS Lambda getting public IP address
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