Write AWS Lambda function in Python

At the moment, JavaScript / Node.js is the only language that can be used as a Lambda function in AWS Lambda. But some people want to write it in Python, so I ran the script written in Python.

But it's not difficult. We know that scripts that run as Lambda functions can execute external commands. We also found in a previous survey that there is a python runtime in the environment (see ls results in Uncover AWS Lambda).

So all you have to do is generate a script written in Python using JavaScript in your Lambda function and call it as an external command using child_process ().

Let's do it now.

This time, instead of generating the script in the Lambda function, the script prepared in advance is saved in S3, and it is acquired by HTTP access in the environment of the Lambda function and executed. In addition, when fetching a file by HTTP, curl is used because there is no wget in the environment of Lambda function for some reason.

First, prepare a Python script that you want to execute. This time I tried it and made it look like this.

test.py


#! /usr/bin/python
print "Hello, Wordl!"

Give this to S3 and make public to enable HTTP access.

Next is the code of the Lambda function that is actually registered.

index.js


exports.handler = function(event, context) {
    var exec = require('child_process').exec;
    
    var cmd = "curl -s https://s3-ap-northeast-1.amazonaws.com/<bucket name>/test.py > /tmp/test.py;chmod 755 /tmp/test.py;/tmp/test.py"
    var child = exec(cmd, function(error, stdout, stderr) {
        if (!error) {
            console.log('standard out: ' + stdout);
            console.log('standard error: ' + stderr);
            context.done();
        } else {
            console.log("error code: " + error.code + ", err: " + error);
            context.done(error,'lambda');
        }
    });
};

Yes, as you may have noticed, the previous script is the same. I'm just changing the external command to execute. The commands that are actually being executed are simply executing the following in order.

  1. Get a Python script to run using curl and save it in / tmp
  2. Grant execute permission
  3. Run

Now let's invoke the Lambda function.

2014-11-26T11:51:47.683Z	a0862c9f-7562-11e4-8f05-ebd8b45899f3	standard out: Hello, Wordl!

Oh, it was executed properly and "Hello, World!" Was output!

So, regardless of whether it is actually useful or not, I ran a script written in Python with a Lambda function.

** Disclaimer This is an individual opinion, regardless of the company or organization to which it belongs. ** **

Recommended Posts

Write AWS Lambda function in Python
[Python] Scraping in AWS Lambda
Write Python in MySQL
Write Pandoc filters in Python
Create a function in Python
Write beta distribution in Python
Write python in Rstudio (reticulate)
ntile (decile) function in python
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
Deploy Python3 function with Serverless Framework on AWS Lambda
[Python] Retry process (Exponential Backoff) memo in AWS Lambda
Write a binary search in Python
Summary if using AWS Lambda (Python)
Function argument type definition in python
python function ①
Write JSON Schema in Python DSL
Write an HTTP / 2 server in Python
Write A * (A-star) algorithm in Python
Measure function execution time in Python
Run Python on Schedule on AWS Lambda
[Python] Make the function a lambda function
Write selenium test code in python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Function synthesis and application in Python
Notify HipChat with AWS Lambda (Python)
[AWS Lambda] Use any container Image in Python very simply
Write C unit tests in Python
python function ②
Set CloudWatch Events in AWS Lambda function (WebAPI) deployed via zappa
Best practice for logging in JSON format on AWS Lambda / Python
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I compared Node.js and Python in creating thumbnails using AWS Lambda
Install pip in Serverless Framework and AWS Lambda with Python environment
Write documentation in Sphinx with Python Livereload
[AWS] Using ini files with Lambda [Python]
Use print in a Python2 lambda expression
Precautions when pickling a function in python
Write the test in a python docstring
Obtaining temporary AWS credentials in PHP, Python
Write a short property definition in Python
Write O_SYNC file in C and Python
Write a Caesar cipher program in Python
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Lambda function to take AMI backup (python)
[Python] Run Headless Chrome on AWS Lambda
Write python modules in fortran using f2py
Write a simple Vim Plugin in Python 3
Connect to s3 with AWS Lambda Python
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
Python + Selenium + Headless Chromium with aws lambda
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda