即使将多个消息发送到SQS,SQS lambda也仅触发一次

时间:2019-07-15 14:16:52

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

我有两个lambda函数,一个向SQS发送消息,另一个在SQS有消息时触发。

我的serverlesss.yml文件是

service: testSLS-serverless

plugins:
  - serverless-webpack
  - serverless-offline-sqs
  - serverless-offline
  - serverless-dotenv-plugin
  - serverless-domain-manager

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true
    keepOutputDirectory: true
    individually: true
  serverless-offline:
    port: 4000
  customDomain:
    domainName: mydomain.io
    basePath: ''
    stage: ${self:provider.stage}
    createRoute53Record: true
  serverless-offline-sqs:
    autoCreate: true
    apiVersion: '2012-11-05'
    endpoint: http://0.0.0.0:9324
    region: eu-west-1
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: us-east-1
  tracing:
    lambda: true

functions: 
  sendSQSMessage:
    handler: src/sqs/sendMessage.sendSQSMessage
    events: 
      - http:
          method: get
          path: sendSQSMessage
  sqsTrigger:
    handler: src/sqs/receiveMessageTrigger.sqsTrigger
    events: 
      - sqs:
          queueName: MyQueue
          arn:
            Fn::GetAtt:
              - MyQueue
              - Arn
          batchSize: 1

resources:
  Resources:
    MyQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: MyQueue

package:
  exclude:
    - node_modules/serverless-offline/**

这就是我调用向SQS发送消息的lambda的方式

urls.map((p) => sendSQSMessage(p, service, createdIdentity.id, userId)); lambda sendSQSMessage是我将数据发送到Queue的位置,

import client from './index';

// serverless-offline-sqs testing QUEUE NAME
const QUEUE_URL = 'http://0.0.0.0:9324/queue/MyQueue';

export const sendSQSMessage = async (url, service, identityId, userId) => {
  const imageMetaData = {
    Service: {
      DataType: 'String',
      StringValue: service,
    },
    User: {
      DataType: 'String',
      StringValue: userId,
    },
    Identity: {
      DataType: 'String',
      StringValue: identityId,
    },
  };
  const sqsParams = {
    DelaySeconds: 10,
    MessageAttributes: imageMetaData,
    MessageBody: `${url}`,
    QueueUrl: QUEUE_URL,
  };
  const params = {
    QueueUrl: QUEUE_URL /* required */,
  };
  client.sendMessage(sqsParams, (error, dataMessage) => {
    if (error) {
      logger.info('Error while sending queue data', error);
    } else {
      logger.info('DATA SENT', dataMessage);
    }
  });
};

这是作为触发器附加到队列的lambda:

import AWS from 'aws-sdk';

const QUEUE_URL = 'http://0.0.0.0:9324/queue/MyQueue';

export const sqsTrigger = (event, context) => {
   console.log('-----------Event in trigger-----------', event.Records[0]);

};

现在我遇到的问题是,即使有多个消息发送到Queue,触发器lambda也仅被调用一次。 我希望通过urls数组的长度来调用lambda 有人可以指导我在这方面做错什么吗?

0 个答案:

没有答案