简体   繁体   中英

How to Write an AWS Python3 Lambda Function using a zip file on Windows OS

I have looked all over for a tutorial or help on creating a python3 lambda function from a zip file using the Lambda Management Console on Windows OS. I have, unfortunately, been unlucky. Here is where I am at...

Following the instructions on the AWS website here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

  • I create a folder on my desktop called 'APP'. In that folder I save a file with my python code called 'twilio_test.py' at the root level of 'App'.

My Python code:

import twilio

def lambda_handler(event, context):
    account_sid = '##########################'
    auth_token = '###########################'

    client = Client(account_sid, auth_token)

    message = client.messages.create(
            to = '###########',
            from_ = '###########',
            body = "Test")
    return("success")
  • Since I am using the twilio library I pip install it in the root of my 'APP' folder based on the instructions found in the above link. The instructions say specifically, "Install any libraries using pip. Again, you install these libraries at the root level of the directory.":

pip install twilio -t \\path\\to\\directory

  • I then zip the contents of 'APP' based on the quoted instruction, "Zip the content of the project-dir directory, which is your deployment package. Zip the directory content, not the directory." This creates a zip file called 'twilio_test'.

  • I then go to the AWS lambda management console, upload the zip file 'twilio_test'.

Here is where I am getting confused. What should be the handler?

Have I correctly done everything so far up until this point? If not, what is the best way to go about installing twilio, zipping a file and then using it in AWS lambda?

Although it is inappropriate to say that AWS lambdas are inherently difficult to use, I can say that I am inherently confused.

Originally I thought doing the pip install with anaconda was the issue, so I used powershell. That worked, but only because I specified the full file path, as @sid8491 pointed out here .

I originally used:

<C:\ProgramData\Anaconda3> C:\Users\userName> pip install twilio -t \Desktop\APP

which did not work

But using:

<C:\ProgramData\Anaconda3> C:\Users\userName> pip install twilio -t  C:\Users\userName\Desktop\APP

Did Work

And that will work in powershell or anaconda

You should set the handler to python_file_name.function_name . So in your case it should be twilio_test.lambda_handler .

From the documentation :

... You specify the function name in the Python code to be used as the handler when you create a Lambda function. For instructions to create a Lambda function using the console, see Create a Simple Lambda Function . In this example, the handler is hello_python.my_handler (file-name.function-name) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM