Search code examples
amazon-web-servicesserverlessaws-step-functions

How to add dynamic TimeoutSeconds for activity in AWS Step Functions?


I have an activity in step function with TimeoutSeconds, like this:

ActivityWaiting:
    Type: Task
    ResultPath: $.output
    Resource: arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:activity:myActivity
    TimeoutSeconds: 3600

I would like to control the value of TimeoutSeconds and to change it with a parameter from the previous step. I've tried something like that:

ActivityWaiting:
    Type: Task
    ResultPath: $.output
    Resource: arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:activity:myActivity
    TimeoutSeconds: $.myTimeout

But unfortunately, it didn't work.

Edit: I would like to calculate/define the time myTimeout before executing the step function with python. Something like that:

data['myTimeout'] = getTimeout() #dymanic time in the seconds(ex 15000)

response = step_functions.start_execution(stateMachineArn=state_machine, input=json.dumps(data))

Solution

  • Since I couldn't find a solution for dynamic timeout.

    I've made a workaround using AWS Choice state

    I was needed to wait for an answer from a microservice, the time depended on the quantity of objects, which I've sent to it. Process of each object took like 3 minutes in average, therefore the timeout could be from 3 minutes and more.

    All the results, my microservice has written into a DB. So I created a lambda, that checks the DB in a loop.

    The exit condition is

    • Getting all the results in DB
    • Waiting time has ended, which calculated dynamically for each every execution

    I work with Serverless framework, here is my final solution:

    VerifyLambda:
        Type: Task
        Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:verify-step
        Next: IsFinished
    IsFinished:
        Type: Choice
        Choices:
            - Variable: $.isFinish
            BooleanEquals: false
            Next: Wait 3m
        Default: NextLambdaStep
    Wait 3m:
        Type: Wait
        Seconds: 180
        Next: VerifyLambda
    NextLambdaStep: ...
    

    Step function Visualization