1
3

More than 3 years have passed since last update.

初めてのAWS Lambda〜Hello World

Last updated at Posted at 2020-05-20

AWS Lambdaとは

サーバレスでコードを実行でき、Java、Go、PowerShell、Node.js、C#、Python、Rubyをサポートしている
https://aws.amazon.com/jp/lambda/faqs/

実際に使ってみる

AWSのチュートリアルを参考に。
https://aws.amazon.com/jp/getting-started/hands-on/run-serverless-code/

  • コンソールを起動
    「コンピューティング」→「Lambda」

    スクリーンショット 2020-05-18 19.08.51.png

  • 設計図を選択
    設計図=テンプレートのようなものかな?

    「Hello」で検索すると、node.jsとpythonの「Hello World」設計図が検索されるので、今回は「hello-world-python」
    スクリーンショット 2020-05-18 19.24.02.png

設計図の中身はこんな感じです

import json

print('Loading function')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])
    print("value2 = " + event['key2'])
    print("value3 = " + event['key3'])
    return event['key1']  # Echo back the first key value
    #raise Exception('Something went wrong')
  • テストイベントの作成
    関数作成画面右上にある「テストイベント作成」から関数呼び出し時に渡すjsonの作成
{
  "key1": "Hello!World1",
  "key2": "Hello!World2",
  "key3": "Hello!World3"
}
  • イベントを選択して関数実行
    実行ログが吐き出されて、関数が実行されたことを確認
START RequestId: e5f1d7c7-9082-4634-a7aa-4363eab2c2e2 Version: $LATEST
value1 = Hello!World1
value2 = Hello!World2
value3 = Hello!World3
END RequestId: e5f1d7c7-9082-4634-a7aa-4363eab2c2e2
REPORT RequestId: e5f1d7c7-9082-4634-a7aa-4363eab2c2e2  Duration: 1.37 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 49 MB  

次回

今回は用意された設計図から関数を作成したので、次回は自分で関数を作成〜実行までを試す

1
3
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
1
3