Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get API gateway url in AWS lambda function?

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.

like image 542
ABCD Avatar asked Sep 30 '18 08:09

ABCD


People also ask

How do I find my API gateway URL AWS?

You can find a REST API's root URL in the Stage Editor for the API in the API Gateway console. It's listed as the Invoke URL at the top. If the API's root resource exposes a GET method without requiring user authentication, you can call the method by clicking the Invoke URL link.

Can we call API gateway from Lambda?

[Answer] There is nothing wrong with this model. And yes, you can call this API (Lambda proxy) as any Rest API.


1 Answers

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.

like image 144
Tom Avatar answered Nov 15 '22 03:11

Tom