无服务器部署不会将AWS Lambda与SQS事件集成

时间:2019-05-19 15:51:30

标签: aws-lambda amazon-sqs serverless-framework

我正在尝试使用无服务器框架创建与名为“ sendExportJob”的AWS Lambda函数关联的AWS SQS事件,但是在部署之后,我在AWS控制台中看不到SQS触发器。显然,我可以通过控制台手动添加此事件,它可以按预期运行。

这是serverless.yml中的Lambda函数配置:

sendExportJob:
  handler: src/sendExportJob.handler
  role: sendExportJobIAM
  memorySize: ${self:custom.config.send-exportjob-lambda-settings.memorySize}
  timeout: ${self:custom.config.send-exportjob-lambda-settings.timeout} 
  environment:
    lambdaName: ${self:custom.config.send-exportjob-lambda-settings.name}
    pendingqueueUrl: https://sqs.${self:custom.config.region}.amazonaws.com/${self:custom.config.account-id}/${self:custom.config.sqs-pending-exports-queue-name}
    region: ${self:custom.config.region} 
  events:
    - schedule: rate(${self:custom.config.send-exportjob-lambda-settings.schedule-rate-minutes} minutes)
    - pendingsqs:
      arn:  arn:aws:sqs:${self:custom.config.region}:${self:custom.config.account-id}:${self:custom.config.sqs-pending-exports-queue-name}
      batchSize: 1

以下是此特定功能的IAM配置:

 sendExportJobIAM:
    Type: AWS::IAM::Role
    Properties:
      RoleName: sendExportJobRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: sendExportJobIAMAll
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                  - logs:CreateLogGroup
                Resource: '*'
              - Effect: Allow
                Action:
                  - sqs:ChangeMessageVisibility
                  - sqs:ChangeMessageVisibilityBatch
                  - sqs:DeleteMessage
                  - sqs:DeleteMessageBatch
                  - sqs:GetQueueAttributes
                  - sqs:ReceiveMessage
                Resource:   arn:aws:sqs:${self:custom.config.region}:${self:custom.config.account-id}:${self:custom.config.sqs-pending-exports-queue-name}
              - Effect: Allow
                Action:
                  - lambda:InvokeFunction
                Resource: '*'

我正在使用最新版本的无服务器框架,并且已经检查了配置文件中的缩进。

SQS资源先前已部署,因此在将其添加为事件之前已经存在。

1 个答案:

答案 0 :(得分:1)

您在sqs部分下缺少events属性。我真的不确定pendingsqs的意思,因为这不是有效的无服务器框架关键字

sendExportJob:
  handler: src/sendExportJob.handler
  role: sendExportJobIAM
  memorySize: ${self:custom.config.send-exportjob-lambda-settings.memorySize}
  timeout: ${self:custom.config.send-exportjob-lambda-settings.timeout} 
  environment:
    lambdaName: ${self:custom.config.send-exportjob-lambda-settings.name}
    pendingqueueUrl: https://sqs.${self:custom.config.region}.amazonaws.com/${self:custom.config.account-id}/${self:custom.config.sqs-pending-exports-queue-name}
    region: ${self:custom.config.region} 
  events:
    - schedule: rate(${self:custom.config.send-exportjob-lambda-settings.schedule-rate-minutes} minutes)
    - sqs: arn:aws:sqs:${self:custom.config.region}:${self:custom.config.account-id}:${self:custom.config.sqs-pending-exports-queue-name}

如果您要像上面那样配置批处理大小,只需将其放在sqs列表中的events项目下即可,

sendExportJob:
      handler: src/sendExportJob.handler
      role: sendExportJobIAM
      memorySize: ${self:custom.config.send-exportjob-lambda-settings.memorySize}
      timeout: ${self:custom.config.send-exportjob-lambda-settings.timeout} 
      environment:
        lambdaName: ${self:custom.config.send-exportjob-lambda-settings.name}
        pendingqueueUrl: https://sqs.${self:custom.config.region}.amazonaws.com/${self:custom.config.account-id}/${self:custom.config.sqs-pending-exports-queue-name}
        region: ${self:custom.config.region} 
      events:
        - schedule: rate(${self:custom.config.send-exportjob-lambda-settings.schedule-rate-minutes} minutes)
        - sqs: 
            arn: aws:sqs:${self:custom.config.region}:${self:custom.config.account-id}:${self:custom.config.sqs-pending-exports-queue-name}
            batchSize: 1

所有记录here