AWS Lambda now supports Python 2.7 from October 8, 2015, following Node.js and Java. This makes it possible to use Python code previously written using boto with Lambda. I tried it immediately, so I will summarize the points.
Name A name managed by Lambda. It's not the name of a Python module that runs on Lambda.
Code In Python, one file is one module. The file name is * module name.py *. If you have one module, edit it online, and if you have multiple modules, upload them together in a zip. When uploading, specify the file uploaded from the browser in Management Console or uploaded to S3.
If there are multiple modules, zip them together. There are two important points.
--Compress so that the module is at the top of the zip.
--Good example: Files are extracted to the current directory when unzipped without options
--Bad example: A directory is created and files are extracted under it when unzipped without options
--Include third party modules to import.
--In addition to the standard modules, if there is a module that you are importing from your code by pip install
, include its dependencies in the zip.
--boto 3 does not need to be included in the zip. If you want to use a specific version of boto, include it in the zip.
Cd
to the top-level directory containing your Python code andzip -r filename.zip *
. Here is an example when the code is under a directory called * myModuleDir *.
There are no rules for zip filenames.
bash
$ cd myModuleDir
$ ls
myCode1.py
myCode2.py
...
$ zip -r ~/myLambdaFunction.zip *
Install with pip install <modulename> -t <myModuleDir>
in the top level directory with the Python code.
If you specify the installation destination with the -t
option in pip install
, an error may occur. According to my research, it seems that it occurs when it is installed on lib64 and it is also raised in the issue, but as of October 2015, if you yum install python27-pip
on Amazon Linux, there is this problem. Lxml is the one I've tried. In this case, it worked by simply copying the installed module without the -t
option.
I got an error when I copied the module of Python2.6, so pip install with Python2.7.
$ cp -R /usr/local/lib64/python2.7/site-packages/lxml <myModuleDir>
Blueprint It is a template that you can easily start for each AWS service or programming language used from Lambda. For example, you can easily get started with Lambda in Python by selecting * hello-world-python *.
Handler If you select Blueprint, the handler written in the code is selected. If you want to reuse an existing Python module, replace ʻif name =='main':` with'def lambda_handler (event, context):' and specify this handler in Lambda. Or if you don't want to edit an existing module, import the existing module and create a wrapper module that describes the handler.
Role Assign a Role to the instance of Lambda you want to run. Start with a template that you can create and select in the AWS Management Console, and edit the permissions as needed. If the Role is set correctly, you do not need to set the Security Credential (access key).
Memory You can select from 128MB to 1536MB in stages. When executed, the maximum memory consumption will be displayed on the console.
Timeout It can be set in seconds. With this update, you can set up to 5 minutes. If the Timeout time is exceeded, the program will be forcibly terminated.
Log It's also visible on the console, but it's automatically logged in CloudWatch.
Appendix A. References -[[AWS Announcement] AWS Lambda Update-Python, VPC, Extension of Execution Time, Schedule, etc.](http://aws.typepad.com/aws_japan/2015/10/aws-lambda-update-python-vpc- increased-function-duration-scheduling-and-more.html)
Recommended Posts