Search code examples
amazon-web-servicesaws-lambdaserverlessaws-step-functions

reading the output of previous lambdas in step function


Below is my state machine graph

Step function graph

when track,coverart migration fails i want i have put a catch which will redirect to dbFallback, now i want the output from dbMigration as the input for dbFallback how do i achieve that?

this is my yaml file

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

Solution

  • As I understand your question now it's not possible to both Catch the lambda error and use it's output, because the fact that it failed means it didn't successfully produce any output. It wouldn't make much sense.

    Alternatively, you can catch the lambda error yourself INSIDE the lambda itself using a try / except statement. Then if the lambda errors you return a specific payload to your stepfunction. Then after this dbMigration lambda, you add a choice task that checks whether or not the migration was successful based on the output of thdbMigration lambda. If NOT succesful, invoke the dbFallback.

    By catching the error inside the dbMigration lambda, the choice task allows you to invoke the dbFallback lambda without loss of information.