在 step 函数中读取先前 lambda 的输出

reading the output of previous lambdas in step function

下面是我的状态机图

Step function graph

当 track、coverart 迁移失败时,我想我已经放置了一个将重定向到 dbFallback 的捕获器,现在我想将 dbMigration 的输出作为 dbFallback 的输入,我该如何实现?

这是我的 yaml 文件

stepFunctions:
  stateMachines:
    divoMigrationMachine:
      name: divoMigrationMachine
      role: arn:aws:iam::#{AWS::AccountId}:role/migration-stepfunction-role
      definition:
        StartAt: dbMigration
        States:
          dbMigration:
            Type: Task
            Resource: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:migration-pilot-backend-${self:provider.stage}-dbMigration
            Next: Parallel
          Parallel:
            Type: Parallel
            Next: Final State
            Branches:
              - StartAt: coverartMigration
                States:
                  coverartMigration:
                    Type: Task
                    Resource: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:migration-pilot-backend-${self:provider.stage}-coverartMigration
                    End: true
              - StartAt: trackMigration
                States:
                  trackMigration:
                    Type: Task
                    Resource: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:migration-pilot-backend-${self:provider.stage}-trackMigration
                    End: true
            Catch:
              - ErrorEquals: ["States.ALL"]
                Next: dbFallback
          dbFallback:
            Type: Task
            Resource: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:migration-pilot-backend-${self:provider.stage}-dbFallback
            End: true
          Final State:
            Type: Pass
            End: True

据我了解你的问题,现在不可能既捕获 lambda 错误又使用它的输出,因为它失败的事实意味着它没有成功产生任何输出。意义不大。

或者,您可以使用 try / except 语句在 lambda 本身内部捕获 lambda 错误。然后,如果 lambda 错误你 return 一个特定的有效负载到你的 stepfunction。 然后在这个 dbMigration lambda 之后,添加一个 choice 任务,根据 thdbMigration lambda 的输出检查迁移是否成功。如果不成功,调用 dbFallback。

通过捕获 dbMigration lambda 内部的错误,choice 任务允许您在不丢失信息的情况下调用 dbFallback lambda。