J_Stan
J_Stan

Reputation: 503

AWS Step Functions - Utilize empty Map state output when no iterations occur

I have a workflow with 3 steps:

  1. Task - Upload N files
    • Produces array of N job definitions to be used as input to Step 2 map state
  2. Map - process each job
    • Due to map state, this is executed N times
  3. Task - do some other thing

What I would like is to only do Step 3 if any iterations occur in Step 2. The way this is designed, Step 1 usually produces no output, so Step 2 is basically skipped.

I've noticed that in the scenario I've outlined, the output from Step 2 is just [], where as normally it contains a whole lot of information about the iterations. Is it possible to perform this kind of workflow?

Upvotes: 3

Views: 4084

Answers (1)

Balu Vyamajala
Balu Vyamajala

Reputation: 10393

Choice state after step 1 to check if there is at least one record in Map, we can't check length of array, so using isPresent on first element of map $.inputForMap[0]

Step Definition

{
   "StartAt":"Dummy Step 1 Output",
   "States":{
      "Dummy Step 1 Output":{
         "Type":"Pass",
         "Result":[
            "iter 1",
            "iter2"
         ],
         "ResultPath":"$.inputForMap",
         "Next":"does map has atleast one record?"
      },
      "does map has atleast one record?":{
         "Type":"Choice",
         "Choices":[
            {
               "Variable":"$.inputForMap[0]",
               "IsPresent":true,
               "Next":"loop on map"
            }
         ],
         "Default":"End of Step Function"
      },
      "End of Step Function":{
         "Type":"Pass",
         "End":true
      },
      "Step three":{
         "Type":"Pass",
         "Next":"End of Step Function"
      },
      "loop on map":{
         "Type":"Map",
         "Next":"Step three",
         "Iterator":{
            "StartAt":"Step 2 - Looping on map",
            "States":{
               "Step 2 - Looping on map":{
                  "Type":"Pass",
                  "End":true
               }
            }
         },
         "ItemsPath":"$.inputForMap",
         "MaxConcurrency":1
      }
   }
}

When Map is not empty

enter image description here

When Map is empty

enter image description here

Upvotes: 4

Related Questions