[975] Creating a POSTMAN Api | AWS API Gateway Passing Data to AWS Lambda

alex_bn_lee / 2024-02-20 / 原文

ref: AWS Lambda Function URLs

ref: Guide to AWS Lambda Function URLs


1. Create a Lambda Function

aws -> Services -> Lambda

2. Create a Function URL

Configuration -> Function URL

Then, the Function URL is generated and can be seen from the upper right corner. Copy it!

3. Create a test POST in Postman

Paste the Function URL in the POST URL area and then click the Savebutton.

Click the Send button, then the test results will be shown below.

4. Add information in Params, Headers, and Body

5. 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.

6. 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"
}