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

SDK throws "Region is missing" error even if region is defined in config #3469

Closed
trivikr opened this issue Mar 24, 2022 · 6 comments
Closed
Assignees
Labels
guidance General information and guidance, answers to FAQs, or recommended best practices/resources.

Comments

@trivikr
Copy link
Member

trivikr commented Mar 24, 2022

Describe the bug

SDK throws "Region is missing" error even if region is defined in config

Your environment

SDK version number

@aws-sdk/client-dynamodb@3.55.0

Is the issue in the browser/Node.js/ReactNative?

Node.js

Details of the browser/Node.js/ReactNative version

v16.14.0

Steps to reproduce

Configure AWS profile as follows, and verify that region is set in AWS Config

$ aws --version
aws-cli/2.4.27 Python/3.8.13 Linux/5.4.181-109.354.amzn2int.x86_64 source/x86_64.amzn.2 prompt/off

$ aws configure --profile test
AWS Access Key ID [None]: HIDDEN
AWS Secret Access Key [None]: HIDDEN
Default region name [None]: us-west-2
Default output format [None]:

$ cat ~/.aws/config
[profile test]
region = us-west-2

Test code to run:

// testRegion.mjs
import { DynamoDB } from "@aws-sdk/client-dynamodb"; // v3.55.0

const client = new DynamoDB({ profile: "test" });
console.log(await client.config.region());

Observed behavior

/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/config-resolver/dist-cjs/regionConfig/config.js:10
        throw new Error("Region is missing");
              ^

Error: Region is missing
    at default (/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/config-resolver/dist-cjs/regionConfig/config.js:10:15)
    at /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/node-config-provider/dist-cjs/fromStatic.js:6:83
    at /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/property-provider/dist-cjs/chain.js:11:28
    at async coalesceProvider (/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/property-provider/dist-cjs/memoize.js:14:24)
    at async /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/property-provider/dist-cjs/memoize.js:26:28
    at async Object.region (/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/config-resolver/dist-cjs/regionConfig/resolveRegionConfig.js:17:36)
    at async file:///local/home/trivikr/workspace/temp/testRegion.mjs:4:13

Expected behavior

Prints us-west-2

@trivikr trivikr added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 24, 2022
@trivikr
Copy link
Member Author

trivikr commented Mar 24, 2022

This happens because profile passed in the client configuration is not passed to function fromSharedConfigFiles

{ preferredFile = "config", ...init }: SharedConfigInit = {}
): Provider<T> =>
async () => {
const { loadedConfig = loadSharedConfigFiles(init), profile = process.env[ENV_PROFILE] || DEFAULT_PROFILE } = init;

@trivikr
Copy link
Member Author

trivikr commented Mar 24, 2022

The profile from client configuration needs to be passed when calling loadConfig

export const loadConfig = <T = string>(
{ environmentVariableSelector, configFileSelector, default: defaultValue }: LoadedConfigSelectors<T>,
configuration: LocalConfigOptions = {}
): Provider<T> =>
memoize(
chain(
fromEnv(environmentVariableSelector),
fromSharedConfigFiles(configFileSelector, configuration),
fromStatic(defaultValue)
)
);

region: config?.region ?? loadNodeConfig(NODE_REGION_CONFIG_OPTIONS, NODE_REGION_CONFIG_FILE_OPTIONS),

@trivikr
Copy link
Member Author

trivikr commented Mar 24, 2022

Proposed fix: read profile from configuration and pass it to loadNodeConfig call.

Example diff in client-dynamodb for region configuration:

$ git diff  
diff --git a/clients/client-dynamodb/src/DynamoDBClient.ts b/clients/client-dynamodb/src/DynamoDBClient.ts
index a3241382b9..30196d4e2b 100644
--- a/clients/client-dynamodb/src/DynamoDBClient.ts
+++ b/clients/client-dynamodb/src/DynamoDBClient.ts
@@ -399,6 +399,11 @@ export interface ClientDefaults extends Partial<__SmithyResolvedConfiguration<__
    * The {@link DefaultsMode} that will be used to determine how certain default configuration options are resolved in the SDK.
    */
   defaultsMode?: DefaultsMode | Provider<DefaultsMode>;
+
+  /**
+   * Name of the AWS CLI profile to use.
+   */
+  profile: string | undefined;
 }
 
 type DynamoDBClientConfigType = Partial<__SmithyConfiguration<__HttpHandlerOptions>> &
diff --git a/clients/client-dynamodb/src/runtimeConfig.ts b/clients/client-dynamodb/src/runtimeConfig.ts
index bd9889770e..fb2795e60b 100644
--- a/clients/client-dynamodb/src/runtimeConfig.ts
+++ b/clients/client-dynamodb/src/runtimeConfig.ts
@@ -31,6 +31,7 @@ import { resolveDefaultsModeConfig } from "@aws-sdk/util-defaults-mode-node";
  * @internal
  */
 export const getRuntimeConfig = (config: DynamoDBClientConfig) => {
+  const { profile } = config;
   const defaultsMode = resolveDefaultsModeConfig(config);
   const defaultConfigProvider = () => defaultsMode().then(loadConfigsForDefaultMode);
   const clientSharedValues = getSharedRuntimeConfig(config);
@@ -50,7 +51,8 @@ export const getRuntimeConfig = (config: DynamoDBClientConfig) => {
     endpointDiscoveryEnabledProvider:
       config?.endpointDiscoveryEnabledProvider ?? loadNodeConfig(NODE_ENDPOINT_DISCOVERY_CONFIG_OPTIONS),
     maxAttempts: config?.maxAttempts ?? loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS),
-    region: config?.region ?? loadNodeConfig(NODE_REGION_CONFIG_OPTIONS, NODE_REGION_CONFIG_FILE_OPTIONS),
+    region:
+      config?.region ?? loadNodeConfig(NODE_REGION_CONFIG_OPTIONS, { profile, ...NODE_REGION_CONFIG_FILE_OPTIONS }),
     requestHandler: config?.requestHandler ?? new RequestHandler(defaultConfigProvider),
     retryMode:
       config?.retryMode ??

@AllanZhengYP
Copy link
Contributor

AllanZhengYP commented Mar 24, 2022

Adding profile in the client config but only control the region resolution will add too much confusion for users. Credentials and other config also relies on the profile.

In v2 region resolution, region is only resolved from the default profile or the AWS_PROFILE environmental variable. I'm leaning towards keeping the behavior.

@trivikr trivikr added guidance General information and guidance, answers to FAQs, or recommended best practices/resources. and removed bug This issue is a bug. labels Mar 24, 2022
@trivikr
Copy link
Member Author

trivikr commented Mar 24, 2022

Closing this issue as:

If you want to set profile, use environment variable AWS_PROFILE

@github-actions
Copy link

github-actions bot commented Apr 8, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
guidance General information and guidance, answers to FAQs, or recommended best practices/resources.
Projects
None yet
Development

No branches or pull requests

2 participants