Periodically run a python program on AWS Lambda


I installed a schedule execution program using AWS lambda. I will leave it because it was unexpectedly clogged with the basics of lambda. It is assumed that you have created an AWS account.

MAC OS X python 3.6


・ Those who have never touched lambda ・ Those who want to move something for the time being

What is lambda

It is a serverless service that can execute scripts. Even though it is serverless, it is an image that starts the server only when the script is executed.

Since the call is made from a free server on AWS, the server to execute is different each time. (IP can be fixed by installing lambda in the subnet to which fixed IP is assigned and executing it)

With 1 million free requests per month and 400,000 GB of compute time per second, you can usually run with the free tier. It's a very convenient service because it can also be used as a backend API for the Web and IOT.

Simple scripting

If you want to script your Lambda in python, you must create it with the name ** **. Since the function ** lambda_handler ** in it is called, prepare the lambda_handler function as well.

# -*- coding:utf-8 -*-

#A function called when lambda is executed. The argument is event,context seems to be the default.
#It seems that there is no need for an argument, but I will write it.
def lambda_handler(event, context):
    print('Test run')

Compress this script into a zip format.


$ zip

In the case of MAC, if you compress it on Explorer, the .DS_Store file will be included and cannot be read, so compress it with a command.

Creating a Lambda function

Select "Lambda"-> "Create Function" from the AWS console screen. Enter the function name and the runtime will be Python 3.6 this time.

スクリーンショット 2020-11-15 23.18.48.png

Once you have entered it, click the Create Function button. Now that you have a function, upload the script. Click the "Action" button at the bottom right and click "Upload .zip file" to upload the zip file.

スクリーンショット 2020-11-15 23.22.16.png

スクリーンショット 2020-11-16 0.26.26.png

Run the test

Click Test in the upper right and enter the event name. The array below is the event argument passed to the function, but since it is not used this time, create it as it is without changing it.

スクリーンショット 2020-11-16 0.30.29.png

With the test you created selected, click the test button.

スクリーンショット 2020-11-16 0.33.35.png

It is successful because the execution result appears under the function code.

スクリーンショット 2020-11-16 0.34.34.png

Periodic event creation

Click the "Add Trigger" button in the designer.

Trigger: EventBridge (CloudWatch Events) Rule: New rule Rule type: Schedule expression Scheduled expression: cron (30 1 * *? *)

スクリーンショット 2020-11-16 0.46.30.png

Click the "Add" button and the settings are complete.

Recommended Posts

Periodically run a python program on AWS Lambda
Run Python on Schedule on AWS Lambda
[Python] Run Headless Chrome on AWS Lambda
Periodically run Python on Heroku Scheduler
Run TensorFlow on a GPU instance on AWS
Run Python code on A2019 Community Edition
A python lambda expression ...
Run mysqlclient on Lambda
Periodically execute Python Script on AWS Data Pipeline
Modules cannot be imported in Python on EC2 run from AWS Lambda
Let's write a Python program and run it
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
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)
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!
Procedure for creating a Line Bot on AWS Lambda
[Python] Scraping in AWS Lambda
Run Tensorflow 2.x on Python 3.7
Run unix command on python
Run YOLO v3 on AWS v2
Run YOLO v3 on AWS
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
A little trick to know when writing a Twilio application using Python on AWS Lambda
Posted as an attachment to Slack on AWS Lambda (Python)
Run AWS IoT Device SDK for Python on Raspberry Pi
How to run a Python program from within a shell script
AWS Amplify + API Gateway + Lambda + Python returns a normal response
[GCP] A memorandum when running a Python program on Cloud Functions
Post images of Papillon regularly on Python + AWS Lambda + Slack
[Python] Allow pip3 packages to be imported on AWS Lambda
Building a Python environment on Mac
Summary if using AWS Lambda (Python)
Building a Python environment on Ubuntu
Create a Python environment on Mac (2017/4)
Write AWS Lambda function in Python
Run a Linux server on GCP
Run TensorFlow Docker Image on Python3
Run Matplotlib on a Docker container
[Python] Make the function a lambda function
Create a python environment on centos
Run headless-chrome on a Debian-based image
Run TensorFlow2 on a VPS server
Notify HipChat with AWS Lambda (Python)
Run a simple algorithm in Python
Build a python3 environment on CentOS7
When writing a program in Python
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
xgboost (python) on EC2 Spot instance environment prepared by AWS Lambda
Run a Java app that resides on AWS EC2 as a daemon
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Things to note when running Python on EC2 from AWS Lambda
Best practice for logging in JSON format on AWS Lambda / Python
Simple code to call a python program from Javascript on EC2
Serverless scraping on a regular basis with AWS lambda + scrapy Part 1
[Python] I wrote a REST API using AWS API Gateway and Lambda.
Building an environment to run ChainerMN on a GPU instance on AWS
[AWS] Using ini files with Lambda [Python]
[Python] A program that creates stairs with #