17

change log says Load config from ~/.aws/config if AWS_SDK_LOAD_CONFIG is set. Couldn't find any examples or documentation regarding how to load the config. Any help!

sreenivas
  • 2,067
  • 1
  • 19
  • 29
  • 2
    If you set `AWS_SDK_LOAD_CONFIG` to a truthy value, the SDK will automatically load the `~/.aws/config` file in the same way the AWS CLI would. What kind of example are you looking for? – giaour Jun 08 '17 at 15:44
  • In nodejs i can set the env variable to truthy value with process.env.AWS_SDK_LOAD_CONFIG = true; How can I retrieve the region value in the config ? Mind sharing some code ? – sreenivas Jun 09 '17 at 10:43

3 Answers3

26

There is a little bit of magic in how aws-sdk loads the config

either set the env variable

export AWS_SDK_LOAD_CONFIG="true"

or before loading the aws-sdk set

process.env.AWS_SDK_LOAD_CONFIG = true; 

Then load the aws module;

var AWS = require('aws-sdk');

You can access the region directly by

AWS.config.region
sreenivas
  • 2,067
  • 1
  • 19
  • 29
2

The answer of sreenivas is correct. It also seems to be the only way to do this without writing a custom function.

I've traced it back in the source code and the the way it loads ~/.aws/config is similar to this pseudocode:

if process.env.AWS_SDK_LOAD_CONFIG:
  return load('~/.aws/credentials').overwrite('~/.aws/config')
else:
  return load('~/.aws/credentials')

This also means you can set the environment variable after require('aws-sdk'), as long as you do it before new SharedIniFileCredentials({..}) or credentials.refresh(). Beware that credentials.get() will not work until the sessiontoken has expired.

0

There is documentation for this:

I would recommend installing the awscli tool to set this up and then run aws configure in your terminal. By default, anything you're running on your local host will assume the credentials listed in your config file unless specified to assume a different profile.

Example from the first link:

AWS.config.credentials = new AWS.SharedIniFileCredentials( { profile: 'work-account' } );

If you are using the CLI to run your script:

AWS_PROFILE=work-account node script.js

If you are using just the CLI tool and not JavaScript:

aws s3 ls --profile work-account

Update:

config and credentials are made and referenced at the same time. When you run aws configure it makes two files. The credentials file containing AccessKey and SecretKey - and the config file containing the response type and region. It is NOT necessary to explicitly define or reference the config file.

iSkore
  • 7,394
  • 3
  • 34
  • 59
  • 4
    Am looking for 'config' not 'credentials'. – sreenivas Jun 09 '17 at 10:45
  • They are made at the same time. When you run `aws configure` it makes two files. The `credentials` file containing AccessKey and SecretKey - and the `config` file containing the response type and region. It is NOT necessary to explicitly define or reference the `config` file – iSkore Jun 09 '17 at 11:09