Search code examples
javascriptamazon-web-servicesregionaws-sdk-js

Configuration Error: Missing Region in Config (AWS)


I am getting a configuration error when I run my node application. The error is:

{ ConfigError: Missing region in config
at Request.VALIDATE_REGION (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:12027:47)
at Request.callListeners (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11804:22)
at callNextListener (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11794:14)
at C:\Users\chris\Documents\AWS API TEST\myapp\node_modules\aws-
sdk\dist\aws-sdk-react-native.js:12021:11
at finish (C:\Users\chris\Documents\AWS API TEST\myapp\node_modules\aws-
sdk\dist\aws-sdk-react-native.js:10842:9)
at Config.getCredentials (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:10887:9)
at Request.VALIDATE_CREDENTIALS (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:12016:28)
at Request.callListeners (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11800:20)
at Request.emit (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11776:12)
at Request.emit (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:13792:16)
message: 'Missing region in config',
code: 'ConfigError',

The code I have is:

//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({IdentityPoolId:'us-west-2a:"Identity Pool ID"'});
var myConfig = new AWS.Config({
          credentials: myCredentials, region: 'us-west-2a'
});

This is running on an express server using ejs and node. I am using the Amazon JavaScript SDK.

I attempted the solution here:

AWSCognito Missing region in config error

This did not help.

EDIT 1:

As requested Below, I have attacheed full code:

var express = require('express');
var AWS = require('aws-sdk');
var EC2 = require('aws-ec2')('access key', 'secret');
var scale = require('aws-scale');
var router = express.Router();

//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId:'us-west-2:b0efe3c7-e4c9-420f-836b-6d776e2a9271'
}); 

var myConfig = new AWS.Config({
  credentials: myCredentials, region: 'us-west-2'
});

AWS.config = myConfig

var minInst = 1;
var maxInst = 3;

var ec2 = new AWS.EC2();
//Set up parameters for EC2 Instances:
var params = {
  ImageId: 'ami-6e1a0117',
  MaxCount: minInst,
  MinCount: maxInst, 
  InstanceInitiatedShutdownBehavior: 'terminate',
  InstanceType: 't2.micro',
  Monitoring: {
    Enabled: true 
  },
  NetworkInterfaces: [{
    AssociatePublicIpAddress: true,
    DeleteOnTermination: true,
  }],
  Placement: {
    AvailabilityZone: 'us-west-2',
  },
  SecurityGroupIds: [
    'sg-b0307ccd',
  ],
  SecurityGroups: [
    'CAB432Assignment2SG',
  ],

};
ec2.runInstances(params, function(err, data) {
  if (err){
    console.log(err, err.stack); //An error occurred
  }
  else{
    console.log(data); //Successful Response
  }
});

Solution

  • The correct code of Oregon region is us-west-2.You have set it as us-west-2a in two places.

    While mentioning identity pool id,correct the code as below and try:

    AWS.config.update({region:'us-west-2'});
    var myCredentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId:'us-west-2:"Identity Pool ID"'
    });
    

    If you are not passing the region for the API CognitoIdentityCredentials, then it will pull the data from AWS.config.

    Also, In your code, while initializing AWS.CONFIG with region name, You have used us-west-2a. Correct it, if you are going to use it instead of AWS.config.update.

    var myConfig = new AWS.Config({
        credentials: myCredentials, region: 'us-west-2'
    });
    

    Update

    I have identified another issue. The issue is that, you are initializing the AWS.config with var myConfig = new AWS.Config() but you are NOT updating the AWS.config class back with it. The missing code is : AWS.config = myConfig.

    As you are not updating it back and also you are not updating the existing default class using AWS.config.update({region:'us-west-2'}); as well, it throws the Missing region in config error.

    Correct Code snippet

    var AWS = require('aws-sdk');
    var myCredentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId:'us-west-2:identity-pool-id'
    }); 
    var myConfig = new AWS.Config({
        credentials: myCredentials, region: 'us-west-2'
    });
    AWS.config = myConfig