Error in sending Python API request from AWS Lambda python function

Question:

I am trying send a POST request from AWS python lambda function but getting following error while sending:

[ERROR] ConnectionError: HTTPConnectionPool(host=
********************s3-website.ap-south-1.amazonaws.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f69fdfbda90>: Failed to establish a new connection: [Errno 110] Connection timed out’))
Traceback (most recent call last):

![Response]

I tried with the following code:

import json
import uuid
import datetime
import re
import base64
import requests
import httpx
import asyncio
def lambda_handler(event,context):

    INVENTORY_CP_OPERATION_URL="************.s3-website.ap-south- 
                 1.amazonaws.com/"
    message={
       "email":"*******@********.com",
       "password":"123456"
        }
    result=requests.post(**************,json=message)
    return "success".
Asked By: Rambo

||

Answers:

Given your Connection timed out, it would seem that your AWS Lambda function is having difficulty connecting to the Internet.

When an AWS Lambda function is NOT connected to a VPC, it has direct access to the Internet.

However, when a Lambda function IS connected to a VPC, it only has Internet access if the Lambda function is connected to a private subnet and there is a NAT Gateway in a public subnet.

Therefore, if the function does not require access to any resources in a VPC (eg Amazon RDS databases), then it is best to disconnect it from the VPC.

Answered By: John Rotenstein