从node.js app调用aws lambda函数会出现AccessDeniedException错误

时间:2018-04-25 07:11:27

标签: node.js amazon-web-services aws-lambda aws-sdk

我试图从我的node.js应用程序调用Lambda函数。我有accesskey&我在下面使用的secretkey是我编写的示例代码,如果我能够从node.js express app命中AWS lambda函数。

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!');
});

知道AWS的当前配置有什么问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

不是node.js,而是从替换以下代码行开始

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

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