AWS API Gateway + Lambda: How to Add a “Get By ID” Resource (Path Parameter)

This thing is not trivial and I couldn’t find a simple explanation. Here is the exact sequence to add a Get By ID-style path parameter like /users/{userid} in API Gateway and wire it to Lambda.

Goal

Turn an existing resource like /users into:

  • /users
  • /users/{userid} (new resource)

Step-by-step

  1. Select an existing resource (e.g. users).
  2. Click Actions and select Create Resource.
  3. In Resource Name, put the parameter name in curly braces (e.g. {userid}).
  4. In Resource Path, you should see read-only text like /users/ and a textbox for the parameter.
  5. Enter {userid} in the textbox and click Create Resource.
  6. You should now see a new branch in your API tree: /{userid}. Select it.
  7. Click Actions → Create Method and add ANY (or the specific method you need).
  8. Integrate it with the Lambda function you already used for this resource. Don’t forget to check Use Lambda Proxy integration.
  9. Select the new ANY method, open Method Request and ensure that userid appears in the Request Paths section.

Lambda side

That’s it. Now handle it in your Lambda (support both QueryString and Path parameters):


var params = event.pathParameters ? event.pathParameters : event.queryStringParameters;

Use params.userid to get the value.

Comments

Popular posts from this blog

Decimal number issue from server to client. Wow! Didn't expect this @_@

Shrinking node_modules for AWS Lambda (My First “npm shock” and a Practical Fix)