Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get 'Error [ConfigError]: Missing region in config' each time I attempt AWS lambda function call from NodeJS

I'm new to AWS lambda functions and was given some sample code (below -- in the actual code I fill in the placeholders correctly, of course). But each time I call the lambda function, I get

Error [ConfigError]: Missing region in config
    at Request.VALIDATE_REGION (/Users/abc/Documents/projects/bot/node_modules/aws-sdk/lib/event_listeners.js:92:45)

Even though the region is set in aws.config.update. I'm not sure what to do to fix this. I've tried removing the call to aws.config.update, removing the reference to region, but nothing helps.

I'd also be keen to know if there's a way to load the credentials from the shared file in ~/.aws/credentials, instead of having to enter them directly here.

const aws = require('aws-sdk');

// do this only in dev, in prod you will not need to put your keys...
aws.config.update({accessKeyId: '<YOU ACCESS KEY>', secretAccessKey: '<YOUR SECRET>', region:'ap-northeast-1'});


const lambda = new aws.Lambda();

function invokeLambda(options) {
  return new Promise((resolve, reject) => {
    lambda.invoke(options, (err, data) => {
      if (err) {
        return reject(err);
      }
      return resolve(data);
    });
  });
}

const sampleCall = async () => {
  try {
    const options = {
      FunctionName: '<NAME OF THE FUNCTION YOU WANT TO INVOKE>',
      Payload: '<The JSON.stringify of the object you want to provide as parameter>',
    }
    const result = await invokeLambda(options);
    console.log(result);
  } catch (err) {
    console.error(err);
  }
};
like image 664
Cerulean Avatar asked Jul 18 '26 12:07

Cerulean


1 Answers

You can configure the region before using any services.

like this:

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

AWS services such as lambda has the ability to assume an IAM Role. Because the applications does not need to store credentials.

here is what you should do:

  • The IAM role is attached to the lambda
  • Permissions are then attached to the IAM role

Basically you can attach the same policy that your IAM user is using to the IAM role. After doing that, you can remove credentials from the code.

Reference: https://aws.amazon.com/blogs/security/how-to-create-an-aws-iam-policy-to-grant-aws-lambda-access-to-an-amazon-dynamodb-table/

like image 107
Arun Kamalanathan Avatar answered Jul 21 '26 02:07

Arun Kamalanathan