【问题标题】:How to access the url that invoked my lambda function如何访问调用我的 lambda 函数的 url
【发布时间】:2018-09-17 19:42:46
【问题描述】:

我使用无服务器框架来部署我的 lambda 函数。 Serverless 使用 API Gateway 来创建我的端点。我知道可以在 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

【问题讨论】:

  • 请提供您的编码尝试。
  • @wayneOS 已编辑。

标签: node.js amazon-web-services aws-lambda handler


【解决方案1】:

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

一个。设置映射模板,并将所需的详细信息作为有效负载的一部分传递。映射模板是可访问某些变量的速度模板。 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
}

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 2019-03-09
    • 2020-06-17
    • 2022-11-03
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2021-03-01
    相关资源
    最近更新 更多