在步进函数中捕获错误时向 sqs 发送消息

时间:2021-01-07 16:41:27

标签: aws-lambda amazon-sqs serverless aws-step-functions

我正在使用带有 serverless-step-functions 插件的无服务器框架。我想检查我的 stepfunction 工作流程中的任何错误并将此错误发送到 sqs 队列。

目前我想将所有输入作为消息传递到队列(MessageBody: $)。但是,如果我从队列中获取数据,则消息是 $(美元符号)而不是实际输入。如何将上一步中的错误消息发送到队列中?

  States:
    state1:
      Type: Task
      Resource:
        Fn::GetAtt: [function1, Arn]
      Next: state2
      Catch:
        - ErrorEquals: [States.ALL]
          Next: sendErrorToDLQ
          ResultPath: $.error
    state2:
      Type: Task
      Resource:
        Fn::GetAtt: [function2, Arn]
      Next: done
      Catch:
        - ErrorEquals: [ States.ALL ]
          Next: sendErrorToDLQ
          ResultPath: $.error
    sendErrorToDLQ:
      Type: Task
      Resource: arn:aws:states:::sqs:sendMessage
      Parameters:
        QueueUrl:
          Ref: ServiceDeadLetterQueue
        MessageBody: $    # <== how to pass input to sqs message
      Next: fail
    fail:
      Type: Fail
    done:
      Type: Succeed

1 个答案:

答案 0 :(得分:2)

我在连接 SNS 时得到了相同的结果。根据 AWS 文档,我们必须按照以下结构发送参数

<块引用>

"MessageBody.$": "$"

参考:https://docs.aws.amazon.com/step-functions/latest/dg/connect-sqs.html