如何将HTTP API或REST API与Lambda(不更新的能力)提供给HTTP API或REST API?
因此,我具有以下代码的lambda功能,该代码由API触发(AWS API网关)。
import json
def lambda_handler(event, context):
testString = "foo"
if "f" in testString:
return {
"statusCode": 200,
"body": json.dumps(testString)
}
我用HTTP API和REST API对其进行了测试。由于条件返回true
,在这两种情况下,输出都是“ foo”
。但是,如果testString
突然更改为“ Goo”
,并且条件返回false
,会发生什么?我希望输出保持原样(未更新),因此它仍然是“ foo”
。但是发生这种情况时,HTTP API输出null
,而REST API输出{“ message”:“ Internal Server Error”}
。
也许我只需要在下面的代码中弄清楚缺少的作品:
import json
def lambda_handler(event, context):
testString = "goo"
if "f" in testString:
return {
"statusCode": 200,
"body": json.dumps(testString)
}
else:
#missing piece: make output not change
这可能是第一次,我尝试了“创建” APIS TBH。我想念什么?
So I have a Lambda Function with the code below, which is triggered by an API (AWS API Gateway).
import json
def lambda_handler(event, context):
testString = "foo"
if "f" in testString:
return {
"statusCode": 200,
"body": json.dumps(testString)
}
I tested it with both a HTTP API and a REST API. Because the condition returns True
, in both cases the output is "foo"
as it should be. But what happens if testString
suddenly changes to "goo"
, and the condition returns False
? I would like the output to remain as it previously was (not update), so it remains "foo"
. But when this happens, the HTTP API outputs null
, and the REST API outputs {"message": "Internal server error"}
.
Maybe I just need to figure out the missing piece in the code below:
import json
def lambda_handler(event, context):
testString = "goo"
if "f" in testString:
return {
"statusCode": 200,
"body": json.dumps(testString)
}
else:
#missing piece: make output not change
This is probably the first time, I have tried "creating" APIs tbh. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能仅使用lambda函数来完成此操作。您必须存储以前的输出外部,例如
dynamodb
。然后,您的功能将始终能够查找最后一个正确的结果,然后将其返回,而不是随机错误消息或不正确的答案。You can't do this with just only a lambda function. You have to store your previous outputs externally, e.g. in a
DynamoDB
. Then your function will always be able to look up the last correct result and return it instead some random error message or incorrect answer.