SQS DeleteMessage阻止并且不返回响应

时间:2019-04-06 12:51:13

标签: aws-lambda aws-sdk amazon-sqs

我正在用lambda函数处理sqs消息,一旦成功处理,我会尝试将其删除。这就是问题所在。似乎userRep.getCurrentUser()的调用才刚刚完成。在deleteMessage返回之前的100秒后,lambda函数超时。

下面是用于尝试和删除消息的代码示例。 当我将其作为hack临时发送时,我将queueUrl与消息一起传递(目前处理队列URL,arns和名称是混乱且不一致的。)

我已经验证queueUrl和ReceiptHandle是正确的。

deleteMessage

我已添加此VPC端点以启用对sqs的访问:

async deleteSQSMessage(record: SQSRecord) {
    try {

        let queueUrl: any = record.messageAttributes['queueUrl']

        console.log(`Deleting message from queue: ${JSON.stringify(queueUrl)}`)

        if(queueUrl) {
            console.log(`Deleting queue message: ${record.body}`)
            let sqs = new SQS()
            let deleteParams = { QueueUrl: queueUrl.stringValue, ReceiptHandle: record.receiptHandle }
            // THIS IS THE LAST STATEMENT PRINTED IN THE LOGS (AFTER 100 SECS THE LAMBDA FUNCTION TIMES OUT
            console.log(`Delete params: ${JSON.stringify(deleteParams)}`)
            let result = await sqs.deleteMessage(deleteParams).promise()
            console.log(`Delete result: ${result}`)
        } else {
            console.error(`Invalid event record, no queueUrl attribute, cant delete message off of queue. ${record}`)
        }

    }
    catch(err) {
        console.error(JSON.stringify(err))
    }
}

上面引用的安全组:

SQSVPCEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub 'com.amazonaws.${AWS::Region}.sqs'
      VpcId: !Ref VPC
      VpcEndpointType: Interface
      SubnetIds:
        - !Ref SubnetA
        - !Ref SubnetB
        - !Ref SubnetC
      SecurityGroupIds: 
        - !Ref VPCSecurityGroup

我已授予lambda完全访问sqs的权限: Full access

在队列本身上,我允许任何人执行任何操作: enter image description here

有人对我在这里做错什么有任何想法吗?

2 个答案:

答案 0 :(得分:1)

来自the docs

Callback (callback):

function(err, data) { ... }

Called when a response from the service is returned. If a callback is not supplied, you
must call AWS.Request.send() on the returned request object to initiate the request.

我希望您的代码已准备好发生错误,例如:

sqs.deleteMessage(deleteParams , function(err, data) {
  if (err)
     console.log(err, err.stack);
  else
     console.log(data);
});

答案 1 :(得分:0)

事实证明,我在VPC和EndPoint上缺少一些属性...

我将这些添加到了VPC

EnableDnsSupport: true
EnableDnsHostnames: true

并将其添加到端点

PrivateDnsEnabled: true

然后lambda函数终于能够与SQS进行通信