Run Python on Schedule on AWS Lambda

Introduction

[Some features added] regarding Lambda at Amazon re: Invent 2015 on October 2015 (https://aws.amazon.com/jp/blogs/aws/aws-lambda-update-python-vpc-increased-function -duration-scheduling-and-more /) has been announced. Personally, the following three are big.

Lambda AWS Lambda is started with Schedule Driven using Data Pipeline I wrote an article before, but the function of Schedule execution is supported as a standard function of Lambda. it was done. In this article, I will write the Lambda function using Python and explain the procedure to execute the Schedule.

Limitations

The Lambda + Python + Schedule execution environment announced / added this time, but there are some restrictions to be aware of. The major items are as follows.

Case1: Try to output with Python Print every 5 minutes

As the very first case study, it is the most solid case of executing a Python Script that simply prints a Schedule every 5 minutes. Check the Log to see if the result of Schedule execution is output correctly.

From Lambda in the AWS Console, select Create a Lambda function.

Step1 Select a template (blueprint = blueprint)

There are many templates for Lambda function, but search and select lambda-canary. It is a template to execute Python script Schedule on Lambda function.

select_blueprint.png

Step2 Setting Event source

Since this time the Schedule is executed, select Scheduled Event as the Event source (I think it is selected by default). Name and Description are OK if you enter an appropriate description. The Schedule expression should berate (5 minutes)by default, so you can leave it as it is.

Configure_event_sources.png

Step3 Python script settings

Set the Python script and IAM role.

The Python code uses: It is a code that just prints.

import json, datetime, commands

def lambda_handler(event, context):
    print commands.getoutput('cat /proc/cpuinfo | grep -e "processor" -e "model name"')
    print commands.getoutput('cat /proc/meminfo | grep MemTotal')
    print commands.getoutput('cat /proc/meminfo | grep MemFree')
    print datetime.datetime.now().strftime('%Y/%m/%d %H:%M')  
    print '-------------------------------'
    
    print event
    print event['account']
    print context.__dict__
    print context.memory_limit_in_mb

Other than Python script, the following settings are required. Others are good with Default mom.

Configure_function.png

Step4 Check settings

This is the final confirmation. Select ʻEnable now to enable Schedule execution with this setting. Select ʻEnable now and press Create function to complete.

Review.png

Step5 Print Check the output result

The output of the Lambda function is saved in CloudWatch logs. From CloudWatch Logs in the AWS Console, select Lambda_Test. Then, the execution log is added every 5 minutes, and I think that the printed contents remain in the log.

CloudWatchLogs.png

Python script supplement

Case2: Try changing the Schedule execution cycle from every 5 minutes to every 10 minutes

Step1 Delete old Schedule execution settings

From Lambda in the AWS Console, go to the ʻEvent sources tab of the function of Lambda_Testyou created earlier. Delete the Schedule execution set inrate (5 minutes)withx and select ʻAdd event source. EditEventSource.png

Step2 Setting Schedule execution every 10 minutes

In the Dialog of ʻAdd event source, select Scheduled Event for ʻEvent sources type and fill the Dialog as follows.

The point to note here is that the Lambda cron grammar is slightly different from the so-called general Linux cron grammar. Refer to this article Please write cron (0/10 * * *? *).

EditEventSource2.png

If you select ʻEnable now`, Schedule will be executed with this setting. Check if the execution log is in CloudWatch logs every 10 minutes.

Case3: Try inserting a non-standard Library

If you want to use a non-Python 2.7 standard library such as numpy, pandas or requests with Lambda + Python, you need to zip and upload the Library together with the Script file itself. there is. For details, see Creating a Deployment Package (Python) on the AWS official website. ).

Step1 Save Python script as file

Save the following Python script to Local (we'll assume you save it with the filename LambdaTest.py). The content of Python Script is to HTTP GET google.co.jp using requests library and print Status Code and Response Body.

LambdaTest.py


import requests, datetime

def lambda_handler(event, context):
  target_URL = 'https://www.google.co.jp'

  r = requests.get(target_URL)
  print datetime.datetime.now().strftime('%Y/%m/%d %H:%M') 
  print '-------------------------------'
  print r.status_code
  print '-------------------------------'
  print r.text

A library called requests, which is not in the Python 2.7 standard, is required, so if you register this Code in Lambda as it is, an Error will occur. Therefore, put the library file of requests in the same folder, zip it, and register the zip with Lambda.

Step2 Create a zip file containing the Library

If you execute pip install with -t /PathTo/LambdaTest.py option, the library file will be placed in the specified folder. For example, if LambdaTest.py is in / home / hoge, you can run pip install -t / home / hoge.

The file structure should look like the one below. Zip these (2 folders, 1 file) and give them a suitable name (let's say LambdaLibraryZip.zip)

folder.png

Step3 Change the function name of Entry Point

When uploading the Code of Lambda function with zip, the function name of Entry Point (main function of Python) becomes the file name of * .py + . + Function of Entry Point in Script. In this example, you need to set it to LambdaTest.lambda_handler. Set Handler on the Configuration tab to LambdaTest.lambda_handler

HanderName.png

Step4 Upload and execute the zip file

From the Code tab, upload the LambdaLibraryZip.zip created earlier with ʻUpload a .ZIP file, click Save and test`, and check if the Python Script is running correctly. Please give me.

uploadzip.png

Finally

Lambda's Python support and Schedule execution support are very good news for me as Python Love. Go to Amazon's 2 tier Architecture or Serverless Architecture I think it will be a big stepping stone (and will be locked in more and more by Amazon ...).

However, I think there are still some areas that cannot be replaced by running Lambda + Python + Schedule.

Cases that need to be executed in a short cycle

Lambda has a minimum run period of 5 minutes. I think this will cover many needs, but there are also requirements such as "I want to check the status every minute and monitor life and death". I don't know the AWS service that runs a server-less short-cycle schedule (please let me know if you know it).

The proposal that Amazon has made is [EC2 t2.nano Instance](http://aws.typepad.com/aws_japan/2015/10/ec2-instance-update-x1-sap-hana-t2-nano-websites. html), so I think. Prepare an Instance that is even cheaper than the current t2.micro, and use this to write on the back of the leaflet.

Cases that take a long time to process

Lambda's maximum processing time has now been increased to 5 minutes (previously 1 minute). When it was 1 minute, use the Lambda function to bring the Log of S3 to Local, parse the contents, and when an error is found, activate Alarm and write the result to RDS. At that time, there was a case that Timeout occurred when the Log size exceeded several hundred MB, but I think that this expansion to 5 minutes will cover many needs. However, there are also requirements that require processing time, such as "Daily ETL Batch processing of a large amount of data".

As far as I know, there are requirements that take time to process with this kind of calculation and data transfer.

I think that is good.

Recommended Posts

Run Python on Schedule on AWS Lambda
[Python] Run Headless Chrome on AWS Lambda
Run mysqlclient on Lambda
Modules cannot be imported in Python on EC2 run from AWS Lambda
[Python] Scraping in AWS Lambda
Run Tensorflow 2.x on Python 3.7
Run Python CGI on CORESERVER
Run unix command on python
Run YOLO v3 on AWS v2
Run YOLO v3 on AWS
Check types_map when using mimetypes on AWS Lambda (Python)
Deploy Python3 function with Serverless Framework on AWS Lambda
Support for Python 2.7 runtime on AWS Lambda (as of 2020.1)
I want to AWS Lambda with Python on Mac!
Posted as an attachment to Slack on AWS Lambda (Python)
Run AWS IoT Device SDK for Python on Raspberry Pi
Summary if using AWS Lambda (Python)
Write AWS Lambda function in Python
Run TensorFlow Docker Image on Python3
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
Post images of Papillon regularly on Python + AWS Lambda + Slack
Notify HipChat with AWS Lambda (Python)
[Python] Allow pip3 packages to be imported on AWS Lambda
Periodically run Python on Heroku Scheduler
xgboost (python) on EC2 Spot instance environment prepared by AWS Lambda
Things to note when running Python on EC2 from AWS Lambda
Best practice for logging in JSON format on AWS Lambda / Python
[AWS] Using ini files with Lambda [Python]
Run TensorFlow on a GPU instance on AWS
[Python] Run Flask on Google App Engine
Install python library on Lambda using [/ tmp]
Run AzureKinect in Python on Christmas Eve.
Run servomotor on Raspberry Pi 3 using python
Run Python code on A2019 Community Edition
Connect to s3 with AWS Lambda Python
Run Python in C ++ on Visual Studio 2017
Python + Selenium + Headless Chromium with aws lambda
Run python wsgi server on NGINX Unit
Python on Windows
twitter on python3
python on mac
Jupyter on AWS
Python on Windbg
Put MicroPython on Windows to run ESP32 on Python
[2020 version] How to install Python3 on AWS EC2
Run Python YOLOv3 in C ++ on Visual Studio 2017
Python development on Ubuntu on AWS EC2 (using JupyterLab)
How to run MeCab on Ubuntu 18.04 LTS Python
Periodically execute Python Script on AWS Data Pipeline
LINE BOT with Python + AWS Lambda + API Gateway
Run Python web apps on NGINX + NGINX Unit + Flask
Run Zookeeper x python (kazoo) on Mac OS X
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Jinja2 2.9.6 does not work on Lambda Python 3 series
Run pip install on MacOS Python 3.7 or later
[AWS / Lambda] How to load Python external library
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
(For myself) AWS_Flask_3 (Install / Run Flask on AWS)
Summary of studying Python to use AWS Lambda
Amazon API Gateway and AWS Lambda Python version
TensorFlow: Run data learned in Python on Android