16

I've looked here and here as I've been trying to work out how to get pymysql running on AWS lambda. The examples I've looked at so far are extremely complex, and with the GitHub tutorial I got as far as IAM before I started running into permissions errors I didn't know how to solve.

Literally, all I want to be able to do is call import pymysql within the prebuilt AWS lambda console template.

It seems like a simple problem, but I'm having a hard time finding a clear, step-by-step work through of how to get new dependencies to work for my lambda function. Ideally the example would not by via AWS CLI, since apparently there is a console option and this seems like it would take some of the headache out of the process.

Cheers,

Aaron

aaron
  • 6,339
  • 12
  • 54
  • 80
  • Note what your second link says: *"Simple scenario – If your custom code requires only the AWS SDK library, then you can use the inline editor in the AWS Lambda console."* Otherwise, you can't use the console editor. It's only for simple prototyping and "hello, world" types of code. You need a deployment package. – Michael - sqlbot Nov 11 '16 at 01:46
  • that's the answer then it sounds -- no way to sidestep the deployment package. alrighty then, thanks! any tips on how to use the console to at least upload the package via the GUI rather than with AWS CLI? – aaron Nov 11 '16 at 03:17
  • I admit the whole process seems overwhelmingly nebulous at first glance, but it's pretty straightforward once you get the idea. My experience is with node rather than python but it looks like @PankajChandankar has your solution, here. You do a "local install" of the package from the workstation directory that has your lambda code, then zip up the entire contents of that directory (with your code in the "root" of the zip file). – Michael - sqlbot Nov 11 '16 at 11:07
  • Note also that if you need libraries or utilities that need to be compiled into executables, there's an EC2 AMI that you can use to create your own machine so that you can build things in a binary-compatible environment: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html – Michael - sqlbot Nov 11 '16 at 11:12

3 Answers3

19

I was facing similar problem with Redis python library. I follow the same documentation instructions which you mentioned in your second link.

here is example snippet for your reference :

Create new directory MyPythonLambda and put MyPythonLambda.py in the same.

Assume MyPythonLambda/MyPythonLambda.py is main lambda containing handler.

 cd MyPythonLambda/
 pip install redis -t .
 zip -r MyPythonLambda.zip *

Upload/import zip in lambda creation from S3 or your local file system.

I think you need to create zip file in similar way containing your python mysql library.

PankajChandankar
  • 327
  • 3
  • 11
3

TheYoungSoul has a fantastic YouTube example of how to do this step-by-step. Once I followed these instructions this was pretty easy to do.

Steps:

  1. Write a locally testable version of the routine I want to implement on lambda and call this function main.py. main.py has the function lambda_handler inside of it, which has the basic structure def lambda_handler(event, context): ...

  2. Use the script create_deployment.py, available on his repo, in conjunction with requirements.txt to create your deployment zip file. Note that if you're on a Mac and this errors out on the first try may need to do this.

  3. Once you have a locally testable example running, create your lambda function on AWS and instead of writing function from scratch select the console menu option to upload a .zip file.

  4. Make sure to create a custom role that has access to RDS resources and be sure to place the DB that you want to connect with in the same VPC group. When setting up your function, specify that you want your lambda function to have VPC access.

Community
  • 1
  • 1
aaron
  • 6,339
  • 12
  • 54
  • 80
0

These are the steps that you need to follow:

  1. install the package like this
pip install --target ./python pymysql

this will install the package in the folder called python. After doing that zip this file.

  1. Then create a layer in AWS lambda and upload this zip file over there.

  2. After creating the layer go to layers and add a layer. After that just import the package in your lambda code and you should be able to use it

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 17 '23 at 16:26