def branch_retry_parallel_state_example(): finally_state = awssl.Pass( Name="MyFinally", Comment= "This is the branch to execute as a 'finally' statement to the Parallel state", ResultAsJSON={"Finally": "Completed"}, EndState=True) # Create the branch processing to be performed - in this case returning the name of the StartState p1 = awssl.Pass(Name="Dummy1", EndState=True, ResultAsJSON={"Value": "Dummy1"}) p2 = awssl.Pass(Name="Dummy2", EndState=True, ResultAsJSON={"Value": "Dummy2"}) p3 = awssl.Pass(Name="Dummy3", EndState=True, ResultAsJSON={"Value": "Dummy3"}) # Attempt to retry any branch error using the defaults r = awssl.Retrier(ErrorNameList=["States.ALL"]) # Run the branches in parallel, each having an individual retrier on any error returned para = awssl.ext.BranchRetryParallel(Name="MyParallel", BranchList=[p1, p2, p3], BranchRetryList=[r], FinallyState=finally_state, EndState=True) # Construct state machine sm = awssl.StateMachine(Comment="This is a test", StartState=para) return sm
def limit_parallel_with_branch_retry_example(iterations, max_concurrency): # Declare the Arns for the Lambda functions required by awssl.ext.For awssl.ext.set_ext_arns( ForInitializer="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForExtractor="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForConsolidator="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizer="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizerParallelIterations="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", LimitedParallelConsolidator="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME") # Create the branch of concurrent processing to be performed - in this case extraction of the iteration value p = awssl.Pass(Name="Dummy", EndState=True, OutputPath="$.iteration.Iteration") # This example is identifical to limit_parallel_example, except that retriers are going to be added # for the branch executions r1 = awssl.Retrier(ErrorNameList=["MyError", "MyOtherError"]) r2 = awssl.Retrier(ErrorNameList=["States.ALL"]) # Sometimes we want to throttle concurrent processing - for example to prevent Lambda function throttling # awssl.ext.LimitedParallel can limit the number of concurrent branches being processed at any given time parallel = awssl.ext.LimitedParallel( Name="LimitedParallel", EndState=True, Iterations=iterations, IteratorPath="$.iteration", MaxConcurrency=max_concurrency, BranchState=p, BranchRetryList=[r1, r2]) # Construct state machine sm = awssl.StateMachine(Comment="This is a test") sm.set_start_state(parallel) return sm
def parallel_example(): # Construct states for each branch branch1 = awssl.Wait( Name="Wait 20s", WaitForSeconds=20, EndState=True) wait_10s = awssl.Wait( Name="Wait 10s", WaitForSeconds=10, EndState=True) branch2 = awssl.Pass( Name="Pass", NextState=wait_10s) # Construct states for main branch final_state = awssl.Pass( Name="Final State", EndState=True) parallel = awssl.Parallel( Name="Parallel", NextState=final_state, EndState=False, BranchList=[branch1, branch2]) # Construct state machine return awssl.StateMachine( Comment="An example of the Amazon States Language using a parallel state to execute two branches at the same time.", StartState=parallel)
def limit_parallel_example(iterations, max_concurrency): # Declare the Arns for the Lambda functions required by awssl.ext.For awssl.ext.set_ext_arns( ForInitializer= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForExtractor="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizer="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizerParallelIterations= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", LimitedParallelConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME") # Create the branch of concurrent processing to be performed - in this case extraction of the iteration value p = awssl.Pass(Name="Dummy", EndState=True, OutputPath="$.iteration.Iteration") # Sometimes we want to throttle concurrent processing - for example to prevent Lambda function throttling # awssl.ext.LimitedParallel can limit the number of concurrent branches being processed at any given time parallel = awssl.ext.LimitedParallel(Name="LimitedParallel", EndState=True, Iterations=iterations, IteratorPath="$.iteration", MaxConcurrency=max_concurrency, BranchState=p) # Construct state machine sm = awssl.StateMachine(Comment="This is a test") sm.set_start_state(parallel) return sm
def choice_example(): # Construct states for each choice default = awssl.Fail( Name="Default", ErrorCause="No Matches!") next_state = awssl.Task( Name="NextState", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=True) first_match_state = awssl.Task( Name="FirstMatchState", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=False, NextState=next_state) second_match_state = awssl.Task( Name="SecondMatchState", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=False, NextState=next_state) # Construct Choice Rules first_rule = awssl.ChoiceRule( Comparison=awssl.Comparison( Variable="$.foo", Comparator="NumericEquals", Value=1), NextState=first_match_state) second_rule = awssl.ChoiceRule( Comparison=awssl.Comparison( Variable="$.foo", Comparator="NumericEquals", Value=2), NextState=second_match_state) # Construct states for main branch choice_state = awssl.Choice( Name="ChoiceState", ChoiceList=[first_rule, second_rule], Default=default) first_state = awssl.Task( Name="FirstState", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=False, NextState=choice_state) # Construct state machine return awssl.StateMachine( Comment="An example of the Amazon States Language using a choice state.", StartState=first_state)
def for_state_example(): # Declare the Arns for the Lambda functions required by awssl.ext.For awssl.ext.set_ext_arns( ForInitializer= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForExtractor="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizer="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizerParallelIterations= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", LimitedParallelConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME") # Create the branch processing to be performed - in this case extraction of the iteration value p = awssl.Pass(Name="Dummy", EndState=True, OutputPath="$.iteration.Iteration") # Create 3 For instances to demonstrate the different From/To/Step features # Notice that the same branch definition can be reused across all the For loops s1 = awssl.ext.For(Name="For1", EndState=True, From=0, To=5, Step=1, BranchState=p, ParallelIteration=True) s2 = awssl.ext.For(Name="For2", EndState=True, From=0, To=8, Step=2, BranchState=p, ParallelIteration=True) s3 = awssl.ext.For(Name="For3", EndState=True, From=-16, To=16, Step=2, BranchState=p) # Run the For loops in parallel, to demonstrate there is no interdependencies para = awssl.Parallel(Name="Parallel-Fors", BranchList=[s1, s2, s3], EndState=True) # Construct state machine sm = awssl.StateMachine(Comment="This is a test") sm.set_start_state(para) return sm
def test2(): import awssl # Construct states hello_world = awssl.Pass( Name="HelloWorld", EndState=True) # Construct state machine return awssl.StateMachine( Comment="A Hello World example of the Amazon States Language using a Pass state", StartState=hello_world)
def for_state_example(): # Declare the Arns for the Lambda functions required by awssl.ext.For awssl.ext.set_ext_arns( ForInitializer= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForExtractor="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizer="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", ForFinalizerParallelIterations= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", LimitedParallelConsolidator= "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME") # Create the branch processing to be performed - in this case extraction of the iteration value p = awssl.Pass(Name="Dummy", EndState=True, OutputPath="$.iteration.Iteration") r = awssl.Retrier(ErrorNameList=["States.ALL"]) # Create 2 identical For loops - one where the branches are retryable, and one where they aren't s1 = awssl.ext.For(Name="For1", EndState=True, From=0, To=5, Step=1, BranchState=p, BranchRetryList=[r], ParallelIteration=True) s2 = awssl.ext.For(Name="For2", EndState=True, From=0, To=5, Step=1, BranchState=p, ParallelIteration=True) # Run the For loops in parallel, to demonstrate there is no interdependencies para = awssl.Parallel(Name="Parallel-Fors", BranchList=[s1, s2], EndState=True) # Construct state machine sm = awssl.StateMachine(Comment="This is a test") sm.set_start_state(para) return sm
def parallel_with_finally_example(): finally_state = awssl.Pass( Name="Finally", Comment= "This is the branch to execute as a 'finally' statement to the Task", ResultAsJSON={"Finally": "Completed"}, EndState=True) catch_state_0 = awssl.Pass( Name="Catcher-MyError", Comment= "This is the branch to execute when a 'MyError' error is caught", ResultAsJSON={"Caught": "Error"}, EndState=True) catch_state_1 = awssl.Pass( Name="Catcher-All", Comment="This is the branch to execute any other error is caught", ResultAsJSON={"Caught": "Error"}, EndState=True) branch_list = [] for n in range(1, 6): branch = awssl.Pass( Name="Branch {}".format(n), Comment="This Pass state is an example of a branch", ResultAsJSON={"Branch": n}, EndState=True) branch_list.append(branch) # Construct states hello_world = awssl.ext.ParallelWithFinally( Name="HelloWorld", Comment="This is the actual Parallel to be executed", BranchList=branch_list, FinallyState=finally_state, CatcherList=[ awssl.Catcher(ErrorNameList=["MyError"], NextState=catch_state_0), awssl.Catcher(ErrorNameList=["States.ALL"], NextState=catch_state_1) ], EndState=True) # Construct state machine return awssl.StateMachine( Comment= "A Parallel finally example, where the finally branch is executed both after successful completion, and before any Catchers are triggered when an error occurs", StartState=hello_world)
def catch_failure_example(): # Fallback states reserved_type_fallback = awssl.Pass( Name="ReservedTypeFallback", ResultAsJSON={"msg": "This is a fallback from a reserved error code"}, OutputPath="$.msg", EndState=True) catch_all_fallback = awssl.Pass( Name="CatchAllFallback", ResultAsJSON={"msg": "This is a fallback from a reserved error code"}, OutputPath="$.msg", EndState=True) custom_error_fallback = awssl.Pass( Name="CustomErrorFallback", ResultAsJSON={ "msg": "This is a fallback from a custom lambda function exception" }, OutputPath="$.msg", EndState=True) # Catchers for the HelloWorld task state states_all_catcher = awssl.Catcher(ErrorNameList=["States.ALL"], NextState=catch_all_fallback) reserved_type_catcher = awssl.Catcher(ErrorNameList=["States.TaskFailed"], NextState=reserved_type_fallback) custom_error_catcher = awssl.Catcher(ErrorNameList=["CustomError"], NextState=custom_error_fallback) # Construct states hello_world = awssl.Task( Name="HelloWorld", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=True, CatcherList=[ custom_error_catcher, reserved_type_catcher, states_all_catcher ]) # Construct state machine return awssl.StateMachine( Comment= "A Catch example of the Amazon States Language using an AWS Lambda Function", StartState=hello_world)
def task_with_finally_example(): finally_state = awssl.Pass( Name="Finally", Comment= "This is the branch to execute as a 'finally' statement to the Task", ResultAsJSON={"Finally": "Completed"}, EndState=True) catch_state_0 = awssl.Pass( Name="Catcher-MyError", Comment= "This is the branch to execute when a 'MyError' error is caught", ResultAsJSON={"Caught": "Error"}, EndState=True) catch_state_1 = awssl.Pass( Name="Catcher-All", Comment="This is the branch to execute any other error is caught", ResultAsJSON={"Caught": "Error"}, EndState=True) # Construct states hello_world = awssl.ext.TaskWithFinally( Name="HelloWorld", Comment="This is the actual Task to be executed", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", FinallyState=finally_state, CatcherList=[ awssl.Catcher(ErrorNameList=["MyError"], NextState=catch_state_0), awssl.Catcher(ErrorNameList=["States.ALL"], NextState=catch_state_1) ], EndState=True) # Construct state machine return awssl.StateMachine( Comment= "A Task finally example, where the finally branch is executed both after successful completion, and before any Catchers are triggered when an error occurs", StartState=hello_world)
def wait_state_example(): # Construct states final_state = awssl.Task( Name="FinalState", EndState=True, ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME") wait_using_seconds_path = awssl.Wait( Name="wait_using_seconds_path", NextState=final_state, WaitForSecondsPath="$.expiryseconds") wait_using_timestamp_path = awssl.Wait( Name="wait_using_timestamp_path", NextState=wait_using_seconds_path, WaitUntilISO8601TimestampPath="$.expirydate") wait_using_timestamp = awssl.Wait( Name="wait_using_timestamp", NextState=wait_using_timestamp_path, WaitUntilISO8601Timestamp="2015-09-04T01:59:00Z") wait_using_seconds = awssl.Wait( Name="wait_using_second", NextState=wait_using_timestamp, WaitForSeconds=10) first_state = awssl.Task( Name="FirstState", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=False, NextState=wait_using_seconds) # Construct state machine return awssl.StateMachine( Comment="An example of the Amazon States Language using wait states", StartState=first_state)
def retry_failure_example(): retrier1 = awssl.Retrier(ErrorNameList=["CustomError"], IntervalSeconds=1, MaxAttempts=2, BackoffRate=2.0) states_all_retrier = awssl.Retrier(ErrorNameList=["States.ALL"], IntervalSeconds=5, MaxAttempts=5, BackoffRate=2.0) # Construct states hello_world = awssl.Task( Name="HelloWorld", ResourceArn="arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", EndState=True, RetryList=[retrier1, states_all_retrier]) # Construct state machine return awssl.StateMachine( Comment= "A Retry example of the Amazon States Language using an AWS Lambda Function", StartState=hello_world)
def branch_retry_parallel_state_example(): # Create the branch processing to be performed - in this case returning the name of the StartState p1 = awssl.Pass(Name="Dummy1", EndState=True, ResultAsJSON={"Value": "Dummy1"}) p2 = awssl.Pass(Name="Dummy2", EndState=True, ResultAsJSON={"Value": "Dummy2"}) p3 = awssl.Pass(Name="Dummy3", EndState=True, ResultAsJSON={"Value": "Dummy3"}) r = awssl.Retrier(ErrorNameList=["States.ALL"]) # Run the branches in parallel, each having an individual retrier on any error returned para = awssl.ext.BranchRetryParallel(Name="Parallel", BranchList=[p1, p2, p3], BranchRetryList=[r], EndState=True) # Construct state machine sm = awssl.StateMachine(Comment="This is a test", StartState=para) return sm