Skip to content

Commit

Permalink
feat(aws-lambda) add static response template to optionnaly match
Browse files Browse the repository at this point in the history
 
Allow lambda's response to set statusCode and add headers to Kong's output response.

If Content-Type is "application/json" and response from AWS Lambda match the following JSON :
{
  status_code: <my_status_code>,
  body: <my_json_encoded_body>,
  headers: <my_headers>
}
Then the response from Kong will set the statusCode value to <my_status_code> and the body to <my_json_encoded_body> and add <my_headers> to response

From #3208
  • Loading branch information
romdsj committed Aug 28, 2018
1 parent 4043aa7 commit f899932
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions kong/plugins/aws-lambda/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ local ngx_req_get_uri_args = ngx.req.get_uri_args
local ngx_req_get_headers = ngx.req.get_headers
local ngx_encode_base64 = ngx.encode_base64


local new_tab
do
local ok
Expand Down Expand Up @@ -179,28 +178,50 @@ function AWSLambdaHandler:access(conf)
end

local content = res:read_body()

local headers = res.headers

local ok, err = client:set_keepalive(conf.keepalive)
if not ok then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end

local status
local status = res.status
local body = content
if conf.unhandled_status
and headers["X-Amz-Function-Error"] == "Unhandled"
and headers["X-Amz-Function-Error"] == "Unhandled"
then
status = conf.unhandled_status

else
status = res.status
if headers["Content-Type"] then
local content_type = headers["Content-Type"]
if content_type:find("application/json", nil, true) then
local params = cjson.decode(content)
local statusCode = params.status_code
local resource = params.body

if statusCode ~= nil then
headers['X-lambda-original-status'] = res.status
status = statusCode
end
if resource ~= nil then
-- As we're changing the body size, we can't set this header.
headers['Content-Length'] = #resource
body = resource
end
for k, v in pairs(params.headers) do
headers[k] = v
end
end
end
end

local ctx = ngx.ctx
if ctx.delay_response and not ctx.delayed_response then
ctx.delayed_response = {
status_code = status,
content = content,
content = body,
headers = headers,
}

Expand All @@ -209,7 +230,7 @@ function AWSLambdaHandler:access(conf)
return
end

return send(status, content, headers)
return send(status, body, headers)
end


Expand Down

0 comments on commit f899932

Please sign in to comment.