LoginSignup
0
0

More than 1 year has passed since last update.

Lambdaコンテナをローカルでデバッグする方法

Posted at

概要

Lambdaをコンテナ毎デプロイすることができるようになりましたが、
今回は、ローカル環境でデバッグする方法について書き記します。
わざわざデプロイしてクラウド環境上でテストする手間が省けます。

方法

ファイル構成は以下となります(ServerlessFrameworkを利用しています)

aws-python-docker-demo
├── Dockerfile
├── README.md
├── app.py
├── requirements.txt
└── serverless.yml

今回デバッグ用に用意したコードはこちらです。
シンプルにリクエストの結果を返却するコードを書いています。

app.py
import json
import requests

def handler(event, context):

    res = requests.get("http://example.com")

    response = {
        "statusCode": 200,
        "body": res.content
    }

    return response

  1. まずDockerCLIを使用して、debug コンテナイメージをローカルでビルドします。

    docker build -t debug .
    
  2. 1.でビルドしたコンテナイメージをローカルで起動します。ローカルの9000番ポートをコンテナ内部の8080番ポートにバインドします。

    docker run -d -p 9000:8080 debug:latest
    
  3. curl を使用して関数呼び出しをテストします。ここでは、空のJSONペイロードを渡しています。

    curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
    

    結果は、無事に意図したデータが返却されました。

    {"statusCode": 200, "body": "<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <style type=\"text/css\">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"}% 
    
0
0
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
0
0