如何在AWS Lambda函数中获取API网关URL? [英] How to get API gateway url in AWS lambda function?

查看:291
本文介绍了如何在AWS Lambda函数中获取API网关URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用API​​ URL调用lambda函数的方案.调用lambda函数后,我想要lambda函数中的特定URL.

I have a scenario where I am using an API URL to invoke lambda function. After invoking the lambda function, I want that particular URL in the lambda function.

https://******.execute-api.eu-west-1.amazonaws.com/test/first

https://******.execute-api.eu-west-1.amazonaws.com/test/first

https://******.execute-api.eu-west-1.amazonaws.com/test/second

https://******.execute-api.eu-west-1.amazonaws.com/test/second

从此URL,我希望在lambda中将资源命名为first或second.这里的测试是我使用我的API的阶段名称.我要创建多个资源来更改lambda的行为.我该怎么办?任何帮助将不胜感激.

From this URL, I want the resource named first or second in lambda. Here the test is the stage name where I deplou\y my API. I have multiple resources created from that I want to change the behavior of lambda. How could I do this? Any help would be appreciated.

推荐答案

您可以从Lambda函数的events变量中的值重建完整的url.

You can reconstruct the full url from values in the Lambda function's events variable.

events['headers']['Host'] = '******.execute-api.eu-west-1.amazonaws.com'
events['requestContext']['stage'] = 'test'
events['path'] = '/first'

总共,您可以将它们加在一起得到https://******.execute-api.eu-west-1.amazonaws.com/test/first:

So altogether, you can get https://******.execute-api.eu-west-1.amazonaws.com/test/first from adding them together:

'https://' + events['headers']['Host'] + '/' + events['requestContext']['stage'] + events['path']

请参见

See the Lambda Proxy integration part of the AWS documentation for details on other info you get can out of the events variable.

这篇关于如何在AWS Lambda函数中获取API网关URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆