AWS Lambda timeout with external dependencies

0

Hi,

I am having issues with a Lambda function which has external dependencies. The function will be triggered by a API Gateway call. When the function is deployed without external dependencies it works fine. However, when i use external dependencies (eg: in the function, for testing purposes i check if the port of a RDS instance is listening) a timeout occurs.

When i remove the reference to the library which i imported, but leave the import the function executes without a timeout. I have tried different things, but to no avail. I also created a layer in Lambda, which has the dependencies, but also when i reference a library which is contained in that layer, the timeout occurs again.

Do you have any suggestions / help? Below my (very simple) source code.

` import json import socket import boto3 import pg8000

import sys

sys.path.insert(0, '/vendor') from botocore.exceptions import ClientError import pg8000.native

def hello(event, context): secret_name = "dev/studieplanning_dev" region_name = "eu-north-1"

# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
    service_name='secretsmanager',
    region_name=region_name
)

try:
    get_secret_value_response = client.get_secret_value(
        SecretId=secret_name
    )
except ClientError as e:
    # For a list of exceptions thrown, see
    # https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    raise e

# Decrypts secret using the associated KMS key.
secret = get_secret_value_response['SecretString']
object = json.loads(secret)
print(object['username'])
print(object['host'])
print(object['port'])

dsn = "dbname=" + object['dbInstanceIdentifier'] + " user=" + object['username'] + " password=" + object['password'] + " host=" + object['host']
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex((object['host'], object['port']))
    if result == 0:
        print
        "Port is open"
    else:
        print
        "Port is not open"
    sock.close()

body = {
    "message": "Go Serverless v1.0! Your function executed successfully!",
    "input": event
}

response = {
    "statusCode": 200,
    "body": json.dumps(body)
}

return response

`

2 Answers
0

Is your Lambda attached to a VPC that can communicate with the RDS instance? Unless your RDS instance is exposed to the internet, which is not recommended, you will need to communicate through a VPC or use a different method to interact with the database.

profile picture
answered a year ago
0

The issue with the Lambda function timing out is that you have not set a timeout on your socket, so when you are trying to do connect_ex that will block and wait in definitively in case it cannot connect to the instance (see answer from Jason for the reason why that could happen).

Add sock.settimeout(timeout) before calling connect_ex with timeout set to an appropriate value which should anyway be less than the Lambda function timeout.

AWS
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions