Skip to content

Instantly share code, notes, and snippets.



Last active Dec 14, 2018
What would you like to do?
lambda for DNS lookups
Platform: Lambda - Python 2.7
This function is designed to troubleshoot lambda DNS resolution
and log the current source IP of a lambda request.
Change the host var to whatever you want to test and go.
import urllib2
import socket
import logging
logger = logging.getLogger()
def lambda_handler(event, context):
host = ''
addr1 = socket.gethostbyname(host)
url = 'https://' + host
response = {
'host': host,
'socket_resolved_addr': addr1,
'response': '',
'response_resolved_addr': '',
'my_public_ip': ''
whatipurl = ""
request = urllib2.urlopen(whatipurl)
pubip ='\n')
response['my_public_ip'] = pubip"My public ip is %s" % pubip)
# Test TCP exchange"Attempting to TCP connect to {}".format(addr1))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr1, 443))"Successfully did a socket to {}:{}".format(addr1, 443))
# Test HTTPS"I think I'm about to make a request to %s" % addr1)
request = urllib2.Request(url, None,)
data = urllib2.urlopen(request)
actual_responder = data.fp._sock.fp._sock.getpeername()[0]"I actually made a successful request to %s" % actual_responder)
actual_response ="Actual response: '%s'" % actual_response)
response['response_resolved_addr'] = actual_responder
response['response'] = actual_response
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment