Skip to content

Instantly share code, notes, and snippets.

@jimbo8098
Created August 5, 2023 11:29
Show Gist options
  • Save jimbo8098/9e9b1fa0ef24be88c82adb7b2ab843a9 to your computer and use it in GitHub Desktop.
Save jimbo8098/9e9b1fa0ef24be88c82adb7b2ab843a9 to your computer and use it in GitHub Desktop.
Verify network connection in Lambda environment

Send payload:

{
  "request": "<url>"
}

The lambda will request the address and return the output in it's logs.

import requests
def handler(event,context):
print("Requesting {}".format(event["request"]))
try:
res = requests.get(event["request"], timeout = 10)
except requests.ConnectionError as ex:
print("A connection error occurred")
print(ex)
exit(1)
except requests.exceptions.RequestException as ex:
print("A generic request library error occurred")
print(ex)
exit(1)
except Exception as ex:
print("Exception type: {}".format(type(ex).__name__))
print("An error occurred while contacting the server")
exit(1)
print("Status Code: {} [{}]".format(res.status_code,res.reason))
if res.ok:
print("Response is okay")
print(res.content)
else:
print("Response is not okay")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment