简体   繁体   English

将无服务器API网关URL作为同一堆栈中Lambda函数的参数传递

[英]Passing serverless API Gateway URL as a parameter for a Lambda function in the same stack

I started building a JAM app using AWS Lambda, AWS API Gateway and serverless, as well as another vendors API. 我开始使用AWS Lambda,AWS API Gateway和无服务器以及其他供应商API构建JAM应用程序。

This vendor API is invoked by a Lambda function, and requires a callback URL to be passed to receive some data once it has done its job. 此供应商API由Lambda函数调用,并且需要传递回调URL以在完成其工作后接收某些数据。

As I'm spawning everything with serverless, going to the console and extracting the API URL to manually set is as an env variable is of no use to me, and I need a way so that serverless can pass the exposed API endpoint URL to the lambda function. 当我用无服务器生成所有内容时,转到控制台并提取API URL以手动设置作为env变量对我没用,我需要一种方法使无服务器可以将公开的API端点URL传递给lambda函数。

How do I get the Lambda function HTTP event URI as an env or something passable to another Lambda function in the same stack? 如何将Lambda函数HTTP事件URI作为env或可传递给同一堆栈中的另一个Lambda函数的东西?

Can someone provide some serverless snippet on how to achieve this? 有人可以提供一些无服务器片段如何实现这一目标? Thanks! 谢谢!

If you want to find the API Gateway URL that triggered the Lambda function, you need to check the event variable that your Lambda function receives. 如果要查找触发Lambda函数的API网关URL,则需要检查Lambda函数接收的event变量。

event.headers.Host -> abcdefghij.execute-api.us-east-1.amazonaws.com
event.requestContext.stage -> dev
event.requestContext.resourcePath -> my-service/resource

If you want to build the API Gateway URL (example: https://abcdefghij.execute-api.us-east-1.amazonaws.com/dev/my-service/resource ), use: 如果要构建API网关URL(例如: https//abcdefghij.execute-api.us-east-1.amazonaws.com/dev/my-service/resource ),请使用:

const url = `https://${event.headers.Host}/${event.requestContext.stage}/${event.requestContext.resourcePath}`;

Complete test example: 完整的测试示例:

module.exports.hello = (event, context, callback) => {

  const url = `https://${event.headers.Host}/${event.requestContext.stage}/${event.requestContext.resourcePath}`;

  const response = {
    statusCode: 200,  
    headers: {
      'Access-Control-Allow-Origin': '*'
    },  
    body: JSON.stringify({
        message: url
    })
  };

  callback(null, response);
};

Note : if you test this directly in the AWS Lambda Console, it may throw an error because the event object will be empty and without the headers and requestContext properties. 注意 :如果您直接在AWS Lambda控制台中对此进行测试,则可能会抛出错误,因为event对象将为空且没有headersrequestContext属性。 So, try this using the API Gateway console or browsing the URL directly. 因此,请尝试使用API​​网关控制台或直接浏览URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda 参数传递错误 | API 网关 - AWS Lambda parameter passing error | API Gateway 使用无服务器向同一个网关API添加新的lambda函数? - Using serverless to add new lambda functions to same Gateway API? 从Lambda的API网关获取URL参数 - Getting URL Parameter from API Gateway in Lambda 将 API 网关作为触发器添加到 Lambda Function 使用无服务器脚本而不是 UI - Adding API Gateway as Trigger to Lambda Function Using Serverless Script not the UI 使用与无服务器数据库对话的 Lambda function 集成的 AWS API 网关创建完全无服务器的解决方案 - Creating a completely serverless solution using AWS API Gateway integrated with a Lambda function that talks to a serverless database AWS Serverless - 我可以在我的 serverless.yml 文件中配置我的提供程序或 lambda function 以使用同一 yml 文件中的 API 网关资源吗? - AWS Serverless - Can I configure my provider or lambda function in my serverless.yml file to use an API Gateway resource in the same yml file? 具有 API 网关和 Lambda 的 Aurora Serverless 超时 - Aurora Serverless with API Gateway and Lambda is timing out 具有Lambda集成的无服务器部署API网关 - Serverless Deployment API Gateway with Lambda Integration 通过无服务器在2个网关阶段部署Lambda功能 - Deploy Lambda function in 2 Stages of Gateway via serverless 获取API网关传递的Lambda(Nodejs)中的url参数 - Get url parameter in Lambda (Nodejs) passed by API Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM