Resolving a private hostname within a lambda function

I've encountered problems with Lambda not being able to resolve the url like http://example.com:1234

I have to use the IP instead. I'm wondering how do I ensure that the url can be resolved, especially when the url I'm using is private. All google researches point me to Route 53, but there's no explanation on how exactly this should be done.

For more clarity:

  1. All I'm doing is using the Python requests and calling my elasticsearch to insert some data:

    response = requests.post(es_url, data=some_data, timeout=some_timeout)

where es_url is <ip>:9200/some_index/some_type/.

I want to change ip to a human-readable domain like my_es.example.com which works in my EC2 instance but I cannot resolve this name in lambda function.

  1. I believe I have my lambda function already connected to a VPC. I don't care about accessing public IP's. All I need is to access my ES which resides in the same VPC. Unless my setting is incorrect?

Solution 1:

It works fine for me.

I did the following:

  • Created a new VPC using the VPC Wizard (Public & Private Subnets, NAT Gateway)
  • Created a Lambda function (shown below) without a VPC connection
  • Tested -- it successfully resolved the domain name
  • Configured the Lambda function to use the private subnet in the new VPC
  • Tested -- successful again
  • Launched an ElastiCache server in the private subnet
  • Changed the Lambda function to instead resolve the DNS name of the ElastiCache server -- Success!

This is the Lambda (Python 3.6) function I used:

def lambda_handler(event, context):
    import socket

    data = socket.gethostbyname_ex('google.com')
    print (data)
    return

That worked with no VPC setting and also with the VPC configured to the private subnet.

I then ran it again with the name of the ElastiCache server:

def lambda_handler(event, context):
    import socket

    data = socket.gethostbyname_ex('stack.b155ae.0001.apse2.cache.amazonaws.com')
    print (data)
    return

It returned:

('stack.b155ae.0001.apse2.cache.amazonaws.com', [], ['10.0.1.168'])

So, resolution of an ElastiCache name from Lambda seems to work fine.

Your problem must lie with your Lambda or VPC configuration (did you change DHCP Options?).

Solution 2:

Try to configure the Lambda in your vpc to access the private hosted zone.