Skip to content

Commit

Permalink
fix(*): don't invoke region detection code on the module toplevel and…
Browse files Browse the repository at this point in the history
… advise against trying to (#81)
  • Loading branch information
hanshuebner authored Sep 13, 2023
1 parent 3809e23 commit 5c0587f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
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
Expand Up @@ -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 @@ function Utils.getECSTaskMetadata(subpath, version)
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 @@ do -- getCurrentRegion
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 @@ do -- getCurrentRegion
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 @@ do -- getCurrentRegion
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

0 comments on commit 5c0587f

Please sign in to comment.