The following call goes through and the third party service integrated into the lambda works as expected, but the call returns an error along with a 200.
For your reference, we use Plaid to handle our financial transactions.
error_template = {
"text/xml": '''#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))\n'''
'''$errorMessageObj.message'''
}
500_integation = apigateway.IntegrationResponse(
status_code="500",
content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
response_templates=error_template,
selection_pattern='''.*500.*''' # NOTE: Lambda integrations search against "errorMessage" with this regex
)
上面的映射期望lambda抛出例外:
except Exception as e:
raise SomethingWentWrong(json.dumps({
"errorType": "InternalServiceError",
"httpStatus": 500,
"message": "Your message, again stringified if a json-like object!"
}))
API gateway, when coupled to a Lambda - always returns status 200 by default if the Lambda successfully responded. And note: This does not mean the lambda was successful just that it properly sent a response and an exception did not get raised above the lambda handler
The status 200 is basically:
Api Hey. Lambda. Here is a request.
Lambda Cool. Ooooh. I messed up. But Im telling you about it with a return!
Api Cool, cool. You responded. Status 200
If you want other status you have to make use of Method and Integration Response templates, mappings.
If you have just a lambda integration (not a proxy!) on the back end between the Api and Lambda, then you should be able to just use some mapping: for example (python, and probably not that efficient, this is old code):
the above CDK python code is expecting the lambda to return a json object like so:
return {
"statusCode": 200,
"body": "json like object stringified"
}
however, if the API Gateway -> Lambda is set as "Lambda Proxy" then the only way to do this actually having the lambda raise an Exception past the handler, and letting the Lambda backend generate the response to ApiGateway.
Normally this will just result in a 502: Internal Service Error response from ApiGateway, but again - using method response and integration response mapping - you can set it directly. (do note the code I pulled this from is an XML response, which probably wont apply - but you can see the general idea)
error_template = {
"text/xml": '''#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))\n'''
'''$errorMessageObj.message'''
}
500_integation = apigateway.IntegrationResponse(
status_code="500",
content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
response_templates=error_template,
selection_pattern='''.*500.*''' # NOTE: Lambda integrations search against "errorMessage" with this regex
)
the above mapping is expecting the lambda to throw an exception:
except Exception as e:
raise SomethingWentWrong(json.dumps({
"errorType": "InternalServiceError",
"httpStatus": 500,
"message": "Your message, again stringified if a json-like object!"
}))
which causes the aws Lambda service backend to send a response to the api gateway that looks (something - its been a bit and im a bit hazy) like this:
But TL:DR -> Api Gateway with a Lambda defaults to 200 response if the lambda successfully returns, only showing another status code if you specifically map it - and with Proxy Integrations, only if the lambda raised an exception
It is entirely based on the resource connection between API Gateway and its services + the mappings that generate this response.
发布评论
评论(1)
API Gateway,如果lambda成功响应,则默认情况下,默认情况下,默认情况下,将其耦合到lambda - 总是。 和注意:这并不意味着lambda是成功的,只是它正确发送了响应,并且例外没有被提高到lambda处理程序之上
状态200基本上是:
< em> api 嘿。 Lambda。这是一个请求。
lambda 很酷。哦。我搞砸了。但是我要回报告诉您这件事!
api 很酷,很酷。你回应了。状态200
如果您想要其他状态,则必须使用方法和集成响应模板,映射。
如果您仅在API和Lambda之间的后端具有Lambda集成(不是代理!),那么您应该只能使用一些映射:例如(Python,可能不是那么有效,这是旧代码)) :
上面的CDK Python代码期望lambda这样的JSON对象:
}
但是,如果API网关 - &gt; Lambda被视为“ Lambda Proxy”,然后将Lambda带到处理程序的例外,并让Lambda后端产生对Apigateway的响应。
通常,这只会导致
502:内部服务错误
apigateway的响应,但是再次使用方法响应和集成响应映射 - 您可以直接设置它。 (请注意,我从中提取的代码是XML响应,它可能不会适用 - 但是您可以看到一般的想法)上面的映射期望lambda抛出例外:
这会导致AWS Lambda服务后端发送响应对于API网关的外观(某些东西 - 有点朦胧,有点朦胧):
请注意,当它到达API时,实际错误消息将被串起。
您可以在此处获取有关映射的更多信息:
它完全基于API Gateway和Api Gateway和API的资源连接它的服务 +产生此响应的映射。
API gateway, when coupled to a Lambda - always returns status 200 by default if the Lambda successfully responded. And note: This does not mean the lambda was successful just that it properly sent a response and an exception did not get raised above the lambda handler
The status 200 is basically:
Api Hey. Lambda. Here is a request.
Lambda Cool. Ooooh. I messed up. But Im telling you about it with a return!
Api Cool, cool. You responded. Status 200
If you want other status you have to make use of Method and Integration Response templates, mappings.
If you have just a lambda integration (not a proxy!) on the back end between the Api and Lambda, then you should be able to just use some mapping: for example (python, and probably not that efficient, this is old code):
the above CDK python code is expecting the lambda to return a json object like so:
}
however, if the API Gateway -> Lambda is set as "Lambda Proxy" then the only way to do this actually having the lambda raise an Exception past the handler, and letting the Lambda backend generate the response to ApiGateway.
Normally this will just result in a
502: Internal Service Error
response from ApiGateway, but again - using method response and integration response mapping - you can set it directly. (do note the code I pulled this from is an XML response, which probably wont apply - but you can see the general idea)the above mapping is expecting the lambda to throw an exception:
which causes the aws Lambda service backend to send a response to the api gateway that looks (something - its been a bit and im a bit hazy) like this:
notice the actual error message is stringified when it reaches the API.
you can get more information about mappings here: https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html
But TL:DR -> Api Gateway with a Lambda defaults to 200 response if the lambda successfully returns, only showing another status code if you specifically map it - and with Proxy Integrations, only if the lambda raised an exception
It is entirely based on the resource connection between API Gateway and its services + the mappings that generate this response.