looking for a way to upgrade / downgrade inside Lambda - python

I need a way to upgrade/downgrade the boto3 lib inside my Python 3.7 env inside Lambda.
Right now, the version is 1.9.42 inside Lambda. I cannot use certain things like Textract (boto3.client('textract'), but I can on my local machine (boto3 version 1.9.138.
So, I decided to install boto3 into a package (pip3 install boto3 -t dir/ --system) then upload it to Lambda after zipping it.
This didn't work because Lambda won't accept a package larger than 3MB (it's around 8MB)
Any other workarounds?
edit: I know I could always just write code that works and keep uploading it to Lambda, but this will become cumbersome as I'd have to include all the packages installed in the package and rebuilding it as I make changes.

The Serverless Application Model is a tool provided by AWS that lets you develop locally as it simulates the lamdba environment inside a docker container. Once you are ready you can deploy your code to a lambda and it will work as expect.
If you really want to keep editing the code in the web platform, there is a workaround by using lambda layers. You create a package with all of your dependencies and upload that to a lambda layer. Then include your layer in the lambda and just modify your own code there. As it has been pointed out in the comments this is not the way to go for real development.

Related

Do I need to include Python PDF Toolkit or pypdftk as a Lambda Layer?

Lambda Layer Docs for Reference: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
The project was written in Python 2.7, which is appears AWS Lambda has deprecated. So I've attempted to update the code to the latest release Py 3.9 and most of my errors are resolved.
Due to very poor documentation & lack of comments, I've been stuck trying to figure out which parts of this codebase go to which AWS services.
For example: this project had 3 different init.py files & one of them is even empty.
The code apparently was working before I received it.
I've no idea which pieces of this project go to which services. Some of them are obvious. Certainly the Lambda.py is supposed to go to a lambda function.
In short the project does this:
Get JSON from Webhook, perform calculation & alter JSON, convert to PDF, email PDF to customer, store PDF in S3 bucket.
The project uses a library called pypdftk or Python PDK Toolkit. Hopefully this is compatible with Py 3.9, because apparently Py 2.7 is deprecated on AWS Lambda.
It appears this project was run almost entirely as a Lambda Function, using Lambda.py as an entry point, and importing the rest the .py code files into that entry point.
Will I need to include this Python PDF Toolkit as a Lambda Layer? To my knowledge, all libraries not included in the Standard Python 3.9 library must be added as a Lambda Layer.
Additionally, is this python pdf toolkit only compatible with Python 2.7? 2.7 is no longer supported by AWS Lambda, so I have updated the code to be compatible with Py 3.9
EDIT:
These are the project files & directories.
Do I upload the entire project zipped into Lambda?
Does a portion of this need to be uploaded to a Lambda Layer?
Will I need to set-up & configure an EC2? S3? SES?
I am assuming that you need https://github.com/revolunet/pypdftk, which should be compatible with Python 3.9. This is a wrapper for the pdftk binary, which must be installed separately in your environment. Lambda functions execute in a container with Amazon Linux. The recommended way to use a native binary with Lambda is to create a layer. There are some projects in Github that generate the layer:
https://github.com/lob/lambda-pdftk-example
https://github.com/inetsys/pdftk-aws-lambda
Both of them are old, so use with caution.

Python dependencies in AWS lambda

I am trying to create a lambda using python directly in the console and am stuck on installing a dependency. Is there a way to install dependencies in the console? I have tried a subprocess call but says I don't have permission to the folders.
I am used to doing lambdas in javascript and have deploy scripts for that. I just don't know how to translate that to python.

Python project in visual studio deployed on AWS using lambda?

I have a python project with a lot of dependencies (around 30 or so python packages) that i want to deploy on aws using lambda function. Right now i have about 30 custom python packages in my VS solution that i import into the main function - there is a lot of code. What is the best way to build a deployment package and how would i go about doing this?
I watched a few tutorials but i am new to this, so im not sure exactly what concrete steps to take. If i use something like zappa and create a virtual environment how would i then get my project there and install all the dependencies and then zip the file?
Thanks so much, sorry for the stupid questions, i couldn't find a stackoverflow post that covered this
Just go to your python environment folder and found site-package folder (usually in /lib), choose all the dependencies you need and zip them with your code.
I guess it's the easiest way.
For example, I may need beautifulsoup and urllib for dependencies, just zip them (and their dependencies, if needed) with my code, then upload to AWS Lambda, that's all.
BTW, you can also see this gist to know whether the module you need can be directly import to AWS Lambda or not.

Python package management for AWS lambda function

I'm struggling to understand how best to manage python packages to get zipped up for an AWS lambda function.
In my project folder I have a number of .py files. As part of my build process I zip these up and use the AWS APIs to create and publish my lambda function supplying the zip file as part of that call.
Therefore, it is my belief that I need to have all the packages my lambda is dependant on within my project folder.
With that in mind, I call pip as follows:
pip install -t . tzlocal
This seems to fill my project folder with lots of stuff and I'm unsure if all of it needs to get zipped up into my lambda function deployment e.g.
.\pytz
.\pytz-2018.4.dist-info
.\tzlocal
...
...
First question - does all of this stuff need to be zipped up into my lambda?
If not, how do I get a package that gives me just the bits I need to go into my zip file?
Coming from a .Net / Node background - with the former I NuGet my package in and it goes into a nice packages folder containing just the .dll file I need which I then reference into my project.
If I do need all of these files is there a way to "put" them somewhere more tidy - like in a packages folder?
Finally, is there a way to just download the binary that I actually need? I've read the problem here is that the Lambda function will need a different binary to the one I use on my desktop development environment (Windows) so not sure how to solve that problem either.
Binary libraries used for example by numpy should be compiled on AWS Linux to work on lambda. I find this tutorial useful (https://serverlesscode.com/post/deploy-scikitlearn-on-lamba/). There is even newer version of it which uses docker container so you do not need EC2 instance for compilation and you can do everything locally.
As for the packages: the AWS docs says to install everything to the root, but you may install them all in a ./packages directory and append it to path in the beginning if the lambda handler code
import os
import sys
cwd = os.getcwd()
package_path = os.path.join(cwd, 'packages')
sys.path.append(package_path)

Aws Lambda: Python function with Pandas dependency

How to deploy to python function which has the dependency on external libraries?
For example, I am trying to deploy to a data-analysis python function. When I try to test the python function from the lambda console, I get:
Unable to import module 'lambda_function': No module named pandas
I am totally new to Aws Lambda
Is there a linux box on which Lambda functions run where I can install these libraries?
You need to create a deployment package as detailed here: http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html#deployment-pkg-for-virtualenv
This just means bundling the contents of site-packages for the environment you're developing on into the deployment package together with the lambda python script into a zip that is uploaded.
If you new to Lambda deployment, you might want check this tutorial (I wrote), which covers the most common pitfalls. And gives you a script as well to automate the entire process.

Categories

Resources