[英]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
对象将为空且没有headers
和requestContext
属性。 So, try this using the API Gateway console or browsing the URL directly. 因此,请尝试使用API网关控制台或直接浏览URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.