Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring region in Node.js AWS SDK

People also ask

How do I change my AWS region SDK?

You can set the Region using the AWS_REGION environment variable. If you define this variable, the SDK for JavaScript reads it and uses it.

How do I get AWS region in node?

To get the AWS Region where your Lambda Function is running we need to access the Environment Variable AWS_REGION . To access the environment variable AWS_REGION using Node. js Lambda Function we can use process. env.

What is the default region for all SDK in AWS?

AWS clients created by using the client constructor will not automatically determine region from the environment and will, instead, use the default SDK region (USEast1).


How about changing the order of statements? Update AWS config before instantiating s3 and dd

var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();

I had the same issue "Missing region in config" and in my case it was that, unlike in the CLI or Python SDK, the Node SDK won't read from the ~\.aws\config file.

To solve this, you have three options:

  1. Configure it programmatically (hard-coded): AWS.config.update({region:'your-region'});

  2. Use an environment variable. While the CLI uses AWS_DEFAULT_REGION, the Node SDK uses AWS_REGION.

  3. Load from a JSON file using AWS.config.loadFromPath('./config.json');

JSON format:

{ 
    "accessKeyId": "akid", 
    "secretAccessKey": "secret", 
    "region": "us-east-1" 
}

If you work with AWS CLI, you probably have a default region defined in ~/.aws/config. Unfortunately AWS SDK for JavaScript does not load it by default. To load it define env var

AWS_SDK_LOAD_CONFIG=1

See https://github.com/aws/aws-sdk-js/pull/1391


You can specify the region when creating the dynamodb connection (haven't tried s3 but that should work too).

var AWS = require('aws-sdk');
var dd = new AWS.DynamoDB({'region': 'us-east-1'});

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

// assign AWS credentials here in following way:

AWS.config.update({
  accessKeyId: 'asdjsadkskdskskdk',
  secretAccessKey: 'sdsadsissdiidicdsi',
  region: 'us-east-1'
});

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();

Same error for me:

After doing a lot of trials I have settled on the below:

OPTION 1

  1. set the AWS_REGION environment variable in local system only, to us-east-1 (example)

For Linux:

export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_DEFAULT_REGION=us-east-1

For Windows
see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html

  1. now, no need to set any lambda variables for region
  2. also, no need to use in code, for example:

    • AWS.config.update(...), this is not required
    • AWS.S3(), etc., these will work without any problems. In place of S3, there can be any aws service

In a rare case if somewhere some defaults are assumed in code and you are forced to send region, then use {'region': process.env.AWS_REGION})


OPTION 2

Instead of environment variables, another way is AWS CONFIG file:

On Linux you can create below files:

~/.aws/credentials

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

~/.aws/config

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

See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html


I have gone through your code and here you are connecting to AWS services before setting the region, so i suggest you to update the region first and then connect to services or create instance of those as below -

var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
var bucketName = 'my-bucket';