AWS Lambda now supports Python so I tried it

Introduction

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.

Creating a zip

If there are multiple modules, zip them together. There are two important points.

Point of zip creation

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

Zip creation command

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 *

To include a third party module in a 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

AWS Lambda now supports Python so I tried it
Code review with machine learning Amazon Code Guru now supports Python so I tried it
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
Wrangle x Python book I tried it [2]
Wrangle x Python book I tried it [1]
I tried connecting AWS Lambda with other services
I tried Python> autopep8
I tried AWS CDK!
I tried Python> decorator
I tried AWS Iot
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
I tried running TensorFlow in AWS Lambda environment: Preparation
I tried to get an AMI using AWS Lambda
I want to AWS Lambda with Python on Mac!
I was able to repeat it in Python: lambda
AWS CLI v2 has become GA (General Availability), so I tried using it immediately.
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried fp-growth with python
I tried scraping with Python
[Python] Scraping in AWS Lambda
I tried Python C extension
I tried gRPC with Python
I tried scraping with python
Note that it supports Python 3
I tried using AWS Chalice
Summary if using AWS Lambda (Python)
I tried running the Python Package Repository (Warehouse) that supports PyPI
I tried to touch Python (installation)
I tried web scraping with python.
Write AWS Lambda function in Python
I got stuck so I reviewed it
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I compared Node.js and Python in creating thumbnails using AWS Lambda
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
Notify HipChat with AWS Lambda (Python)
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
I tried running prolog with python 3.8.2.
I made a segment tree with python, so I will introduce it
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I tried Line notification in Python
I tried SMTP communication with Python
[Python] I tried using YOLO v3
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I tried to notify Zabbix Server of execution error of AWS Lambda function
I want to do it with Python lambda Django, but I will stop
Python coding: I'm not sure about Nantoka so I looked it up
When I tried to run Python, it was skipped to the Microsoft Store
I tried to make a calculator with Tkinter so I will write it
[AWS] [GCP] I tried to make cloud services easy to use with Python
[Zaif] I tried to make it easy to trade virtual currencies with Python
Github Interesting Repository ① ~ I found a graphic repository that looks interesting, so I tried it ~
[Lambda] I tried to incorporate an external module of python via S3
I tried to summarize Python exception handling
I tried to implement PLSA in Python
[AWS] Using ini files with Lambda [Python]
I tried to implement permutation in Python
I installed DSX Desktop and tried it
I tried scraping Yahoo News with Python
I tried to implement PLSA in Python 2