Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - creating aws lambda deployment package

I want to script updating code for my AWS Lambda using a Fabric task. Boto3 api expects a byte array of base-64 encoded zip file.

What would be the simplest way to create it assuming I have the source code files as the input?

like image 845
sumek Avatar asked Mar 14 '23 00:03

sumek


2 Answers

With the current boto3, don't unzip it, don't base64 encode it. You can do it with an open and a read like this:

import boto3
c = boto3.client('lambda')
c.create_function({
    'FunctionName': 'your_function',
    'Handler': 'your_handler',
    'Runtime': 'python3.6',
    'Code': {'ZipFile': open('./deploy.zip', 'rb').read()}
})

I use the zip file above for getting started quickly. You can also upload the deploy.zip to a S3 bucket and pass the bucket + key as strings in the 'Code' dict as 'S3Bucket' and 'S3Key'.

like image 77
Brian C. Avatar answered Mar 23 '23 06:03

Brian C.


Actually boto3 documentation is out of date, you should pass the bytes directly:

https://github.com/boto/boto3/issues/201

As to the zip file this should point you in the right direction:

  • http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
  • http://www.devshed.com/c/a/Python/Python-UnZipped/
  • https://docs.python.org/3/library/functions.html#bytearray
like image 40
rsFF Avatar answered Mar 23 '23 05:03

rsFF