Skip to content

Commit

Permalink
fix(aws-lambda): lazily initialize aws library
Browse files Browse the repository at this point in the history
### Summary

Lazily initializes AWS library on a first use, to remove startup delay caused
by AWS metadata discovery.

Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
  • Loading branch information
bungle committed Sep 19, 2023
1 parent 84ef30c commit 9f4fa97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
24 changes: 0 additions & 24 deletions kong/db/dao/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,29 +355,5 @@ function Plugins:get_handlers()
return list
end

function Plugins:execute_plugin_init()
local handlers, err = self:get_handlers()
if not handlers then
return nil, err
end

local errors

for _, handler in ipairs(handlers) do
if implements(handler.handler, "init") then
local ok, err = pcall(handler.handler.init, handler.handler)
if not ok then
errors = errors or {}
errors[#errors + 1] = "on plugin '" .. handler.name .. "': " .. tostring(err)
end
end
end

if errors then
return nil, "error executing plugin init: " .. table.concat(errors, "; ")
end

return true
end

return Plugins
2 changes: 0 additions & 2 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,6 @@ function Kong.init()
-- Load plugins as late as possible so that everything is set up
assert(db.plugins:load_plugin_schemas(config.loaded_plugins))

assert(db.plugins:execute_plugin_init())

if is_stream_module then
stream_api.load_handlers()
end
Expand Down
17 changes: 12 additions & 5 deletions kong/plugins/aws-lambda/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local ngx_update_time = ngx.update_time
local kong = kong
local meta = require "kong.meta"
local constants = require "kong.constants"
local aws_config = require "resty.aws.config" -- reads environment variables, thus specified here
local VIA_HEADER = constants.HEADERS.VIA
local VIA_HEADER_VALUE = meta._NAME .. "/" .. meta._VERSION

Expand All @@ -30,18 +31,24 @@ local function get_now()
end


local function initialize()
AWS_GLOBAL_CONFIG = aws_config.global
AWS = aws()
initialize = nil
end


local AWSLambdaHandler = {
PRIORITY = 750,
VERSION = meta.version
}

function AWSLambdaHandler:init()
AWS_GLOBAL_CONFIG = require("resty.aws.config").global
AWS = aws()
end


function AWSLambdaHandler:access(conf)
if initialize then
initialize()
end

-- The region in plugin configuraion has higher priority
-- than the one in environment variable
local region = conf.aws_region or AWS_REGION
Expand Down

0 comments on commit 9f4fa97

Please sign in to comment.