如何访问调用我的 lambda 函数的 url

How to access the url that invoked my lambda function

我使用无服务器框架来部署我的 lambda 函数。无服务器使用 API 网关来创建我的端点。我知道可以在 GET 等方法中传递参数,并通过使用事件对象在 lambda 中使用它。前端已经部署,他们使用以下 URL 调用端点:

example.com/api/EG43

`exports.myHandler = async function(event, context) {
        ...

        // somehow access the URL which was used by the frontend 
        // to grab that EG43 and then return the result based on that key.
        // return information to the caller.  
 }

哪个EG43是一个键,之前的后端是根据那个键返回结果的。我的问题是,是否有可能以某种方式知道 URL 是什么。以下来自 AWS 的文档显示了可以使用处理程序参数读取的参数,但它没有 URL.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

您应该可以通过两种方式访问​​这些详细信息:

一个。设置一个映射模板,并将您需要的详细信息作为有效负载的一部分传递。映射模板是一个速度模板,可以访问某些变量。 See this for 您可以访问的详细信息。

b。您可以在代理集成模式下设置 lambda。有了这个,您就可以访问原始请求。 See this for more details.

以下是在代理集成模式下设置 Lambda 的方法:

当 Lambda 以代理集成方式设置时,这就是您在事件中得到的结果:

{
    "resource": "/abc/version/test",
    "path": "/abc/version/test",
    "httpMethod": "GET",
    "headers": null,
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "/abc/version/test",
        "accountId": "1234",
        "resourceId": "resourceId",
        "stage": "Stage",
        "requestId": "AWS Request ID",
        "identity": {
            "cognitoIdentityPoolId": null,
            "cognitoIdentityId": null,
            "apiKey": "API Key",
            "cognitoAuthenticationType": null,
            "userArn": "arn:aws:iam::<acc_Id>:user/yogesh",
            "apiKeyId": "api-key-id",
            "userAgent": "aws-internal/3",
            "accountId": "<AccId>",
            "caller": "<Some caller ID>",
            "sourceIp": "test-invoke-source-ip",
            "accessKey": "<Access Key>",
            "cognitoAuthenticationProvider": null,
            "user": "<Some User Id>"
        },
        "resourcePath": "/abc/version/test",
        "httpMethod": "GET",
        "extendedRequestId": "test-invoke-extendedRequestId",
        "apiId": "API ID, is typically the API GW ID"
    },
    "body": null,
    "isBase64Encoded": false
}