最近人気のサーバレスアーキテクチャ。その一環を担うのが書いたコードを実行するだけの存在です。知られているのはAWSのLambdaでしょう。 開発時であったり、社内でも使えるLambdaっぽい環境が欲しいならばemulambdaを使ってみましょう。Python製のLambdaクローンです。

emulambdaの使い方

emulambdaのヘルプです。

$ emulambda --help
usage: emulambda [-h] [-s] [-t TIMEOUT] [-v] lambdapath eventfile

Python AWS Lambda Emulator. At present, AWS Lambda supports Python 2.7 only.

positional arguments:
  lambdapath            An import path to your function, as you would give it
                        to AWS: `module.function`.
  eventfile             A JSON file to give as the `event` argument to the
                        function.

optional arguments:
  -h, --help            show this help message and exit
  -s, --stream          Treat `eventfile` as a Line-Delimited JSON stream.
  -t TIMEOUT, --timeout TIMEOUT
                        Execution timeout in seconds. Default is 300, the AWS
                        maximum.
  -v, --verbose         Verbose mode. Provides exact function run, timing,
                        etc.

例えば次のようなファイルがあります。

$ cat example/example.json 
{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

そしてコードは次のようになります。

from __future__ import print_function

def very_inefficient(recursion, accumulator):
    if recursion > 0:
        accumulator = accumulator + ('f' * 1024 + '\n')
        very_inefficient(recursion - 1, accumulator)
    else:
        some_str = ' ' * (10 ** 9 / 4)
        return accumulator


def example_handler(event, context):
    result = very_inefficient(512, '')
    return event['key1']  # echo first key value

これを実行してみます。

$ emulambda example.example_handler - -s -v -t 2 < example/ex-stream.ldjson
Entering stream mode.

Object 1 { "key1": "value1", "key2": "value2", "key3": "value3" }
Executed example.example_handler
Estimated...
...execution clock time:		 208ms (300ms billing bucket)
...execution peak RSS memory:		 368M (385875968 bytes)
----------------------RESULT----------------------
value1

Object 2 { "key2": "value2b", "key3": "value3b" }

There was an error running your function. Ensure it has a signature like `def lambda_handler (event, context)`.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/emulambda/__init__.py", line 160, in invoke_lambda
    return _invoke_lambda(lfunc, event, context)
  File "/usr/local/lib/python2.7/site-packages/emulambda/timeout.py", line 20, in wrapper
    result = func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/emulambda/__init__.py", line 155, in _invoke_lambda
    r = l(e, c)
  File "/Users/nakatsugawa/Downloads/zombie_epidemic/emulambda/example/__init__.py", line 14, in example_handler
    return event['key1']  # echo first key value
KeyError: 'key1'
Executed example.example_handler
Estimated...
...execution clock time:		 -1ms (0ms billing bucket)
...execution peak RSS memory:		 374M (392675328 bytes)
----------------------RESULT----------------------
EMULAMBDA: LAMBDA ERROR

Object 3 { "key1": "value1", "key2": "value2", "key3": "value3" }
Executed example.example_handler
Estimated...
...execution clock time:		 197ms (200ms billing bucket)
...execution peak RSS memory:		 374M (392691712 bytes)
----------------------RESULT----------------------
value1
  :
Object 18 { "key1": "value1b", "key2": "value2b", "key3": "value3b" }
Executed example.example_handler
Estimated...
...execution clock time:		 188ms (200ms billing bucket)
...execution peak RSS memory:		 412M (432082944 bytes)
----------------------RESULT----------------------
value1b

Summary profile from stream execution:
Samples: 18
(ERRORS DETECTED: Removing timing samples from aborted invocations.)
New sample size: 17
[208.7700366973877, 197.10016250610352, 217.46587753295898, 187.3149871826172, 192.95406341552734, 198.44794273376465, 204.28895950317383, 195.2509880065918, 195.96195220947266, 183.29095840454102, 184.88001823425293, 175.92906951904297, 180.62090873718262, 175.16088485717773, 183.3639144897461, 185.38784980773926, 188.2319450378418]
Clock time:
	Min: 175ms, Max: 217ms, Median: 188ms, Median Billing Bucket: 200ms, Rounded Standard Deviation: 12ms
Peak resident set size (memory):
	Min: 368M, Max: 412M

このように結果が得られます。今のところPythonのコードにしか対応していません。好きなときに好きなコードを実行できる、そんなLambdaならではの使い方を考えてみてください。

emulambdaはPython製のオープンソース・ソフトウェア(Apache License 2.0)です。

fugue/emulambda