Home > Blockchain >  AWS Lambda Intermittent Connection Issues to External HTTP Requests
AWS Lambda Intermittent Connection Issues to External HTTP Requests

Time:07-14

I am currently building a script in AWS Lambda that requires it to send HTTP Post requests to an External API. However, occasionally the script loses connection and cannot send requests to anyone (the external site, other Lambda Function URLS, Google, etc) for about 20 minutes before it can send requests again. It just reaches the timeout point and retries a couple times. I also need to reach internal AWS services such as RDS.

I have tried using the AWS Systems Manager to run the AWSSupport-TroubleshootLambdaInternetAccess automation and that returns a success.

I have set up an Internet Gateway and a NAT Gateway as well as a 'public' and 'private' subnet. The public subnet is routed to the internet gateway and the private is routed to the NAT gateway. The Lambda is connected to both of those subnets.

Although, this question Why can't an AWS lambda function inside a public subnet in a VPC connect to the internet? says to have 2 privates and not 1 private, 1 public subnet, but if I do this I cannot access RDS.

The Lambda function has the following permissions:

  • "ec2:DescribeNetworkInterfaces",
  • "ec2:CreateNetworkInterface",
  • "ec2:DeleteNetworkInterface",
  • "ec2:DescribeInstances",
  • "ec2:AttachNetworkInterface"
  • "logs:CreateLogStream",
  • "logs:PutLogEvents"

The Python 3.9 script related to testing the connection is as follows and functions properly on a local machine.

def ConnectTest():
http = urllib3.PoolManager(timeout=Timeout(connect=1.3,read=1.3));
logger.info("Lodging")
url = "---" # A link to one of my servers that simply responses "Hello" to POST HTTP requests. 

headers = {"Accept": "application/json"}

response = ""

try:
    response = http.request('POST',url, headers=headers,timeout=Timeout(connect=1.3,read=1.3))
except Exception as e:
    logger.error("Request error | %s", response);

return response.data;

I have also tried increasing the timeout time but it really shouldn't take more than a couple seconds to send a request.

Thanks for your help!

CodePudding user response:

Despite the AWS Guide stating to use a public and private subnet, this does not work and ended up being the cause of the intermittent connection.

I have the Lambda function and the RDS database on 3 private subnets and that seems to work okay, I haven't had any issues so far.

  • Related