State machines (Step functions)
State machines allow you to design and automate business processes and data pipelines by composing workloads (functions, batch-jobs) or other AWS services into workflows. State machines manage failures, retries, parallelization, service integrations, and observability so developers can focus on higher-value business logic.
When to use
Extract, Transform, and Load (ETL) process - state machines ensure that long-running, multiple ETL jobs execute in order and complete successfully, instead of manually orchestrating those jobs or maintaining a separate application.
Orchestrate microservices - use state machines to combine multiple functions into responsive serverless applications and microservices.
Definition of state machines is written using Amazon states language.
Amazon states language syntax allows you to to specify any workflow from easy ones to most complex ones.
The following example shows an order-payment flow made up of lambda functions:
Copyresources:checkAndHoldProduct:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'check-and-hold-product.ts'billCustomer:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'bill-customer.ts'shipmentNotification:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'shipment-notification.ts'eshopBuyingProcessStateMachine:type: 'state-machine'properties:definition:StartAt: 'checkAndHold'States:checkAndHold:Type: TaskResource: $ResourceParam('checkAndHoldProduct', 'arn')Next: billbill:Type: TaskResource: $ResourceParam('billCustomer', 'arn')Next: notifynotify:Type: TaskResource: $ResourceParam('shipmentNotification', 'arn')Next: succeedsucceed:Type: Succeed
The following example shows:
- generateReport - batch-job which generates report.
- uploadReport - function that uploads generated report.
- reportStateMachine - state machines that ties above compute resources together.
State machine definitions provide great flexibility. In this case, reportStateMachine only retries upload part of our workflow, since regenerating the report (in case of upload failure) would be costly and redundant.
Copyresources:uploadReport:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'upload-report.ts'generateReport:type: 'batch-job'properties:container:packaging:type: stacktape-image-buildpackproperties:entryfilePath: generate-report.tsresources:cpu: 2memory: 7800reportStateMachine:type: 'state-machine'properties:definition:StartAt: 'generate'States:generate:Type: TaskResource: 'arn:aws:states:::batch:submitJob.sync'Parameters:JobDefinition: $ResourceParam('generateReport', 'jobDefinitionArn')JobName: report-jobJobQueue: $Param('SHARED_GLOBAL', 'BatchOnDemandJobQueue::Arn')Next: uploadupload:Type: TaskResource: $Param('uploadReport', 'LambdaFunction::Arn')Next: succeedRetry:- ErrorEquals:- 'State.ALL'IntervalSeconds: 10succeed:Type: Succeed
The following parameters can be easily referenced using $ResourceParam directive directive.
To learn more about referencing parameters, refer to referencing parameters.
Arn of the state machine..
AWS (physical) name of the state machine.