...

/

Handling GET Requests with Path Parameters in AWS Lambda

Handling GET Requests with Path Parameters in AWS Lambda

Learn how to handle GET requests with path parameters in AWS Lambda using TypeScript.

Handling path parameters in Lambda functions

A number of our Lambda functions have a path parameter within them. So, to GET details of a particular user, we will need to use the URL /users/{userId}, where we substitute the {userId} variable with the actual value. In other words, to GET the details of a user named test_user_1, our path would need to be /users/test_user_1. This is a standard REST pattern, where the URL itself contains a parameter.

Let’s now take a look at how the definition of the user’s GET Lambda function in our template.yaml file supports this syntax as follows:

Press + to interact
UserGetApiFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: api-handlers/
Handler: users.getHandler
Runtime: nodejs14.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /users/{userId}
Method: get
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref UserTable

Here, the definition of the Lambda function named UserGetApiFunction is almost identical to the previous definition, with two notable exceptions.

  • Firstly, we have specified on line 5 that the handler function is named getHandler within the users.js file, so we will need to create an exported function with this name. ...