How to get API gateway url in AWS lambda function?

守給你的承諾、 提交于 2019-12-23 19:01:07

问题


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/second

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.


回答1:


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'

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.



来源:https://stackoverflow.com/questions/52575952/how-to-get-api-gateway-url-in-aws-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!