Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a AWS lambda package using python?

I have created the lambda function by using inline code editor for video convert process using zencoder its worked fine.

Now i have to Resize the images in 3 different sizes and from one bucket to another bucket.

For this scenario i need to import some python modules. But it says error like no module found image .

This was my lambda code.

import boto3
import cStringIO
import urllib
import os
import image
fp=urllib.urlopen('iamgeurl')
img = cStringIO.StringIO(fp.read())
im = Image.open(img)
im2 = im.resize((500, 100), Image.NEAREST)  
out_im2 = cStringIO.StringIO()
im2.save(out_im2, 'PNG')
conn = boto.connect_s3()
b = conn.get_bucket('Bucketname')
k = b.new_key('example.png')  
k.set_contents_from_string(out_im2.getvalue())
like image 683
pradeep andrewson2 Avatar asked Feb 06 '23 12:02

pradeep andrewson2


2 Answers

First of all, export your code from the lambda dashboard. Then do the following:

Unzip the downloaded package into a directory, for example project-dir.

Install any libraries using pip. Again, you install these libraries at the root level of the directory.

pip install module-name -t /path/to/project-dir

Zip the content of the project-dir directory, which is your deployment package.

Zip the directory content, not the directory. The contents of the Zip file are available as the current working directory of the Lambda function. For example: /project-dir/codefile.py/lib/yourlibraries

Upload the zip file back to your lambda function.

For more information: http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

like image 192
Deividi Silva Avatar answered Feb 23 '23 02:02

Deividi Silva


To build the zip with your dependencies and your source file, I'm using a library I created for this specific use case called juniper.

With a very simple manifest, you can easily create the zip artifact that you need to enter either in the console or through the awscli.

In your case if you have a requirements.txt with image==1.5.27, or whatever version you want. With this manifest:

functions:
  converter:
    requirements: ./src/requirements.txt.
    include:
    - ./src/lambda_function.py

After running juni build, you will have a converter.zip file with the image dependency included.

like image 43
Pedro Avatar answered Feb 23 '23 04:02

Pedro