skip to Main Content

I have step function with 5 steps, each step writes to s3

I want all steps to write to s3://my-bucket/my-step-function/YYYY/MM/DD/HH/mm/specifc-step-name/ and that date of the dir will be the same in all steps

Is there a way to calculate the current date and pass it to all steps running? or even calculate and pass the entire string (without the specific step name obviously…)

thanks

2

Answers


  1. In AWS stepfunctions, if you edit your state machine in workflow studio, you can change the configurations for each step.

    To pass same input data to next step, you can simply enable below setting on each step.enter image description here

    You can transform the result or pass same input for next step or pass only output as per your requirement.

    This will change state machine Json definition accordingly as "ResultPath": null.

    Login or Signup to reply.
  2. Passing variables through step functions is a pain, as you have to keep the parameters through all concatenated steps. It can be done though, by adding step results to the input using ResultPath, instead of replacing the input.

    However, what I would find easier is defining your step function that it takes the path as input. Assuming your step function receives the following input:

    {
     "path": "s3://my-bucket/my-step-function/YYYY/MM/DD/HH/mm/specifc-step-name/"
    }
    

    You could access this parameter in all your steps like so:

    "DoSomething" : {
          "Next" : "DoSomethingElse",
          "Parameters" : {
            "path.$" : "$$.Execution.Input.path",
          },
          "ResultPath" : "$.my_result",
          "Type" : "Pass"
    },
    

    If you cannot calculate your path outside of step functions, you could write a nested step function, meaning the outer step function calculates the date and then triggers the inner step function that can then read the parameter as described above.

    To get the current time, you could use "$$.State.EnteredTime". This gives you the time when the state was entered.

    To add to this, in your case you can actually do it in one step function, by taking the StartTime of the Execution ($$.Execution.StartTime) and formatting this timestamp into your required path. The below code can be added as parameter to every step, as it only requires the Context Objects:

    {
      "path.$": "States.Format('s3://my-bucket/my-step-function/{}/{}/{}/{}/{}/{}/', States.ArrayGetItem(States.StringSplit($$.Execution.StartTime, '-'), 0), States.ArrayGetItem(States.StringSplit($$.Execution.StartTime, '-'), 1), States.ArrayGetItem(States.StringSplit(States.ArrayGetItem(States.StringSplit($$.Execution.StartTime, '-'), 2), 'T'), 0), States.ArrayGetItem(States.StringSplit(States.ArrayGetItem(States.StringSplit($$.Execution.StartTime, 'T'), 1), ':'), 0), States.ArrayGetItem(States.StringSplit($$.Execution.StartTime, ':'), 1), $$.State.Name)"
    }
    

    Hope that helps. For more help consider reading about intrinsic functions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search