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
`