1

I have installed AWS-CLI in my local machine. I have created a configuration profile. But I don't how to get the region from the profile from my code .

Code

var credentials = new AWS.SharedIniFileCredentials({ profile: node_env });
console.log(credentials);

I can get access ID but I couldn't get the region. Is there is any other way to get the region ?

0

1 Answer 1

1

When you set credentials using aws configure, it creates two files

  • ~/.aws/config
  • ~/.aws/credentials

region and output information save under ~/.aws/config and access_key and secret_key store under ~/.aws/credentials.

To get using aws-cli

aws configure get region --profile test

It will return the region for profile test.

But in case of Nodejs it a little bit different, it does not set region and you get null region when you print the credentials.

The SDK for JavaScript doesn't select a Region by default. However, you can set the Region using an environment variable, a shared config file, or the global configuration object.

So you have two option.

Using a Shared Config File

Much like the shared credentials file lets you store credentials for use by the SDK, you can keep your Region and other configuration settings in a shared file named config that is used by SDKs. If the AWS_SDK_LOAD_CONFIG environment variable has been set to a truthy value, the SDK for JavaScript automatically searches for a config file when it loads. Where you save the config file depends on your operating system:

Linux, macOS, or Unix users: ~/.aws/config

Windows users: C:\Users\USER_NAME\.aws\config

If you don't already have a shared config file, you can create one in the designated directory. In the following example, the config file sets both the Region and the output format.

[default]
   region=us-west-2
   output=json

setting-region vs loading-node-credentials-shared

Order of Precedence for Setting the Region

  • The order of precedence for Region setting is as follows:

  • If a Region is passed to a client class constructor, that Region is used. If not, then...

  • If a Region is set on the global configuration object, that Region is used. If not, then...

  • If the AWS_REGION environment variable is a truthy value, that Region is used. If not, then...

  • If the AMAZON_REGION environment variable is a truthy value, that Region is used. If not, then...

  • If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value and the shared credentials file (~/.aws/credentials or the path indicated by AWS_SHARED_CREDENTIALS_FILE) contains a Region for the configured profile, that Region is used. If not, then...

  • If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value and the config file (~/.aws/config or the path indicated by AWS_CONFIG_FILE) contains a Region for the configured profile, that Region is used.

Or you can read ~/.aws/config as a file, not as AWS config in nodejs with SharedIniFileCredentials approach.

1
  • thank you, best detailed answer on this that I've seen. It is now referenced in all my source code so that someone can understand why I have passed all these environment variables in via docker-compose
    – Paul S
    Commented Jan 26, 2020 at 1:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.