0
votes

I'm trying to create a deployment package for AWS Lambda but no matter what I do I can't get it to run. Here's my process:

1) Launch AWS EC2 instance from the AMI linked here

2) Use Pip3 to install all my dependencies to a local folder with Python 3.6 installed via Yum.

3) FTP the dependencies to my local computer and zip it with my python file.

My folder structure normally: Folder Structure Pre-Zip

My folder structure INSIDE zip: Folder Structure Post-zip

The exact code I'm trying to call is the "handler" method inside of login.py. This is how my Lambda function is set-up: Lambda Set-up

Lastly, here is the actual error message I receive, I've done everything I think of can't seem to figure this out: Lambda Error

Any help greatly appreciated!!

EDIT: Here's the python code:

import sys
import logging
import rds_config
import bcrypt
import pymysql

#rds settings
rds_host  = "xxx"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name

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

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    sys.exit()

logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
def handler(event, context):
    accountData = None
    accountEmail = event['userEmail']
    accountPassword = event['userPassword']

    with conn.cursor() as cur:
        sql = "SELECT `id`, `email`, `password` FROM `accounts` WHERE `email`=%s"
        cur.execute(sql, ('[email protected]'))
        accountData = cur.fetchone()
        if accountData['email'] == accountEmail:
            if bcrypt.hashpw(password, accountData['password']) == accountData['password']:
                print('Returning Account Id: ' + str(accountData['id']))
                return accountData['id']
            else:
                print("Invalid Credentials")
                return "Invalid Credentials"
1
Add relevant code you have in login.py specially all the imports and handler function. - Asdfg
Nothing looks odd. It looks like your zip package is corrupt or for some other reason AWS is not able to unzip it. Try pasting the code directly in the function code window or create a new zip package. - Asdfg
Also try to create the zip package using 7zip or WinZip instead of windows internal compress file thing. - Asdfg
Check file extension. Make sure it is not login.py.py. Also add your requirements.txt file and I may try it at my end. - Asdfg
Kind of did what you have and no errors. Was able to execute the code with no problem. I am pretty sure something is not right with your filename or extension. - Asdfg

1 Answers

0
votes

I needed to add a directory called .libs_cffi_backend from my EC2 instance to the .zip and then upload it to Lambda to allow all my pip libraries to work correctly.