Tuesday, February 1, 2022

[SOLVED] AWS Lambda running a zip package created in windows

Issue

I'm freshly in AWS so this is why probably this is a basic question. Anyways the thing is that I'm to upload a zip file with my project created on my computer with virtualenv. So basically my environment is:

windows 10 Python 3.6 PyCharm editor create a virtual environment for aws: virtualenv

My script requires import a new python library requests, my script below using a kind of lambda_handler (probably my code has error in that function because remember i'm new in aws and not the best in python.)

import requests
import datetime
import json



def handler_name(event, context):
    file = 'https://secure.bixi.com/data/stations.json'
    result = requests.get(file)
    data = result.json()
    now = datetime.datetime.now()
    hour = now.hour
    day = now.day
    month = now.month
    mins = now.minute


    with open('Bixi_%s_%s_%s-%s.json' % (month,day,hour,mins) , 'w') as outfile:
        json.dump(data, outfile)
    return print("download succeed")

#handler_name('n','n')

Once I do that I have create in my virtualenv an env to install the required python package, finally I tested my script and was executed successfully. the virtualenv has the same name as my python script you can see it and the beginning of the command line within ():

enter image description here

Now I just go to my project where I created my env get the folder and zipped to be upload in AWS Lambda. Unfortunately, after executing the zip file I got this message.

START RequestId: 42e3e3ce-9aec-11e7-b97c-8116ad342de4 Version: $LATEST Unable to import module 'lambda_function': No module named 'lambda_function'

END RequestId: 42e3e3ce-9aec-11e7-b97c-8116ad342de4 REPORT RequestId: 42e3e3ce-9aec-11e7-b97c-8116ad342de4 Duration: 0.38 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB

What I have tried: I said ok I will change my script by the default suggested script provided by Lambda as hello-world.

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])
    print("value2 = " + event['key2'])
    print("value3 = " + event['key3'])
    return event['key1']  # Echo back the first key value
    #raise Exception('Something went wrong')

Unfortunately, guys when I run my function again that returns the same message. Probably my error is when I'm creating the virtualenv but well I really don't know how to figure it out. I appreciate if someone can give me a hand with that. Thanks.

Update Based on a couple of answers I have verified my project and I realized two things:

  1. the name used in my function was not updated with the right file name
  2. I was not zipping really well my project

For the second point I just set the right name in aws lambda as: bixi_import.lambda_handler

For the first point I created again a virtual env and then I saw in my file system that created a new folder file for this environment then I downloaded the required package I went to the path site-packages within my virtual environment I just copied the python file and then carefully i did a crt+a for all files and created my zip to ensure that Lambda will be able to read directly all files without go thru a folder.

thanks to @Michael in point 2 and @Kannaiyan in point 1.


Solution

Based on the documentation,

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])
    print("value2 = " + event['key2'])
    print("value3 = " + event['key3'])
    return event['key1']  # Echo back the first key value
    #raise Exception('Something went wrong')

If you store this in a file called lambda_function.py.

The handler configuration for this function is, lambda_function.lambda_handler

Format is filename(without extension).functionname

http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html



Answered By - Kannaiyan
Answer Checked By - Dawn Plyler (WPSolving Volunteer)