Samir
Samir

Reputation: 691

calling aws lambda function from node.js app gives error of AccessDeniedException

I am trying to invoke Lambda function from my node.js application. I have accesskey & secretkey which I am using below is sample code which I have written to just test if I am able to hit AWS lambda function from node.js express app.

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

var app = express();
app.get('/', function (req, res) {
  res.send('Hello World!');
});

const invokeLambda = (lambda, params) => new Promise((resolve, reject) => {
  lambda.invoke(params, (error, data) => {
    if (error) {
      reject(error);
    } else {
      resolve(data);
    }
  });
});

const main = async () => {
  AWS.config.update({ 
    accessKeyId: 'XXXXX', 
    secretAccessKey: 'YYYYY', 
    region: 'eu-west-1',
  });

  const lambda = new AWS.Lambda();

  const params = {
    FunctionName: 'myLambdaFunction', 
    Payload: JSON.stringify({
      'x': 1, 
      'y': 2,
      'z': 3,
    }),
  };

  const result = await invokeLambda(lambda, params);

  console.log('Success!');
  console.log(result);
};

main().catch(error => console.error(error));

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Any idea what is wrong in current configuration of AWS?

Thanks in Advance!

Upvotes: 0

Views: 163

Answers (1)

S.N
S.N

Reputation: 5140

Not into node.js but I would start by replacing following line of code

const main = async () => {
  AWS.config.update({ 
    accessKeyId: 'XXXXX', 
    secretAccessKey: 'YYYYY', 
    region: 'eu-west-1',
  });

with

AWS.config.update({accessKeyId: 'XXXXX', secretAccessKey: 'YYYYY'});

Upvotes: 0

Related Questions