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
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.
If you want to script your Lambda in python, you must create it with the name ** lambda_function.py **. Since the function ** lambda_handler ** in it is called, prepare the lambda_handler function as well.
lambda_function.py
# -*- 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.
bash
$ zip function.zip lambda_function.py
In the case of MAC, if you compress it on Explorer, the .DS_Store file will be included and lambda_function.py cannot be read, so compress it with a command.
Select "Lambda"-> "Create Function" from the AWS console screen. Enter the function name and the runtime will be Python 3.6 this time.
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.
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.
With the test you created selected, click the test button.
It is successful because the execution result appears under the function code.
Click the "Add Trigger" button in the designer.
Trigger: EventBridge (CloudWatch Events) Rule: New rule Rule type: Schedule expression Scheduled expression: cron (30 1 * *? *)
Click the "Add" button and the settings are complete.
Recommended Posts