问题
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