问题描述
从S3将python zip软件包上传到AWS Lambda时,出现以下神秘错误:
When uploading a python zip package to AWS Lambda from S3 I get the following cryptic error:
module initialization error: [Errno 13] Permission denied: '/var/task/lambda_function.py'
错误似乎是,如果您创建具有限制性权限的zip软件包,则AWS会感到困惑.本质上,AWS使用您赋予它的权限将您的程序包解压缩并尝试使用它.尤其令人困惑的是,您可能可以从AWS Lambda内联代码编辑器中看到部分zip文件(因此您显然拥有一些权限),但是Lambda函数将给出上述错误.
The error seems to be that if you create a zip package with restrictive permissions, then AWS gets confused. Essentially, AWS unzips your package with the permissions you gave it and tries to use it. What can make this especially confusing is that you may be able to see part of the zip files from the AWS Lambda inline code editor (so you clearly have some permission), but the Lambda function will give the above error.
处理此问题的最佳方法是什么(更好的错误消息或解决问题的方法)?
What is the best way to handle this (either a better error message or resolve the problem)?
推荐答案
我使用的方法是在使用python创建zip软件包时要小心.
The approach I used was to be careful in how I created my zip package in python.
代替
ziph = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
ziph.writestr(file_name, my_data)
我将上面的最后一行替换为
I replaced the last line above with
zinfo = zipfile.ZipInfo(file_name)
zinfo.external_attr = 0o777 << 16 # give full access to included file
ziph.writestr(zinfo, my_data)
要确保明确授予完全权限.如果不执行此操作,则writestr
将使用限制性太强的默认权限. (注意:以上是针对python 3.6的.)
To make sure to explicitly grant full permissions. If you don't do this, then writestr
will use too restrictive default permissions. (Note: the above is for python 3.6).
这篇关于从S3上传AWS Lambda python zip后权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!