Path parameters, body coming as null while using authorizer in aws lambda

Viewed 1242

I have create a Lambda authorizor method (token based), with custom vpc and integrated with another lambda for api gateway authorization, when the authorization succeeds and when it wents to the destination lambda the path parameters and query parameters in event are coming as null.

in serverless.yml file authorizor function

authorizer:
  handler: authorizerHandler.verifyUser
  vpc: ${customvpc}

in serverless.yml file normal lambda

 user:
  handler: user.router
  vpc: ${customvpc}
  integration:lambda
  events:
     - http:
         path:api/v1/user/{id}
         cors: true

when user authorized, i am passing returning the object as

{
  "principalId": "yyyyyyyy", // The principal user identification associated with the token sent by the client.
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Effect": "Allow|Deny",
        "Resource": "arn:aws:execute-api:{regionId}:{accountId}:{apiId}/{stage}/{httpVerb}/[{resource}/[{child-resources}]]"
      }
    ]
  }
}

but when i tried to use the id in event.pathParameter it is returning null, the same goes with queryStringParameters. any one can help?

thanks in advance :-)

2 Answers

I think you need to use curly braces {id} instead of colons :id

 user:
  handler: user.router
  vpc: ${customvpc}
  integration:lambda
  events:
     - http:
         path:api/v1/user/{id}
         cors: true

I think I only had that issue, when was using the custom authorizer with the type token. The query string information will only be present on the authorizer with the type request.

functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          authorizer:
            arn: xxx:xxx:Lambda-Name
            resultTtlInSeconds: 0
            identitySource: method.request.header.Authorization, context.identity.sourceIp
            identityValidationExpression: someRegex
            type: request

https://serverless.com/framework/docs/providers/aws/events/apigateway/

Note that changing the type from type token to type request it will change the way you cache key of the policies.

Also more information here: https://aws.amazon.com/blogs/compute/using-enhanced-request-authorizers-in-amazon-api-gateway/

Related