4

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?

2 Answers 2

7

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'.

Sign up to request clarification or add additional context in comments.

2 Comments

FYI: I'm still using the above code with boto3 1.9.
I used the same code, However I am getting error that No such directory
1

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:

6 Comments

Yes, I've looked at them. They don't answer my question though. As I've mentioned I'd like to use boto create_function method that accepts a byte array of base-64 encoded zip file.
actually IIUC you can pass the bytes directly, did you tried that?github.com/boto/boto3/issues/201
Thats useful! So the remaining part is how to zip file and then get the bytes out of it in elegant way? Ideally I was thinking about doing it all in memory without intermediary files.
I edited the answer so it could provide the pointers necessary for you and for future reference.
There is code in kappa that does this. Perhaps it would be useful. Specifically, checkout: github.com/garnaat/kappa/blob/develop/kappa/function.py#L121
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.