我正在创建(第一个)Lambda函数。Lambda设计用于打开/关闭飞利浦色调灯泡。Lambda功能的触发器是AWS IoT(仪表板)按钮
但是,在触发Lambda函数后,我收到以下错误消息:
[ERROR] TypeError: lambda_handler() takes 1 positional argument but 2 were given
Traceback (most recent call last):
File "/var/runtime/bootstrap.py", line 131, in handle_event_request
response = request_handler(event, lambda_context)
有人能洞察我的Python代码的错误吗?谢谢
import requests,json
bridgeIP = "PublicIPAddress:999"
userID = "someone"
lightID = "2"
def lambda_handler(lightID):
url = "http://"+bridgeIP+"/api/"+userID+"/lights/"+lightID
r = requests.get(url)
data = json.loads(r.text)
if data["state"]["on"] == False:
r = requests.put(url+"/state",json.dumps({'on':True}))
elif data["state"]["on"] == True:
r = requests.put(url+"/state",json.dumps({'on':False}))
lambda_handler(lightID)
错误消息告诉您
lambda_handler
函数有两个位置参数,但函数定义为只接受一个Lambda会自动为处理程序函数提供两个参数,因此需要定义函数以接受两个参数
您可以通过将函数定义更改为:
您的处理函数应定义为:
从AWS Lambda Function Handler in Python - AWS Lambda:
很可能您的
event
只包含代码所示的Light ID,但是最好调用它event
以识别它是传递到Lambda函数中的值,但是您的代码随后选择将其解释为lightID
另外,您的代码不应该调用
lambda_handler
函数。AWS Lambda服务将在调用函数时执行此操作最后,您可能希望利用Python 3.x f-strings,它可以生成更漂亮的格式字符串:
相关问题 更多 >
编程相关推荐