How do I correctly specify the "path:" when setting up an http event for a Lambda function?

After much trial and error, I’m going to ask for help on a (seemingly) simple issue.
I’m trying to set up API gateway to handle http requests. My serverless.yml file looks like this:

functions:
  create:
    handler: handlers/createUser.Create
    events:
      - http:
          path: createUser/Create
          method: post
          integration: lambda-proxy
          cors: true

I have a “handlers/” directory at the root of my serverless service, containing the handler “createUser.js”.
Inside of “createUser.js”, I’m exporting like this:
export default function Create(event, context, callback) {

}
After a successful deployment, I’m trying to use the endpoint which is:
https://XXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/createUser/Create
(using the proper url of course, not XXXXXXXX !) , but I keep receiving the same error:
{“message”:“Missing Authentication Token”}

According to this article
http://www.awslessons.com/2017/aws-api-gateway-missing-authentication-token/
There are 3 possible causes of this error, and I’ve eliminated 2 of them, leaving “Incorrect URL String” as the culprit. I feel like I’m probably causing this error by the way I’m specifying my http path and/or handler… any help would be much appreciated!

Note: I’ve also tried this in my sls.yml:
path: createUser/Create
and using this url: https://XXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/handlers/createUser/Create
but the result is the same error.

Your formatting is wrong in the serverless.yml file. You need to nest another level deep under http.

functions:
  create:
    handler: handlers/createUser.Create
    events:
      - http:
          path: createUser/Create
          method: post
          cors: true

Also I think you need to not default export your create function.

export function Create(event, context, callback) {
...
}

Hope this helps :sweat_smile:

Thank you. I’ll try changing the way I’m exporting my function. My formatting is actually correct in my serverless.yml, but I messed it up while pasting, sorry about that.

Update:
I’ve removed the handlers/ directory from the picture to simplify. My handler function createUser.js is now in the root of my service with my serverless.yml.

I’m exporting my function like this:
export function Create(event, context, callback) {
//code
}

Serverless.yml like this:

functions:
  create:
     handler: createUser.Create
     events:
        - http:
             path: createUser/Create
             method: post
             integration: lambda-proxy
             cors: true

When I test the function "create" in the AWS console, I'm getting this:
 {
  "errorMessage": "Cannot find module '/var/task/createUser'",
  "errorType": "Error",
  "stackTrace": [
    "require (internal/module.js:12:17)"
  ]
}

I should note that I’m using the serverless-babel-plugin as well, but I’m not sure if that’s relevant here or not… It’s possible that the issue is in the packaging of my service

It looks like the file is missing. Run sls deploy -n. This should create a zip file inside your .serverless folder. Unzip that file and you can see what’s being uploaded. It may be that serverless-babel-plugin is putting the compiled JS files into another folder (I don’t use it so I’m not sure how it works).

Thanks for the tip, will try that now.

I managed to get it working. I don’t think the serverless-babel-plugin was an issue, as all the required files seemed to be zipped correctly. ( However, I decided to remove the plugin for other reasons)
In the end, this is what I’m using successfully:

serverless.yml:

functions:
  create:
    handler: handlers/subscribeUser.subscribe
    events:
      - http:
          path: subscribe
          method: post
          integration: lambda-proxy
          cors: true

and in my handler (subscribeUser.js):

exports.subscribe = subscribe;
function subscribe(event, context, callback) {
    // code
}

All is working well now!

1 Like