1
votes

pyodbc is included in zip but still giving error when trying to test the lambda function.

Installed pyodbc using pip3 and included the lib file while creating the lambda zip.

import logging
import rds_config
import pyodbc

# import requests

#rds settings
rds_host  = rds_config.server
name = rds_config.username
password = rds_config.password
db_name = rds_config.database

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+rds_host+';DATABASE='+db_name+';UID='+name+';PWD='+ password);
except pyodbc.Error as e:
    logger.error("ERROR: Unexpected error: Could not connect to mssql server instance.")
    logger.error(e)
    sys.exit()

Should be able to run the testing of aws lambda function.

2
Can you please check if the answer below is of any helpamittn

2 Answers

0
votes

To install everything in the same directory, use the below command

pip install pyodbc -t .

OR if you mentioned everything in requirements.txt

pip install -r requirements.txt -t .

After that, zip the entire dir & upload to Lambda console.

0
votes
  • Make sure your code is having a handler lambda_handler and the same is configured on the UI just to cross check.
import json

def lambda_handler(event, context):
    # TODO implement 
    # All your code
    # goes here
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

enter image description here

  • Handler – Handler is the function AWS Lambda calls to start execution of your Lambda function. You identify the handler when you create your Lambda function. When a Lambda function is invoked, AWS Lambda starts executing your code by calling the handler function. AWS Lambda passes any event data to this handler as the first parameter. Your handler should process the incoming event data and may invoke any other functions/methods in your code.

  • The lambda_function file exports a function named lambda_handler that takes an event object and a context object. This is the handler function that Lambda calls when the function is invoked. The Python function runtime gets invocation events from Lambda and passes them to the handler. In the function configuration, the handler value is lambda_function.lambda_handler aws docs.

  • The lambda_handler file (.py) should be at the root of the zip created.

  • and worth using pip install -r requirements.txt -t . or pip install pyodbc -t . to install.

  • The lambda also need a iam role.

  • github example which might be helpful for iam role and lambda. You can also check other repositories(s3-trigger-lambda, lambda-invoking-lambda).