直接打 API Gateway

Part3 中,Deployment 完時,CloudFomration 的 Output 可以看到 ApiGateway 的 URI。如果沒有記住,我們也可以在 AWS Console 中的 API Gateway Stage 找到 URI apigateway

找到 URI 以後,可以直接使用 CURL 做測試

1$ curl https://t3faydf1rb.execute-api.ap-northeast-1.amazonaws.com/Prod/hello
2{"message":"hello world"}%

取得執行 Log

透過 AWS Console

AWS Lambda 預設會建立 CloudWatch Log Group,我們可以在 CloudWatch 中的 Log Group 找到 Lambda 執行的 Log。 預設的 Log Group 名字是 /aws/lambda/<function name> cloudwatch_log_group

在 Log Steam 裡面,可以找到艾倫在 Part3 中,修改 Lambda Function Code 時,所印出 Event 的 Log cloudwatch_log_detail

SAM Log

 1$ sam logs -n HelloWorldFunction --stack-name sam-app --tail 
 2You can now use 'sam logs' without --name parameter, which will pull the logs from all supported resources in your stack.
 3
 42024/03/26/[$LATEST]a0274c6a05404868b3fb3c15d3443312 2024-03-26T13:12:19.327000 {
 5  "time": "2024-03-26T13:12:19.327Z",
 6  "type": "platform.initStart",
 7  "record": {
 8    "initializationType": "on-demand",
 9    "phase": "init",
10    "runtimeVersion": "nodejs:20.v19",
11    "runtimeVersionArn": "arn:aws:lambda:ap-northeast-1::runtime:ecf83feba464e64dae4dc1a80327f8f786abd91aed7e0d884474a062cd1a7d63",
12    "functionName": "sam-app-HelloWorldFunction-Qiut5gK4QT6I",
13    "functionVersion": "$LATEST"
14  }
15}
162024/03/26/[$LATEST]a0274c6a05404868b3fb3c15d3443312 2024-03-26T13:12:19.468000 {
17  "time": "2024-03-26T13:12:19.468Z",
18  "type": "platform.start",
19  "record": {
20    "requestId": "baf81fc5-767d-43e9-a780-6f96f0955a6c",
21    "version": "$LATEST",
22    "tracing": {
23      "spanId": "6c72e2f7121224ce",
24      "type": "X-Amzn-Trace-Id",
25      "value": "Root=1-6602c9b3-1f608cce18790dbc0c64ca6e;Parent=7621c3d74d395e42;Sampled=1"
26    }
27  }
28}
292024/03/26/[$LATEST]a0274c6a05404868b3fb3c15d3443312 2024-03-26T13:12:19.469000 {
30  "timestamp": "2024-03-26T13:12:19.469Z",
31  "level": "INFO",
32  "requestId": "baf81fc5-767d-43e9-a780-6f96f0955a6c",
33  "message": "EVENT: \n{}"
34}
352024/03/26/[$LATEST]a0274c6a05404868b3fb3c15d3443312 2024-03-26T13:12:19.514000 {
36  "time": "2024-03-26T13:12:19.514Z",
37  "type": "platform.report",
38  "record": {
39    "requestId": "baf81fc5-767d-43e9-a780-6f96f0955a6c",
40    "metrics": {
41      "durationMs": 44.002,
42      "billedDurationMs": 45,
43      "memorySizeMB": 128,
44      "maxMemoryUsedMB": 66,
45      "initDurationMs": 139.993
46    },
47    "tracing": {
48      "spanId": "6c72e2f7121224ce",
49      "type": "X-Amzn-Trace-Id",
50      "value": "Root=1-6602c9b3-1f608cce18790dbc0c64ca6e;Parent=7621c3d74d395e42;Sampled=1"
51    },
52    "status": "success"
53  }
54}

使用 SAM 測試雲上 Lambda Function

除了使用基本的 Curl 以外, SAM remote 讓我們可以直接在地端觸發雲上的 Lambda Function ,並且提供一些額外的資訊,例如 billedDurationMs

 1$ sam remote invoke
 2
 3Invoking Lambda Function HelloWorldFunction
 4{"time":"2024-03-26T13:12:19.327Z","type":"platform.initStart","record":{"initializationType":"on-demand","phase":"init","runtimeVersion":"nodejs:20.v19","runtimeVersionArn":"arn:aws:lambda:ap-northeast-1::runtime:ecf83feba464e64dae4dc1a80327f8f786abd91aed7e0d884474a062cd1a7d63","functionName":"sam-app-HelloWorldFunction-Qiut5gK4QT6I","functionVersion":"$LATEST"}}
 5{"time":"2024-03-26T13:12:19.466Z","type":"platform.initRuntimeDone","record":{"initializationType":"on-demand","phase":"init","status":"success"}}
 6{"time":"2024-03-26T13:12:19.466Z","type":"platform.initReport","record":{"initializationType":"on-demand","phase":"init","status":"success","metrics":{"durationMs":139.899}}}
 7{"time":"2024-03-26T13:12:19.468Z","type":"platform.start","record":{"requestId":"baf81fc5-767d-43e9-a780-6f96f0955a6c","version":"$LATEST","tracing":{"spanId":"6c72e2f7121224ce","type":"X-Amzn-Trace-Id","value":"Root=1-6602c9b3-1f608cce18790dbc0c64ca6e;Parent=7621c3d74d395e42;Sampled=1"}}}
 8{"timestamp":"2024-03-26T13:12:19.469Z","level":"INFO","requestId":"baf81fc5-767d-43e9-a780-6f96f0955a6c","message":"EVENT: \n{}"}
 9{"time":"2024-03-26T13:12:19.511Z","type":"platform.runtimeDone","record":{"requestId":"baf81fc5-767d-43e9-a780-6f96f0955a6c","status":"success","tracing":{"spanId":"3444a7bf6600aaff","type":"X-Amzn-Trace-Id","value":"Root=1-6602c9b3-1f608cce18790dbc0c64ca6e;Parent=6c72e2f7121224ce;Sampled=1;Lineage=7e433ca5:0"},"spans":[{"name":"responseLatency","start":"2024-03-26T13:12:19.468Z","durationMs":6.097},{"name":"responseDuration","start":"2024-03-26T13:12:19.474Z","durationMs":0.106},{"name":"runtimeOverhead","start":"2024-03-26T13:12:19.475Z","durationMs":36.296}],"metrics":{"durationMs":43.028,"producedBytes":57}}}
10{"time":"2024-03-26T13:12:19.514Z","type":"platform.report","record":{"requestId":"baf81fc5-767d-43e9-a780-6f96f0955a6c","status":"success","metrics":{"durationMs":44.002,"billedDurationMs":45,"memorySizeMB":128,"maxMemoryUsedMB":66,"initDurationMs":139.993},"tracing":{"type":"X-Amzn-Trace-Id","value":"Root=1-6602c9b3-1f608cce18790dbc0c64ca6e;Parent=7621c3d74d395e42;Sampled=1","spanId":"6c72e2f7121224ce"}}}{"statusCode":200,"body":"{\"message\":\"hello world\"}"}
11SAM CLI update available (1.113.0); (1.111.0 installed)
12To download: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html