[975] Creating a POSTMAN Api | AWS API Gateway Passing Data to AWS Lambda
ref: Guide to AWS Lambda Function URLs
ref: Test your Lambda function with Postman
1. Create a Lambda Function
2. Edit basic settings
The internal error in Postman test, sometimes it's because the function's running time is too long. Change the timeout time can fix this error as follows.
Changing the timeout value is very important, otherwise the function often raise the issue of "internal sever errors"!!!
3. Create a Function URL
Configuration -> Function URL
Then, the Function URL is generated and can be seen from the upper right corner. Copy it!
4. Create a test POST in Postman
Paste the Function URL in the POST URL area and then click the Save
button.
Click the Send
button, then the test results will be shown below.
5. Add information in Params, Headers, and Body
6. Modify the lambda_function and show details of "event"
Test the lambda_function from the test case of Postman. The return information is show as below.
The details of output are as follows.
{ "event": { "headers": { "content-length": "59", "x-amzn-tls-version": "TLSv1.2", "x-forwarded-proto": "https", "postman-token": "0f337b21-f35a-4b63-9904-423aea79304a", "x-forwarded-port": "443", "x-forwarded-for": "54.86.50.139", "header_name": "Alex", "accept": "*/*", "header_country": "China", "x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256", "x-amzn-trace-id": "Root=1-65d3f694-5a37ccac4de9141818821224", "host": "as2yuw3zjpqcankr7k4eteumme0bmnbz.lambda-url.ap-southeast-2.on.aws", "content-type": "application/json", "cache-control": "no-cache", "accept-encoding": "gzip, deflate, br", "user-agent": "PostmanRuntime/7.36.3" }, "isBase64Encoded": false, "rawPath": "/", "routeKey": "$default", "requestContext": { "accountId": "anonymous", "timeEpoch": 1708390036365, "routeKey": "$default", "stage": "$default", "domainPrefix": "as2yuw3zjpqcankr7k4eteumme0bmnbz", "requestId": "62ac7803-5d6b-4208-a8e9-c74647caa7a6", "domainName": "as2yuw3zjpqcankr7k4eteumme0bmnbz.lambda-url.ap-southeast-2.on.aws", "http": { "path": "/", "protocol": "HTTP/1.1", "method": "POST", "sourceIp": "54.86.50.139", "userAgent": "PostmanRuntime/7.36.3" }, "time": "20/Feb/2024:00:47:16 +0000", "apiId": "as2yuw3zjpqcankr7k4eteumme0bmnbz" }, "queryStringParameters": { "Param_Country": "China", "Param_Name": "Alex" }, "body": "{\r\n \"Body_Name\": \"Alex\",\r\n \"Body_Country\": \"China\"\r\n}", "version": "2.0", "rawQueryString": "Param_Name=Alex&Param_Country=China" } }
From the lambda_function, we can extract detail values. Specifically, for the key of body
, the value of it is the type of string, we should convert it to the JSON format. Using json.loads()
function can extract the JSON file from the string.
7. Extract specific values from "event"
Get the values from the above JSON file in the lambda_function.
import json def lambda_handler(event, context): return { # Headers "Header_Name": event["headers"]["header_name"], "Header_Country": event["headers"]["header_country"], # Params "Param_Name": event["queryStringParameters"]["Param_Name"], "Param_Country": event["queryStringParameters"]["Param_Country"], # Body "Body_Name": json.loads(event["body"])["Body_Name"], "Body_Country": json.loads(event["body"])["Body_Country"], # "POST" or "GET" "Type": event["requestContext"]["http"]["method"] }
It will shows like this.
{ "Header_Name": "Alex", "Type": "POST", "Param_Country": "China", "Body_Country": "China", "Body_Name": "Alex", "Header_Country": "China", "Param_Name": "Alex" }
8. Debug the codes in lambda_function
Monitor -> View CloudWatch logs -> Log groups -> Lambda Function Name -> Log streams
Using print()
function to test the code and it will be printed in the log streams as follows.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2020-02-20 【460】循环神经网络 RNN
2012-02-20 【013】◀▶ Diagnostics & System