[英]AWS Lambda, Python serverless handler with url pattern / parameters
I am trying to set up a lambda function using Python and serverless.我正在尝试使用 Python 和无服务器设置 lambda function。 I was able to perform simple POST/GET requests and have them to proxy to my lambda function just fine.我能够执行简单的 POST/GET 请求并让它们代理到我的 lambda function 就好了。
Now I am struggling getting something like this to work with serverless:现在我正在努力让这样的东西与无服务器一起工作:
GET /my_function/{foo}
In a way that I could get it by:我可以通过以下方式获得它:
http://....aws.../my_function/bar
This is what I'm trying:这就是我正在尝试的:
serverless.yml无服务器.yml
functions:
my_function:
handler: handler.my_function
events:
- http: GET /my_function/{foo}
It deployed to aws, but I am not able to get the value.它部署到 aws,但我无法获得价值。 It says there is no key for the event:它说该事件没有密钥:
handler.py:处理程序.py:
def sitemap_entries(event, context):
s = MyNiceClass(event['foo'])
...
What am I missing?我错过了什么?
PS: It works on local invoke when I send --data '{"foo": "bar"}'
PS:当我发送--data '{"foo": "bar"}'
时,它适用于本地调用
Thank you谢谢
If you use the Lambda Proxy Integration (which is the default setting so unless you've changed it you'll be using it) then the path parameters are inside event
in an attribute called pathParameters
, so to access them use:如果您使用Lambda 代理集成(这是默认设置,除非您更改它,否则您将使用它)然后路径参数在名为pathParameters
的属性中的event
内部,因此要访问它们,请使用:
event.get('pathParameters', {}).get('foo', None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.