Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(*): don't invoke region detection code on the module toplevel #81

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ read that setting, hence you have to set it manually, see [the docs](https://tie
### Global settings

This library depends on global settings. Especially the core services for authentication
and metadata. Many of those can (also) be specified as environment variables.
and metadata. Many of those can (also) be specified as environment variables. Environment
variables can only be accessed during the OpenResty `init` phase. Thus, to ensure correct
configuration from environment variables, the `resty.aws.config` module must be required on
the top-level of the module using this library:

Hence it is recommended to populate the global configuration object at application start
in the OpenResty `init` phase. Simply add the following line;

```
local _ = require("resty.aws.config").global
```Lua
local aws_config = require("resty.aws.config")
```

This ensures the environment variables can still be read (in the `init` phase). And
the auto-detection of the AWS region will execute.
The `.global` property of the `aws_config` variable can then be used as the global
configuration. Note that when `.global` is first accessed, automatic region detection
through the AWS metadata service is performed. Thus, it is not advisable to access
it on the module level unless to avoid startup delays in non-AWS environment, caused by
the requests to the metadata service timing out.

---

Expand Down Expand Up @@ -166,6 +169,11 @@ Release process:
1. upload using: `VERSION=x.y.z APIKEY=abc... make upload`
1. test installing the rock from LuaRocks

### 1.3.3 (13-Sep-2023)

- fix: don't invoke region detection code on the module toplevel and advise against trying to.
[81](https://github.com/Kong/lua-resty-aws/pull/81)

### 1.3.2 (13-Sep-2023)

- fix: unsigned request should support network related config option
Expand Down
28 changes: 11 additions & 17 deletions src/resty/aws/utils.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--- AWS utility module.
--
-- Provides methods for detecting the AWS Region, as well as fetching metadata.
Expand All @@ -5,14 +5,8 @@
local semaphore = require "ngx.semaphore"
local http = require "resty.luasocket.http"
local json = require "cjson"
local global_config = require("resty.aws.config").global

-- get the Env vars here, they have been stored in the config module
local AWS_REGION = global_config.AWS_REGION
local AWS_DEFAULT_REGION = global_config.AWS_DEFAULT_REGION
local AWS_EC2_METADATA_DISABLED = global_config.AWS_EC2_METADATA_DISABLED
local ECS_CONTAINER_METADATA_URI_V4 = global_config.ECS_CONTAINER_METADATA_URI_V4
local ECS_CONTAINER_METADATA_URI_V3 = global_config.ECS_CONTAINER_METADATA_URI
local aws_config = require("resty.aws.config")

local ECS_CONTAINER_METADATA_URI_V2 = "http://169.254.170.2/v2/"
local IDMS_URI = "http://169.254.169.254"
local METADATA_TIMEOUTS = 5000 -- in milliseconds
Expand Down Expand Up @@ -112,12 +106,12 @@
local url
local version = version and version:upper() or "V4"
if version == "V4" then
url = ECS_CONTAINER_METADATA_URI_V4
url = aws_config.global.ECS_CONTAINER_METADATA_URI_V4
if not url then
return nil, "ECS metadata url V4 not found in env var ECS_CONTAINER_METADATA_URI_V4"
end
elseif version == "V3" then
url = ECS_CONTAINER_METADATA_URI_V3
url = aws_config.global.ECS_CONTAINER_METADATA_URI_V3
if not url then
return nil, "ECS metadata url V3 not found in env var ECS_CONTAINER_METADATA_URI"
end
Expand Down Expand Up @@ -172,23 +166,23 @@
end

local function detect_region()
if AWS_REGION then
if aws_config.global.AWS_REGION then
ngx.log(ngx.DEBUG, "detecting AWS region from AWS_REGION env variable")
set_region(AWS_REGION)
set_region(aws_config.global.AWS_REGION)
return true
else
ngx.log(ngx.DEBUG, "no AWS_REGION env variable")
end

if AWS_DEFAULT_REGION then
if aws_config.global.AWS_DEFAULT_REGION then
ngx.log(ngx.DEBUG, "detecting AWS region from AWS_DEFAULT_REGION env variable")
set_region(AWS_DEFAULT_REGION)
set_region(aws_config.global.AWS_DEFAULT_REGION)
return true
else
ngx.log(ngx.DEBUG, "no AWS_DEFAULT_REGION env variable")
end

if ECS_CONTAINER_METADATA_URI_V4 then
if aws_config.global.ECS_CONTAINER_METADATA_URI_V4 then
ngx.log(ngx.DEBUG, "detecting AWS region from ECS_CONTAINER_METADATA_URI_V4 env variable")
local metadata, err = Utils.getECSTaskMetadata("/task", "V4")
if not metadata then
Expand All @@ -201,7 +195,7 @@
ngx.log(ngx.DEBUG, "no ECS_CONTAINER_METADATA_URI_V4 env variable")
end

if ECS_CONTAINER_METADATA_URI_V3 then
if aws_config.global.ECS_CONTAINER_METADATA_URI_V3 then
ngx.log(ngx.DEBUG, "detecting AWS region from ECS_CONTAINER_METADATA_URI env variable")
local metadata, err = Utils.getECSTaskMetadata("/task", "V3")
if not metadata then
Expand All @@ -214,7 +208,7 @@
ngx.log(ngx.DEBUG, "no ECS_CONTAINER_METADATA_URI env variable")
end

if AWS_EC2_METADATA_DISABLED then
if aws_config.global.AWS_EC2_METADATA_DISABLED then
ngx.log(ngx.DEBUG, "AWS_EC2_METADATA_DISABLED is set, skipping region detection from IDMSv2 metadata")
else
ngx.log(ngx.DEBUG, "detecting AWS region from IDMSv2 metadata")
Expand Down
Loading