Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use path params in serverless framework 1.0

I want to use the path param /customer/{customerId} of a GET request in order to query a customer using AWS Lambda:

functions:
  createCustomer:
    handler: handler.createCustomer
    events:
    - http:
        path: customer
        method: post
  readCustomer:
    handler: handler.readCustomer
    events:
    - http:
        path: customer
        method: get

How do I have to define the path param in order to pass it to my AWS Lambda function using serverless framework 1.0?

like image 457
Marcel Avatar asked Aug 23 '16 09:08

Marcel


2 Answers

Define in serverless.yml

readCustomer:
  handler: handler.readCustomer
  events:
    - http:
        path: customer/{customerId}
        method: get

Access customerId in code

const customerId = event.pathParameters.customerId;
like image 199
Frank Avatar answered Oct 24 '22 18:10

Frank


change path name

path: customer/{customerId}

Change your handler.js file

module.exports.createCustomer= function(event, context) {

{ message: 'Go Serverless v1.0! Your function executed successfully!', event }

// you can write your logic here


};
like image 21
Niroshan Ranapathi Avatar answered Oct 24 '22 18:10

Niroshan Ranapathi