kl27driver
kl27driver

Reputation: 89

How do I get access to the request and path variables from AWS API Gateway URI in an AWS Lambda function

I am replatforming an existing application to work with AWS API Gateway and AWS Lambda. The current application exposes it functionality as REST API which is implemented as a Spring Boot application and Spring REST Controller annotations.

While I am able to get the request body JSON from the API Gateway into the Lambda function, in certain cases I would need variables from the Request object as well as path variables accessible in the Lambda function. I did take a look at the Lambda Context object but it did not have anything that could help me in this regard.

Example API URLs: I use a path variable similar to the id variable in the following API call GET http://www.example.com/users/{id}/alerts. {id} will be the path variable here. An example of how we use a request variable is in the following URL where the alert id is passed as a query string parameter - GET http://www.example.com/users/{id}/alerts?id=1234

Is there any recommended way to get this done? I do not want to use the RequestHandler interface as I am aiming to tie each API to a separate Lambda function.

Upvotes: 3

Views: 6972

Answers (2)

RyanG
RyanG

Reputation: 4152

If you are using AWS integration type:

Use a mapping template to send $input.params('id') property in the request body to your Lambda function.

If you are using AWS_PROXY integration type:

You can access the path parameters via the "pathParameters" property of the incoming event. For more details, please read the docs.

Upvotes: 3

user7401700
user7401700

Reputation:

I suggest you use the Lambda Proxy integration type, it makes all the information you need available with the least hassle. So, the properties you need to read will be available as follows:

  • your /users/... path: event.path
  • your id in query string: event.queryStringParameters.id
  • you'll need the http method as well: event.httpMethod
  • your path parameters like the id one: event.pathParameters

lambda proxy integration option in api gateway

Let me know if you need any more information.

Upvotes: 2

Related Questions