是否可以在lambda函数中发送多个SNS?

时间:2017-08-25 00:53:04

标签: aws-lambda amazon-sns

我尝试使用Promise来做到这一点,但总是出现超时错误。已记录所有Going to send但未记录SNS 0 has errorSNS 0 has no error

function sendSNSMessage(params, index) {
  return new Promise(function (resolve, reject) {
    console.log('Going to send', index, params)
    sns.publish(params, function(err, data) {
      if (err) {
        console.log(`SNS ${index} has error: ${err}`)
        return reject(err)
      }
      console.log(`SNS ${index} has no error: ${data}`)
      resolve(data)
    })
  })
}

exports.query = (event, context, callback) => {
  const connection = mysql.createConnection(config.db)
  try {
    connection.connect(function(err) {
      if (!err) {
        connection.query('SELECT * FROM urls', function(err, results) {
          if (!err) {
            const messsages = results.map((result, index)=>{
              console.log(`results[${index}]: ${result.url}`)
              return sendSNSMessage({
                Message: result.url,
                TopicArn: config.sns.queryArn
              }, index)
            })

            Promise.all(messsages).then(()=>{
              callback(null, 'All messages sent')
            }, console.error)
          } else {
            console.log('Query error!')
            callback(err)
          }
          connection.end()
        })
      } else {
        console.log('Error connecting database ...' + err.message)
      }
    })

  } catch (error) {
    callback(`Exceptiodn: ${error}`)
  }
}

基本上背后的想法是:

  • 执行mysql查询以获取一些记录
  • 每个记录发送一个SNS tp触发另一个lambda函数

0 个答案:

没有答案