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:
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.
You can configure a Lambda function to connect to private subnets in a virtual private cloud (VPC) in your AWS account. Use Amazon Virtual Private Cloud (Amazon VPC) to create a private network for resources such as databases, cache instances, or internal services.
Lambda doesn't support running functions in dedicated tenancy VPCs. To connect a Lambda function to a dedicated VPC, first peer the dedicated VPC to a default tenancy VPC that contains the function. The solution requires using an Amazon Elastic Compute Cloud (Amazon EC2) Dedicated Instance.
Be sure that all the subnets you configure for your Lambda function are private subnets. It is a common mistake to configure, for example, 1 private subnet and 1 public subnet. This will result in your Lambda function working OK sometimes and failing at other times without any obvious cause.
The Lambda function's security group has no rules whatsoever. None are required. It is merely a placeholder for the Lambda function that allows us to specify the Lambda function as source in our other EC2 security groups.
It works fine for me.
I did the following:
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?).
Try to configure the Lambda in your vpc to access the private hosted zone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With