14
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Aws Lambda をローカルで実行する (Python3)

Last updated at Posted at 2017-10-06

必要なソフトのインストール

sudo pip install python-lambda-local

イベントの定義

event.json
{
  "key1": "おはよう",
  "key2": "こんにちは",
  "key3": "テスト",
  "key4": "Hello",
  "key5": "Good Afternoon"
}

プログラム

lambda_function.py
# -*- coding: utf-8 -*-
#
#	lambda_function.py
#
#					Oct/6/2017
#
# --------------------------------------------------------------------
import sys
import json
 
# --------------------------------------------------------------------
def lambda_handler(event, context):
	sys.stderr.write("*** lambda_handler *** start ***\n")
	print("Received event: " + json.dumps(event, indent=2))
	print("AAA value1 = " + event['key1'])
	print("BBB value2 = " + event['key2'])
	print("CCC value3 = " + event['key3'])
	print("DDD value4 = " + event['key4'])
	print("EEE value5 = " + event['key5'])
	sys.stderr.write("*** lambda_handler *** end ***\n")
#
	return event['key1']  # Echo back the first key value
	#raise Exception('Something went wrong')
# --------------------------------------------------------------------

実行コマンド

python-lambda-local -f lambda_handler lambda_function.py event.json

実行結果

$ python-lambda-local -f lambda_handler lambda_function.py event.json
[root - INFO - 2017-10-06 10:15:38,666] Event: {'key1': 'おはよう', 'key2': 'こんにちは', 'key3': 'テスト', 'key4': 'Hello', 'key5': 'Good Afternoon'}
[root - INFO - 2017-10-06 10:15:38,667] START RequestId: 2218d928-ed76-4d6e-b290-dc597505fd4d
*** lambda_handler *** start ***
Received event: {
  "key1": "\u304a\u306f\u3088\u3046",
  "key2": "\u3053\u3093\u306b\u3061\u306f",
  "key3": "\u30c6\u30b9\u30c8",
  "key4": "Hello",
  "key5": "Good Afternoon"
}
AAA value1 = おはよう
BBB value2 = こんにちは
CCC value3 = テスト
DDD value4 = Hello
EEE value5 = Good Afternoon
*** lambda_handler *** end ***
[root - INFO - 2017-10-06 10:15:38,667] END RequestId: 2218d928-ed76-4d6e-b290-dc597505fd4d
[root - INFO - 2017-10-06 10:15:38,667] RESULT:
おはよう
[root - INFO - 2017-10-06 10:15:38,667] REPORT RequestId: 2218d928-ed76-4d6e-b290-dc597505fd4d	Duration: 0.35 ms
$ 

次の環境で動作を確認しました。

$ uname -a
Linux iwata 5.14.7-arch1-1 #1 SMP PREEMPT Wed, 22 Sep 2021 21:35:11 +0000 x86_64 GNU/Linux
$ python --version
Python 3.9.7
$ python-lambda-local --version
python-lambda-local 0.1.12
14
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?