2016-08-23 48 views
1

我想用一个的路径PARAM /customer/{customerId}为了GET要求使用AWS LAMBDA查询客户:使用路径参数1.0

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

我希望如何定义路径PARAM为了将它传递给我的AWS Lambda函数,使用无服务器框架1.0

回答

4

定义在serverless.yml

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

访问customerId

const customerId = event.pathParameters.customerId; 
2

变化路径名

path: customer/{customerId} 

更改您的handler.js文件

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

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

// you can write your logic here 


}; 
+1

我可以创建资源,即工作!但是路径参数似乎没有传递给Lambda函数......我得到一个“消息”:“提供的关键元素与模式不匹配”​​ – Marcel

+0

再次检查它。如果要注册客户使用一些数据库,你可以在这里写代码 –

+0

谢谢,我刚刚解决了你的帮助问题! – Marcel

-1

#解决方案

  1. 定义路径参数:param - 例如客户ID - 在serverless.yml

path: customer/customerId

  • 在API网关,您的API下/customer/{customerId}合并请求,并创建一个新的身体映射模板这是为了响应application/json,并具有以下内容:
  • { "customerId": "$input.params('customerId')" }

    现在的路径PARAM客户ID我s到传递到AWS lambda函数作为您的JSON事件:在代码

    { 
        "input": "{\"customerId\":\"[email protected]\"}", 
        "response": { 
        "Item": { 
         "password": "abc#123", 
         "email": "[email protected]" 
        } 
        } 
    } 
    
    相关问题