【问题标题】:AWS SNS not working from lambda, but working locallyAWS SNS 不能从 lambda 工作,但可以在本地工作
【发布时间】:2018-10-23 17:51:09
【问题描述】:

我有一个自己无法解决的问题。我的 lambda 函数在本地调用时按预期工作,但从 AWS Lambda 调用时它不发送文本消息。它也不会记录任何错误。

这是我的代码,我只为私人内容加了星标:

import request from 'request';
import AWS from "aws-sdk";

const options = {***};
const sentAlert = async msg => {
  const sns = new AWS.SNS();
  await sns.publish({
    Message: msg,
    PhoneNumber: '***',
    MessageAttributes: {
      'AWS.SNS.SMS.SenderID': {
        'DataType': 'String',
        'StringValue': '***'   
      }
    }
  }, function (err, data) {
  if (err) {
    console.log(err.stack);
    return;
  }
  });
  console.log('sms sent');
};

export const getAlert = async (event, context, callback) => {
  request(options, (err, res, body) => {
    if (err) { return console.log('error: ', err); }
    if (body.length === 0 ) { return }
    console.log(`***`);
    const optionsId = {*** };
    request(optionsId, (err, res, body) => { 
      const msg = body.current.indexes[0].description;
      console.log('msg: ', msg);
      sentAlert(msg);
    });
  });
};

我使用 serverless invoke local --function getSmogAlert 在本地对其进行测试,它按预期工作,我从 AWS 收到短信,但是当我使用 serverless invoke --function getSmogAlert 调用它时 - 它返回 null 并且不发送任何文本消息。 我在使用 Nexmo 时遇到过类似的问题,并认为 AWS.SNS 可能会帮助我,但不是。

有什么帮助吗?

【问题讨论】:

  • 请检查 Lambda 函数的 AWS CloudWatch 日志并包含函数调用的任何日志输出。
  • 只有START RequestId: 824ea951-d6e8-11e8-a428-77d70245b74d Version: $LATEST END RequestId: 824ea951-d6e8-11e8-a428-77d70245b74d REPORT RequestId: 824ea951-d6e8-11e8-a428-77d70245b74d Duration: 8.84 ms Billed Duration: 100 ms Memory Size: 1856 MB Max Memory Used: 45 MB 之类的东西
  • 检查 IAM 权限,您的 Lambda 角色是否有 sns:publish?
  • 在您的sns.publish 调用中,我错过了参数TopicArn。但根据日志,执行到此为止。在您的处理程序 getAlert 中,我错过了一个 callback(...) 调用,因此 lambda 执行程序可能会忽略 request(...) 调用所做的承诺,并在执行到达 SNS 发布之前结束...

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


【解决方案1】:

正如我在评论中所写,我认为您混淆了执行中的承诺和回调。试试这个改变:

const options = {***};
const sentAlert = (msg, callback) => {
  const sns = new AWS.SNS();
  await sns.publish({
    TopicArn: ***
    Message: msg,
    PhoneNumber: '***',
    MessageAttributes: {
      'AWS.SNS.SMS.SenderID': {
        'DataType': 'String',
        'StringValue': '***'   
      }
    }
  }, function (err, data) {
  if (err) {
    console.log(err.stack);
    callback(err);
  }
  });
  console.log('sms sent');
  callback(null)
};

export const getAlert = (event, context, callback) => {
  request(options, (err, res, body) => {
    if (err) { 
      console.log('error: ', err);
      callback(err); 
    }
    if (body.length === 0 ) {
      console.log('Got no body!') 
      callback(null) 
    }
    console.log(`***`);
    const optionsId = {*** };
    request(optionsId, (err, res, body) => {
      if (err) {
        console.log(err.stack);
        callback(err);
      } 
      const msg = body.current.indexes[0].description;
      console.log('msg: ', msg);
      sentAlert(msg, callback);
    });
  });
};

但总的来说,我更喜欢使用 AWS Lambda nodejs8.10 映像支持的 async/await 机制。这将使您的代码简单且易于推理。

【讨论】:

  • 我已经添加了使用 sns 的权限,进行了建议的更改,并且还从 SentAlert 中删除了等待 - 仍然一样,它在本地调用时有效,而不是从 lambda...
  • 尝试通过 Cloud9 或仅使用日志来跟踪执行 - 查看执行停止的位置。
  • 还有一件事我忽略了:尝试从处理函数中删除async。我将编辑我的答案。
  • 好的,我有一些东西:CloudWatch 中的有用日志:User: arn:aws:sts::***:assumed-role/sns/***Alert is not authorized to perform: SNS:Publish on resource: PHONE_NUMBER。
  • 表示你的 lambda 角色无权执行“sns:publish”
猜你喜欢
  • 2017-06-05
  • 2023-03-12
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 2012-04-01
相关资源
最近更新 更多